/
ComboBox.cs
151 lines (134 loc) · 6.05 KB
/
ComboBox.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
namespace Legerity.Windows.Elements.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using Legerity.Exceptions;
using Legerity.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
/// <summary>
/// Defines a <see cref="WindowsElement"/> wrapper for the core UWP ComboBox control.
/// </summary>
public class ComboBox : WindowsElementWrapper
{
private readonly By comboBoxItemLocator = By.ClassName("ComboBoxItem");
/// <summary>
/// Initializes a new instance of the <see cref="ComboBox"/> class.
/// </summary>
/// <param name="element">
/// The <see cref="WindowsElement"/> reference.
/// </param>
public ComboBox(WindowsElement element)
: base(element)
{
}
/// <summary>
/// Gets the currently selected item.
/// </summary>
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
public virtual string SelectedItem => this.GetSelectedItem();
/// <summary>
/// Allows conversion of a <see cref="WindowsElement"/> to the <see cref="ComboBox"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="WindowsElement"/>.
/// </param>
/// <returns>
/// The <see cref="ComboBox"/>.
/// </returns>
public static implicit operator ComboBox(WindowsElement element)
{
return new ComboBox(element);
}
/// <summary>
/// Allows conversion of a <see cref="AppiumWebElement"/> to the <see cref="ComboBox"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="AppiumWebElement"/>.
/// </param>
/// <returns>
/// The <see cref="ComboBox"/>.
/// </returns>
public static implicit operator ComboBox(AppiumWebElement element)
{
return new ComboBox(element as WindowsElement);
}
/// <summary>
/// Allows conversion of a <see cref="RemoteWebElement"/> to the <see cref="ComboBox"/> without direct casting.
/// </summary>
/// <param name="element">
/// The <see cref="RemoteWebElement"/>.
/// </param>
/// <returns>
/// The <see cref="ComboBox"/>.
/// </returns>
public static implicit operator ComboBox(RemoteWebElement element)
{
return new ComboBox(element as WindowsElement);
}
/// <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="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
/// <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="ElementsNotShownException">Thrown when no elements are shown for the expected locator.</exception>
public virtual void SelectItem(string name)
{
IEnumerable<AppiumWebElement> listElements = this.GetItemsToSelect();
AppiumWebElement item = listElements.FirstOrDefault(
element => element.GetName().Equals(name, StringComparison.CurrentCultureIgnoreCase));
if (item == null)
{
throw new NoSuchElementException($"Unable to find {name} in the combo box.");
}
item.Click();
}
/// <summary>
/// Selects an item in the combo-box with the specified partial item name.
/// </summary>
/// <param name="name">
/// The partial name of the item to select.
/// </param>
/// <exception cref="NoSuchElementException">Thrown when no element matches the expected locator.</exception>
/// <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="ElementsNotShownException">Thrown when no elements are shown for the expected locator.</exception>
public virtual void SelectItemByPartialName(string name)
{
IEnumerable<AppiumWebElement> listElements = this.GetItemsToSelect();
AppiumWebElement item = listElements.FirstOrDefault(
element => element.GetName().Contains(name, CultureInfo.CurrentCulture, CompareOptions.IgnoreCase));
if (item == null)
{
throw new NoSuchElementException($"Unable to find {name} in the combo box.");
}
item.Click();
}
/// <exception cref="StaleElementReferenceException">Thrown when an element is no longer valid in the document DOM.</exception>
private string GetSelectedItem()
{
ReadOnlyCollection<AppiumWebElement> listElements = this.Element.FindElements(this.comboBoxItemLocator);
return listElements.Count == 1 ? listElements.FirstOrDefault().GetName() : null;
}
/// <exception cref="ElementsNotShownException">Thrown when no elements are shown for the expected locator.</exception>
/// <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>
private IEnumerable<AppiumWebElement> GetItemsToSelect()
{
this.Click();
this.VerifyElementsShown(this.comboBoxItemLocator, TimeSpan.FromSeconds(2));
ReadOnlyCollection<AppiumWebElement> listElements = this.Element.FindElements(this.comboBoxItemLocator);
return listElements;
}
}