/
FindExtensions.cs
87 lines (80 loc) · 3.72 KB
/
FindExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System.Collections.Generic;
using OpenQA.Selenium;
namespace Nito.BrowserBoss.Finders
{
/// <summary>
/// Extension methods for finders.
/// </summary>
public static class FindExtensions
{
/// <summary>
/// Finds all matching elements. Does not throw exceptions; an empty enumeration is returned if any errors occur.
/// </summary>
/// <param name="this">The finder.</param>
/// <param name="context">The context of the search. All results should be within this context.</param>
/// <param name="searchText">The search string used to match the elements.</param>
public static IReadOnlyCollection<IWebElement> TryFind(this IFind @this, ISearchContext context, string searchText)
{
try
{
return @this.Find(context, searchText);
}
catch
{
return new IWebElement[0];
}
}
/// <summary>
/// Finds all matching elements. Only returns results from the first finder that returns results. Does not throw exceptions; an empty enumeration is returned if any errors occur.
/// </summary>
/// <param name="this">The finders.</param>
/// <param name="context">The context of the search. All results should be within this context.</param>
/// <param name="searchText">The search string used to match the elements.</param>
public static IReadOnlyCollection<IWebElement> TryFind(this IEnumerable<IFind> @this, ISearchContext context, string searchText)
{
foreach (var finder in @this)
{
var result = finder.TryFind(context, searchText);
if (result.Count != 0)
return result;
}
return new IWebElement[0];
}
/// <summary>
/// Finds all matching elements, searching child iframes if no matches were found. Only returns results from the first finder that returns results. Does not throw exceptions; an empty enumeration is returned if any errors occur.
/// </summary>
/// <param name="this">The finders.</param>
/// <param name="webDriver">The web driver, representing the top-level context of this search.</param>
/// <param name="context">The context of the search. All results should be within this context.</param>
/// <param name="searchText">The search string used to match the elements.</param>
public static IReadOnlyCollection<IWebElement> TryFind(this IReadOnlyCollection<IFind> @this, IWebDriver webDriver, ISearchContext context, string searchText)
{
// First, return all the normal matches.
webDriver.SwitchTo().DefaultContent();
var result = @this.TryFind(context, searchText);
if (result.Count != 0)
return result;
// If more results are required, then search the iframes.
foreach (var iframe in context.FindElements(By.TagName("iframe")))
{
webDriver.SwitchTo().Frame(iframe);
var html = webDriver.FindElement(By.TagName("html"));
result = @this.TryFind(webDriver, html, searchText);
if (result.Count != 0)
return result;
}
return result;
}
/// <summary>
/// The default finders used by BrowserBoss.
/// </summary>
public static IEnumerable<IFind> DefaultFinders()
{
yield return new FindByJQueryCss();
yield return new FindByXPath();
yield return new FindByValue();
yield return new FindByLabel();
yield return new FindByText();
}
}
}