Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using OpenQA.Selenium.Internal;
- using System;
- using System.Collections.ObjectModel;
- namespace OpenQA.Selenium
- {
- [Serializable]
- public class By
- {
- //
- // Fields
- //
- private string description = "OpenQA.Selenium.By";
- private Func<ISearchContext, IWebElement> findElementMethod;
- private Func<ISearchContext, ReadOnlyCollection<IWebElement>> findElementsMethod;
- //
- // Properties
- //
- protected string Description {
- get {
- return this.description;
- }
- set {
- this.description = value;
- }
- }
- protected Func<ISearchContext, IWebElement> FindElementMethod {
- get {
- return this.findElementMethod;
- }
- set {
- this.findElementMethod = value;
- }
- }
- protected Func<ISearchContext, ReadOnlyCollection<IWebElement>> FindElementsMethod {
- get {
- return this.findElementsMethod;
- }
- set {
- this.findElementsMethod = value;
- }
- }
- //
- // Constructors
- //
- protected By ()
- {
- }
- protected By (Func<ISearchContext, IWebElement> findElementMethod, Func<ISearchContext, ReadOnlyCollection<IWebElement>> findElementsMethod)
- {
- this.findElementMethod = findElementMethod;
- this.findElementsMethod = findElementsMethod;
- }
- //
- // Static Methods
- //
- public static By ClassName (string classNameToFind)
- {
- if (classNameToFind == null) {
- throw new ArgumentNullException ("classNameToFind", "Cannot find elements when the class name expression is null.");
- }
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsByClassName)context).FindElementByClassName (classNameToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsByClassName)context).FindElementsByClassName (classNameToFind)),
- description = "By.ClassName[Contains]: " + classNameToFind
- };
- }
- public static By CssSelector (string cssSelectorToFind)
- {
- if (cssSelectorToFind == null) {
- throw new ArgumentNullException ("cssSelectorToFind", "Cannot find elements when name CSS selector is null.");
- }
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsByCssSelector)context).FindElementByCssSelector (cssSelectorToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsByCssSelector)context).FindElementsByCssSelector (cssSelectorToFind)),
- description = "By.CssSelector: " + cssSelectorToFind
- };
- }
- public static By Id (string idToFind)
- {
- if (idToFind == null) {
- throw new ArgumentNullException ("idToFind", "Cannot find elements with a null id attribute.");
- }
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsById)context).FindElementById (idToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsById)context).FindElementsById (idToFind)),
- description = "By.Id: " + idToFind
- };
- }
- public static By LinkText (string linkTextToFind)
- {
- if (linkTextToFind == null) {
- throw new ArgumentNullException ("linkTextToFind", "Cannot find elements when link text is null.");
- }
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsByLinkText)context).FindElementByLinkText (linkTextToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsByLinkText)context).FindElementsByLinkText (linkTextToFind)),
- description = "By.LinkText: " + linkTextToFind
- };
- }
- public static By Name (string nameToFind)
- {
- if (nameToFind == null) {
- throw new ArgumentNullException ("nameToFind", "Cannot find elements when name text is null.");
- }
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsByName)context).FindElementByName (nameToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsByName)context).FindElementsByName (nameToFind)),
- description = "By.Name: " + nameToFind
- };
- }
- public static By PartialLinkText (string partialLinkTextToFind)
- {
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsByPartialLinkText)context).FindElementByPartialLinkText (partialLinkTextToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsByPartialLinkText)context).FindElementsByPartialLinkText (partialLinkTextToFind)),
- description = "By.PartialLinkText: " + partialLinkTextToFind
- };
- }
- public static By TagName (string tagNameToFind)
- {
- if (tagNameToFind == null) {
- throw new ArgumentNullException ("tagNameToFind", "Cannot find elements when name tag name is null.");
- }
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsByTagName)context).FindElementByTagName (tagNameToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsByTagName)context).FindElementsByTagName (tagNameToFind)),
- description = "By.TagName: " + tagNameToFind
- };
- }
- public static By XPath (string xpathToFind)
- {
- if (xpathToFind == null) {
- throw new ArgumentNullException ("xpathToFind", "Cannot find elements when the XPath expression is null.");
- }
- return new By {
- findElementMethod = ((ISearchContext context) => ((IFindsByXPath)context).FindElementByXPath (xpathToFind)),
- findElementsMethod = ((ISearchContext context) => ((IFindsByXPath)context).FindElementsByXPath (xpathToFind)),
- description = "By.XPath: " + xpathToFind
- };
- }
- //
- // Methods
- //
- public override bool Equals (object obj)
- {
- By by = obj as By;
- return by != null && this.description.Equals (by.description);
- }
- public virtual IWebElement FindElement (ISearchContext context)
- {
- return this.findElementMethod.Invoke (context);
- }
- public virtual ReadOnlyCollection<IWebElement> FindElements (ISearchContext context)
- {
- return this.findElementsMethod.Invoke (context);
- }
- public override int GetHashCode ()
- {
- return this.description.GetHashCode ();
- }
- public override string ToString ()
- {
- return this.description;
- }
- //
- // Operators
- //
- public static bool operator == (By one, By two) {
- return object.ReferenceEquals (one, two) || (one != null && two != null && one.Equals (two));
- }
- public static bool operator != (By one, By two) {
- return !(one == two);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment