/
Spinner.cs
120 lines (107 loc) · 4.42 KB
/
Spinner.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
namespace Legerity.Android.Elements.Core;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
/// <summary>
/// Defines a <see cref="AndroidElement"/> wrapper for the core Android Spinner control.
/// </summary>
public class Spinner : AndroidElementWrapper
{
/// <summary>
/// Initializes a new instance of the <see cref="Spinner"/> class.
/// </summary>
/// <param name="element">
/// The <see cref="AndroidElement"/> reference.
/// </param>
public Spinner(AndroidElement element)
: base(element)
{
}
/// <summary>
/// Gets the currently selected item.
/// </summary>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
public virtual string SelectedItem => this.GetSelectedItem();
/// <summary>
/// Allows conversion of a <see cref="AndroidElement"/> to the <see cref="Spinner"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="AndroidElement"/>.
/// </param>
/// <returns>
/// The <see cref="Spinner"/>.
/// </returns>
public static implicit operator Spinner(AndroidElement element)
{
return new Spinner(element);
}
/// <summary>
/// Allows conversion of a <see cref="AppiumWebElement"/> to the <see cref="Spinner"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="AppiumWebElement"/>.
/// </param>
/// <returns>
/// The <see cref="Spinner"/>.
/// </returns>
public static implicit operator Spinner(AppiumWebElement element)
{
return new Spinner(element as AndroidElement);
}
/// <summary>
/// Allows conversion of a <see cref="RemoteWebElement"/> to the <see cref="Spinner"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="RemoteWebElement"/>.
/// </param>
/// <returns>
/// The <see cref="Spinner"/>.
/// </returns>
public static implicit operator Spinner(RemoteWebElement element)
{
return new Spinner(element as AndroidElement);
}
/// <summary>
/// Selects an item in the combo-box with the specified item name.
/// </summary>
/// <param name="name">
/// The name of the item to select.
/// </param>
/// <exception cref="InvalidElementStateException">Thrown when an element is not enabled.</exception>
/// <exception cref="ElementNotVisibleException">Thrown when an element is not visible.</exception>
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
public virtual void SelectItem(string name)
{
this.Click();
var locator =
new ByAndroidUIAutomator(
$"new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().text(\"{name}\"));");
AndroidElement item = this.Driver.FindElement(locator);
item.Click();
}
/// <summary>
/// Selects an item in the combo-box with the specified partial item name.
/// </summary>
/// <param name="partialName">The partial name match for the item to select.</param>
/// <exception cref="InvalidElementStateException">Thrown when an element is not enabled.</exception>
/// <exception cref="ElementNotVisibleException">Thrown when an element is not visible.</exception>
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
public virtual void SelectItemByPartialName(string partialName)
{
this.Click();
var locator =
new ByAndroidUIAutomator(
$"new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().textContains(\"{partialName}\"));");
AndroidElement item = this.Driver.FindElement(locator);
item.Click();
}
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
private string GetSelectedItem()
{
TextView textElement = this.FindElement(By.ClassName("android.widget.TextView"));
return textElement.Text;
}
}