Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using Nexus.UITestFramework.WrappedSets.Operations;
- using System.Text;
- using OpenQA.Selenium.Support.UI;
- namespace Nexus.UITestFramework {
- public class WrappedSet {
- private ILocator m_Locator;
- internal ILocator Locator { get { return m_Locator; } }
- internal WrappedSet(ILocator locator) {
- m_Locator = locator;
- }
- /// <summary>
- /// Reduce the set of matched elements to the one at the specified index.
- /// </summary>
- public WrappedSet Eq(int index) {
- return new WrappedSet(new EqLocator(m_Locator, index));
- }
- /// <summary>
- /// Iterates over all elements in the WrappedSet.
- /// </summary>
- /// <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>
- public void Each(Action<int, WrappedSet> iterator) {
- int count = m_Locator.Elements.Count();
- for(int index=0; index < count; index++) {
- iterator(index, Eq(index));
- }
- }
- /// <summary>
- /// Returns child elements of elements in this WrappedSet that match the selector.
- /// </summary>
- public WrappedSet Find(string selector) {
- return new WrappedSet(new CascadingByLocator(m_Locator, selector.GetBy()));
- }
- /// <summary>
- /// Only applies to select tags; sets the selected option, throws
- /// </summary>
- public void SelectedOptionText(string text) {
- Execute(new SetSelectedOptionText(text));
- }
- /// <summary>
- /// returns the text of the first matched element
- /// </summary>
- public string SelectedOptionText() {
- var first = m_Locator.Elements.FirstOrDefault();
- return first == null ? null : new SelectElement(first).SelectedOption.Text;
- }
- /// <summary>
- /// checks the first matched element select to see if it contains the text
- /// </summary>
- public bool IsOptionAvailable(string text) {
- var first = m_Locator.Elements.First();
- if (first.TagName.ToLower() != "select") {
- throw new Exception("<select> element expected.");
- }
- var option = new SelectElement(first).Options.FirstOrDefault(r => r.Text == text);
- return option == null ? false : true;
- }
- /// <summary>
- /// Call Click() on each item in the wrapped set.
- /// </summary>
- public void Click() {
- Execute(new Click());
- }
- /// <summary>
- /// Set the "value" of the matched elements.
- /// </summary>
- public void Val(string value) {
- Execute(new SetValue(value));
- }
- /// <summary>
- /// Gets the "value" of the first matched element.
- /// </summary>
- public string Val() {
- var first = m_Locator.Elements.FirstOrDefault();
- return first == null ? null : first.GetAttribute("value");
- }
- /// <summary>
- /// Gets the inner text of the element(s).
- /// </summary>
- public string Text() {
- var text = new StringBuilder();
- foreach(var element in m_Locator.Elements) {
- text.Append(element.Text);
- }
- return text.ToString();
- }
- /// <summary>
- /// Gets the value of the attribute passed in of the first matched element
- /// </summary>
- public string GetAttribute(string property) {
- var first = m_Locator.Elements.FirstOrDefault();
- return first == null ? null : first.GetAttribute(property);
- }
- /// <summary>
- /// Checks if the first element is displayed
- /// </summary>
- public bool IsDisplayed() {
- var first = m_Locator.Elements.FirstOrDefault();
- return first == null ? false : first.Displayed;
- }
- /// <summary>
- /// Sets each item to checked or not checked based on passed in boolean
- /// </summary>
- public void Checked(bool isChecked) {
- Execute(new SetCheckbox(isChecked));
- }
- /// <summary>
- /// checks if the first element checkbox is checked or not
- /// </summary>
- public bool Checked() {
- var first = m_Locator.Elements.FirstOrDefault();
- return first == null ? false : first.Selected;
- }
- private void Execute(ICommand command) {
- foreach (var element in m_Locator.Elements) {
- command.ExecuteOn(element);
- }
- }
- /// <summary>
- /// overrides the ContectClick to accept a WrappedSet and perform a left click on the first element
- /// </summary>
- //public Actions ContextClick(this Actions actions, WrappedSet wrappedSet) {
- // return actions.ContextClick(wrappedSet.m_Locator.Elements.First());
- //}
- }
- }
- using System;
- using System.Linq;
- using OpenQA.Selenium;
- using System.Text.RegularExpressions;
- using OpenQA.Selenium.Interactions;
- namespace Nexus.UITestFramework {
- public static class WebDriverExtensions {
- public static WrappedSet Find(this IWebDriver driver, string selector) {
- return new WrappedSet(new DriverRootedLocator(driver, selector.GetBy()));
- }
- internal static By GetBy(this string selector) {
- if (Regex.IsMatch(selector, @"#[a-zA-Z0-9-]+")) {
- return By.Id(selector.Substring(1));
- }
- if (selector.StartsWith("/") || selector.StartsWith("./")) {
- return By.XPath(selector);
- }
- if (Regex.IsMatch(selector, @"\A\.[a-zA-Z0-9-]+")) {
- return By.ClassName(selector.Substring(1));
- }
- if (selector.StartsWith("_")) {
- return By.LinkText(selector.Substring(1));
- }
- return By.CssSelector(selector);
- }
- /// <summary>
- /// overrides the ContectClick to accept a WrappedSet and perform a left click on the first element
- /// </summary>
- public static Actions ContextClick(this Actions actions, WrappedSet wrappedSet) {
- return actions.ContextClick(wrappedSet.Locator.Elements.First());
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using OpenQA.Selenium;
- namespace Nexus.UITestFramework {
- internal interface ILocator {
- IEnumerable<IWebElement> Elements { get; }
- }
- }
- using System;
- using System.Collections.Generic;
- using OpenQA.Selenium;
- namespace Nexus.UITestFramework {
- internal class DriverRootedLocator : ILocator {
- private IWebDriver m_Driver;
- private By m_By;
- public DriverRootedLocator(IWebDriver driver, By by) {
- m_Driver = driver;
- m_By = by;
- }
- public IEnumerable<IWebElement> Elements {
- get { return m_Driver.FindElements(m_By); }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Interactions;
- namespace Nexus.UITestFramework {
- internal class CascadingByLocator : ILocator {
- private By m_ChildSelector;
- private ILocator m_Parent;
- public CascadingByLocator(ILocator parent, By childSelector) {
- m_Parent = parent;
- m_ChildSelector = childSelector;
- }
- public IEnumerable<IWebElement> Elements {
- get {
- var result = new List<IWebElement>();
- foreach (var parent in m_Parent.Elements) {
- var children = parent.FindElements(m_ChildSelector);
- result.AddRange(children);
- }
- return result;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment