Guest User

Untitled

a guest
Aug 17th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.53 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using Nexus.UITestFramework.WrappedSets.Operations;
  4. using System.Text;
  5. using OpenQA.Selenium.Support.UI;
  6.  
  7. namespace Nexus.UITestFramework {
  8.  
  9. public class WrappedSet {
  10.  
  11. private ILocator m_Locator;
  12.  
  13. internal ILocator Locator { get { return m_Locator; } }
  14.  
  15. internal WrappedSet(ILocator locator) {
  16. m_Locator = locator;
  17. }
  18.  
  19. /// <summary>
  20. /// Reduce the set of matched elements to the one at the specified index.
  21. /// </summary>
  22. public WrappedSet Eq(int index) {
  23. return new WrappedSet(new EqLocator(m_Locator, index));
  24. }
  25.  
  26. /// <summary>
  27. /// Iterates over all elements in the WrappedSet.
  28. /// </summary>
  29. /// <param name="iterator">A function that takes the index of the item in the set and the item in the set at that index.</param>
  30. public void Each(Action<int, WrappedSet> iterator) {
  31. int count = m_Locator.Elements.Count();
  32. for(int index=0; index < count; index++) {
  33. iterator(index, Eq(index));
  34. }
  35. }
  36.  
  37. /// <summary>
  38. /// Returns child elements of elements in this WrappedSet that match the selector.
  39. /// </summary>
  40. public WrappedSet Find(string selector) {
  41. return new WrappedSet(new CascadingByLocator(m_Locator, selector.GetBy()));
  42. }
  43.  
  44. /// <summary>
  45. /// Only applies to select tags; sets the selected option, throws
  46. /// </summary>
  47. public void SelectedOptionText(string text) {
  48. Execute(new SetSelectedOptionText(text));
  49. }
  50.  
  51. /// <summary>
  52. /// returns the text of the first matched element
  53. /// </summary>
  54. public string SelectedOptionText() {
  55. var first = m_Locator.Elements.FirstOrDefault();
  56. return first == null ? null : new SelectElement(first).SelectedOption.Text;
  57. }
  58.  
  59. /// <summary>
  60. /// checks the first matched element select to see if it contains the text
  61. /// </summary>
  62. public bool IsOptionAvailable(string text) {
  63. var first = m_Locator.Elements.First();
  64. if (first.TagName.ToLower() != "select") {
  65. throw new Exception("<select> element expected.");
  66. }
  67. var option = new SelectElement(first).Options.FirstOrDefault(r => r.Text == text);
  68. return option == null ? false : true;
  69. }
  70.  
  71. /// <summary>
  72. /// Call Click() on each item in the wrapped set.
  73. /// </summary>
  74. public void Click() {
  75. Execute(new Click());
  76. }
  77.  
  78. /// <summary>
  79. /// Set the "value" of the matched elements.
  80. /// </summary>
  81. public void Val(string value) {
  82. Execute(new SetValue(value));
  83. }
  84.  
  85. /// <summary>
  86. /// Gets the "value" of the first matched element.
  87. /// </summary>
  88. public string Val() {
  89. var first = m_Locator.Elements.FirstOrDefault();
  90. return first == null ? null : first.GetAttribute("value");
  91. }
  92.  
  93. /// <summary>
  94. /// Gets the inner text of the element(s).
  95. /// </summary>
  96. public string Text() {
  97. var text = new StringBuilder();
  98. foreach(var element in m_Locator.Elements) {
  99. text.Append(element.Text);
  100. }
  101. return text.ToString();
  102. }
  103.  
  104. /// <summary>
  105. /// Gets the value of the attribute passed in of the first matched element
  106. /// </summary>
  107. public string GetAttribute(string property) {
  108. var first = m_Locator.Elements.FirstOrDefault();
  109. return first == null ? null : first.GetAttribute(property);
  110. }
  111.  
  112. /// <summary>
  113. /// Checks if the first element is displayed
  114. /// </summary>
  115. public bool IsDisplayed() {
  116. var first = m_Locator.Elements.FirstOrDefault();
  117. return first == null ? false : first.Displayed;
  118. }
  119.  
  120. /// <summary>
  121. /// Sets each item to checked or not checked based on passed in boolean
  122. /// </summary>
  123. public void Checked(bool isChecked) {
  124. Execute(new SetCheckbox(isChecked));
  125. }
  126.  
  127. /// <summary>
  128. /// checks if the first element checkbox is checked or not
  129. /// </summary>
  130. public bool Checked() {
  131. var first = m_Locator.Elements.FirstOrDefault();
  132. return first == null ? false : first.Selected;
  133. }
  134.  
  135.  
  136. private void Execute(ICommand command) {
  137. foreach (var element in m_Locator.Elements) {
  138. command.ExecuteOn(element);
  139. }
  140. }
  141.  
  142. /// <summary>
  143. /// overrides the ContectClick to accept a WrappedSet and perform a left click on the first element
  144. /// </summary>
  145. //public Actions ContextClick(this Actions actions, WrappedSet wrappedSet) {
  146. // return actions.ContextClick(wrappedSet.m_Locator.Elements.First());
  147. //}
  148. }
  149.  
  150. }
  151.  
  152.  
  153. using System;
  154. using System.Linq;
  155. using OpenQA.Selenium;
  156. using System.Text.RegularExpressions;
  157. using OpenQA.Selenium.Interactions;
  158.  
  159. namespace Nexus.UITestFramework {
  160.  
  161. public static class WebDriverExtensions {
  162.  
  163. public static WrappedSet Find(this IWebDriver driver, string selector) {
  164. return new WrappedSet(new DriverRootedLocator(driver, selector.GetBy()));
  165. }
  166.  
  167. internal static By GetBy(this string selector) {
  168. if (Regex.IsMatch(selector, @"#[a-zA-Z0-9-]+")) {
  169. return By.Id(selector.Substring(1));
  170. }
  171. if (selector.StartsWith("/") || selector.StartsWith("./")) {
  172. return By.XPath(selector);
  173. }
  174. if (Regex.IsMatch(selector, @"\A\.[a-zA-Z0-9-]+")) {
  175. return By.ClassName(selector.Substring(1));
  176. }
  177. if (selector.StartsWith("_")) {
  178. return By.LinkText(selector.Substring(1));
  179. }
  180. return By.CssSelector(selector);
  181. }
  182.  
  183. /// <summary>
  184. /// overrides the ContectClick to accept a WrappedSet and perform a left click on the first element
  185. /// </summary>
  186. public static Actions ContextClick(this Actions actions, WrappedSet wrappedSet) {
  187. return actions.ContextClick(wrappedSet.Locator.Elements.First());
  188. }
  189. }
  190. }
  191.  
  192.  
  193. using System;
  194. using System.Collections.Generic;
  195. using OpenQA.Selenium;
  196.  
  197. namespace Nexus.UITestFramework {
  198.  
  199. internal interface ILocator {
  200.  
  201. IEnumerable<IWebElement> Elements { get; }
  202.  
  203. }
  204. }
  205.  
  206.  
  207. using System;
  208. using System.Collections.Generic;
  209. using OpenQA.Selenium;
  210.  
  211. namespace Nexus.UITestFramework {
  212.  
  213. internal class DriverRootedLocator : ILocator {
  214.  
  215. private IWebDriver m_Driver;
  216. private By m_By;
  217.  
  218. public DriverRootedLocator(IWebDriver driver, By by) {
  219. m_Driver = driver;
  220. m_By = by;
  221. }
  222.  
  223. public IEnumerable<IWebElement> Elements {
  224. get { return m_Driver.FindElements(m_By); }
  225. }
  226.  
  227. }
  228.  
  229. }
  230.  
  231.  
  232. using System;
  233. using System.Collections.Generic;
  234. using OpenQA.Selenium;
  235. using OpenQA.Selenium.Interactions;
  236.  
  237. namespace Nexus.UITestFramework {
  238.  
  239. internal class CascadingByLocator : ILocator {
  240.  
  241. private By m_ChildSelector;
  242. private ILocator m_Parent;
  243.  
  244. public CascadingByLocator(ILocator parent, By childSelector) {
  245. m_Parent = parent;
  246. m_ChildSelector = childSelector;
  247. }
  248.  
  249. public IEnumerable<IWebElement> Elements {
  250. get {
  251. var result = new List<IWebElement>();
  252. foreach (var parent in m_Parent.Elements) {
  253. var children = parent.FindElements(m_ChildSelector);
  254. result.AddRange(children);
  255. }
  256. return result;
  257. }
  258. }
  259.  
  260. }
  261.  
  262. }
Add Comment
Please, Sign In to add comment