yashaka

Untitled

May 28th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.19 KB | None | 0 0
  1. using OpenQA.Selenium.Internal;
  2. using System;
  3. using System.Collections.ObjectModel;
  4.  
  5. namespace OpenQA.Selenium
  6. {
  7.     [Serializable]
  8.     public class By
  9.     {
  10.         //
  11.         // Fields
  12.         //
  13.         private string description = "OpenQA.Selenium.By";
  14.  
  15.         private Func<ISearchContext, IWebElement> findElementMethod;
  16.  
  17.         private Func<ISearchContext, ReadOnlyCollection<IWebElement>> findElementsMethod;
  18.  
  19.         //
  20.         // Properties
  21.         //
  22.         protected string Description {
  23.             get {
  24.                 return this.description;
  25.             }
  26.             set {
  27.                 this.description = value;
  28.             }
  29.         }
  30.  
  31.         protected Func<ISearchContext, IWebElement> FindElementMethod {
  32.             get {
  33.                 return this.findElementMethod;
  34.             }
  35.             set {
  36.                 this.findElementMethod = value;
  37.             }
  38.         }
  39.  
  40.         protected Func<ISearchContext, ReadOnlyCollection<IWebElement>> FindElementsMethod {
  41.             get {
  42.                 return this.findElementsMethod;
  43.             }
  44.             set {
  45.                 this.findElementsMethod = value;
  46.             }
  47.         }
  48.  
  49.         //
  50.         // Constructors
  51.         //
  52.         protected By ()
  53.         {
  54.         }
  55.  
  56.         protected By (Func<ISearchContext, IWebElement> findElementMethod, Func<ISearchContext, ReadOnlyCollection<IWebElement>> findElementsMethod)
  57.         {
  58.             this.findElementMethod = findElementMethod;
  59.             this.findElementsMethod = findElementsMethod;
  60.         }
  61.  
  62.         //
  63.         // Static Methods
  64.         //
  65.         public static By ClassName (string classNameToFind)
  66.         {
  67.             if (classNameToFind == null) {
  68.                 throw new ArgumentNullException ("classNameToFind", "Cannot find elements when the class name expression is null.");
  69.             }
  70.             return new By {
  71.                 findElementMethod = ((ISearchContext context) => ((IFindsByClassName)context).FindElementByClassName (classNameToFind)),
  72.                 findElementsMethod = ((ISearchContext context) => ((IFindsByClassName)context).FindElementsByClassName (classNameToFind)),
  73.                 description = "By.ClassName[Contains]: " + classNameToFind
  74.             };
  75.         }
  76.  
  77.         public static By CssSelector (string cssSelectorToFind)
  78.         {
  79.             if (cssSelectorToFind == null) {
  80.                 throw new ArgumentNullException ("cssSelectorToFind", "Cannot find elements when name CSS selector is null.");
  81.             }
  82.             return new By {
  83.                 findElementMethod = ((ISearchContext context) => ((IFindsByCssSelector)context).FindElementByCssSelector (cssSelectorToFind)),
  84.                 findElementsMethod = ((ISearchContext context) => ((IFindsByCssSelector)context).FindElementsByCssSelector (cssSelectorToFind)),
  85.                 description = "By.CssSelector: " + cssSelectorToFind
  86.             };
  87.         }
  88.  
  89.         public static By Id (string idToFind)
  90.         {
  91.             if (idToFind == null) {
  92.                 throw new ArgumentNullException ("idToFind", "Cannot find elements with a null id attribute.");
  93.             }
  94.             return new By {
  95.                 findElementMethod = ((ISearchContext context) => ((IFindsById)context).FindElementById (idToFind)),
  96.                 findElementsMethod = ((ISearchContext context) => ((IFindsById)context).FindElementsById (idToFind)),
  97.                 description = "By.Id: " + idToFind
  98.             };
  99.         }
  100.  
  101.         public static By LinkText (string linkTextToFind)
  102.         {
  103.             if (linkTextToFind == null) {
  104.                 throw new ArgumentNullException ("linkTextToFind", "Cannot find elements when link text is null.");
  105.             }
  106.             return new By {
  107.                 findElementMethod = ((ISearchContext context) => ((IFindsByLinkText)context).FindElementByLinkText (linkTextToFind)),
  108.                 findElementsMethod = ((ISearchContext context) => ((IFindsByLinkText)context).FindElementsByLinkText (linkTextToFind)),
  109.                 description = "By.LinkText: " + linkTextToFind
  110.             };
  111.         }
  112.  
  113.         public static By Name (string nameToFind)
  114.         {
  115.             if (nameToFind == null) {
  116.                 throw new ArgumentNullException ("nameToFind", "Cannot find elements when name text is null.");
  117.             }
  118.             return new By {
  119.                 findElementMethod = ((ISearchContext context) => ((IFindsByName)context).FindElementByName (nameToFind)),
  120.                 findElementsMethod = ((ISearchContext context) => ((IFindsByName)context).FindElementsByName (nameToFind)),
  121.                 description = "By.Name: " + nameToFind
  122.             };
  123.         }
  124.  
  125.         public static By PartialLinkText (string partialLinkTextToFind)
  126.         {
  127.             return new By {
  128.                 findElementMethod = ((ISearchContext context) => ((IFindsByPartialLinkText)context).FindElementByPartialLinkText (partialLinkTextToFind)),
  129.                 findElementsMethod = ((ISearchContext context) => ((IFindsByPartialLinkText)context).FindElementsByPartialLinkText (partialLinkTextToFind)),
  130.                 description = "By.PartialLinkText: " + partialLinkTextToFind
  131.             };
  132.         }
  133.  
  134.         public static By TagName (string tagNameToFind)
  135.         {
  136.             if (tagNameToFind == null) {
  137.                 throw new ArgumentNullException ("tagNameToFind", "Cannot find elements when name tag name is null.");
  138.             }
  139.             return new By {
  140.                 findElementMethod = ((ISearchContext context) => ((IFindsByTagName)context).FindElementByTagName (tagNameToFind)),
  141.                 findElementsMethod = ((ISearchContext context) => ((IFindsByTagName)context).FindElementsByTagName (tagNameToFind)),
  142.                 description = "By.TagName: " + tagNameToFind
  143.             };
  144.         }
  145.  
  146.         public static By XPath (string xpathToFind)
  147.         {
  148.             if (xpathToFind == null) {
  149.                 throw new ArgumentNullException ("xpathToFind", "Cannot find elements when the XPath expression is null.");
  150.             }
  151.             return new By {
  152.                 findElementMethod = ((ISearchContext context) => ((IFindsByXPath)context).FindElementByXPath (xpathToFind)),
  153.                 findElementsMethod = ((ISearchContext context) => ((IFindsByXPath)context).FindElementsByXPath (xpathToFind)),
  154.                 description = "By.XPath: " + xpathToFind
  155.             };
  156.         }
  157.  
  158.         //
  159.         // Methods
  160.         //
  161.         public override bool Equals (object obj)
  162.         {
  163.             By by = obj as By;
  164.             return by != null && this.description.Equals (by.description);
  165.         }
  166.  
  167.         public virtual IWebElement FindElement (ISearchContext context)
  168.         {
  169.             return this.findElementMethod.Invoke (context);
  170.         }
  171.  
  172.         public virtual ReadOnlyCollection<IWebElement> FindElements (ISearchContext context)
  173.         {
  174.             return this.findElementsMethod.Invoke (context);
  175.         }
  176.  
  177.         public override int GetHashCode ()
  178.         {
  179.             return this.description.GetHashCode ();
  180.         }
  181.  
  182.         public override string ToString ()
  183.         {
  184.             return this.description;
  185.         }
  186.  
  187.         //
  188.         // Operators
  189.         //
  190.         public static bool operator == (By one, By two) {
  191.             return object.ReferenceEquals (one, two) || (one != null && two != null && one.Equals (two));
  192.         }
  193.  
  194.         public static bool operator != (By one, By two) {
  195.             return !(one == two);
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment