/
IOSElementWrapper.cs
133 lines (123 loc) · 4.52 KB
/
IOSElementWrapper.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
namespace Legerity.IOS.Elements;
using System;
using Legerity.Exceptions;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.iOS;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
/// <summary>
/// Defines an element wrapper for a <see cref="IOSElement"/>.
/// </summary>
public class IOSElementWrapper : ElementWrapper<IOSElement>
{
/// <summary>
/// Initializes a new instance of the <see cref="IOSElementWrapper"/> class.
/// </summary>
/// <param name="element">
/// The <see cref="IOSElement"/> reference.
/// </param>
public IOSElementWrapper(IOSElement element)
: base(element)
{
}
/// <summary>
/// Gets the instance of the Appium driver for the iOS application.
/// </summary>
public IOSDriver<IOSElement> Driver => this.ElementDriver as IOSDriver<IOSElement>;
/// <summary>
/// Allows conversion of a <see cref="IOSElement"/> to the <see cref="IOSElementWrapper"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="IOSElement"/>.
/// </param>
/// <returns>
/// The <see cref="IOSElementWrapper"/>.
/// </returns>
public static implicit operator IOSElementWrapper(IOSElement element)
{
return new IOSElementWrapper(element);
}
/// <summary>
/// Allows conversion of a <see cref="AppiumWebElement"/> to the <see cref="IOSElementWrapper"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="AppiumWebElement"/>.
/// </param>
/// <returns>
/// The <see cref="IOSElementWrapper"/>.
/// </returns>
public static implicit operator IOSElementWrapper(AppiumWebElement element)
{
return new IOSElementWrapper(element as IOSElement);
}
/// <summary>
/// Allows conversion of a <see cref="RemoteWebElement"/> to the <see cref="IOSElementWrapper"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="RemoteWebElement"/>.
/// </param>
/// <returns>
/// The <see cref="IOSElementWrapper"/>.
/// </returns>
public static implicit operator IOSElementWrapper(RemoteWebElement element)
{
return new IOSElementWrapper(element as IOSElement);
}
/// <summary>
/// Determines whether the specified element is shown with the specified timeout.
/// </summary>
/// <param name="locator">The locator to find a specific element.</param>
/// <param name="timeout">
/// The amount of time the driver should wait when searching for the <paramref name="locator"/> if it is not immediately present.
/// </param>
/// <exception cref="ElementNotShownException">Thrown when an element is not shown for the expected locator.</exception>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
protected void VerifyDriverElementShown(By locator, TimeSpan? timeout)
{
if (timeout == null)
{
try
{
if (this.Driver.FindElement(locator) == null)
{
throw new ElementNotShownException(locator.ToString());
}
}
catch (NoSuchElementException ex)
{
throw new ElementNotShownException(locator.ToString(), ex);
}
}
else
{
var wait = new WebDriverWait(this.Driver, timeout.Value);
wait.Until(driver => driver.FindElement(locator) != null);
}
}
/// <summary>
/// Determines whether the specified elements are shown with the specified timeout.
/// </summary>
/// <param name="locator">
/// The locator to find a collection of elements.
/// </param>
/// <param name="timeout">
/// The amount of time the driver should wait when searching for the <paramref name="locator"/> if it is not immediately present.
/// </param>
/// <exception cref="ElementsNotShownException">Thrown when no elements are shown for the expected locator.</exception>
protected void VerifyDriverElementsShown(By locator, TimeSpan? timeout)
{
if (timeout == null)
{
if (this.Driver.FindElements(locator).Count == 0)
{
throw new ElementsNotShownException(locator.ToString());
}
}
else
{
var wait = new WebDriverWait(this.Driver, timeout.Value);
wait.Until(driver => driver.FindElements(locator).Count != 0);
}
}
}