Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.87 KB | None | 0 0
  1. class FreshWebElement : IWebElement
  2.     {
  3.         private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  4.  
  5.         private IWebElement Element;
  6.         private IWebDriver Driver;
  7.         private By FoundBy;
  8.  
  9.         public string TagName
  10.         {
  11.             get
  12.             {
  13.                 try
  14.                 {
  15.                     return Element.TagName;
  16.                 }
  17.                 catch (StaleElementReferenceException E)
  18.                 {
  19.                     Log.Debug(E.Message, E);
  20.                     Element = Driver.FindElement(FoundBy);
  21.                     return TagName;
  22.                 }
  23.             }
  24.         }
  25.  
  26.         public string Text
  27.         {
  28.             get
  29.             {
  30.                 try
  31.                 {
  32.                     return Element.Text;
  33.                 }
  34.                 catch (StaleElementReferenceException E)
  35.                 {
  36.                     Log.Debug(E.Message, E);
  37.                     Element = Driver.FindElement(FoundBy);
  38.                     return Text;
  39.                 }
  40.             }
  41.         }
  42.  
  43.         public bool Enabled
  44.         {
  45.             get
  46.             {
  47.                 try
  48.                 {
  49.                     return Element.Enabled;
  50.                 }
  51.                 catch (StaleElementReferenceException E)
  52.                 {
  53.                     Log.Debug(E.Message, E);
  54.                     Element = Driver.FindElement(FoundBy);
  55.                     return Enabled;
  56.                 }
  57.             }
  58.         }
  59.  
  60.         public bool Selected
  61.         {
  62.             get
  63.             {
  64.                 try
  65.                 {
  66.                     return Element.Selected;
  67.                 }
  68.                 catch (StaleElementReferenceException E)
  69.                 {
  70.                     Log.Debug(E.Message, E);
  71.                     Element = Driver.FindElement(FoundBy);
  72.                     return Selected;
  73.                 }
  74.             }
  75.         }
  76.  
  77.         public Point Location
  78.         {
  79.             get
  80.             {
  81.                 try
  82.                 {
  83.                     return Element.Location;
  84.                 }
  85.                 catch (StaleElementReferenceException E)
  86.                 {
  87.                     Log.Debug(E.Message, E);
  88.                     Element = Driver.FindElement(FoundBy);
  89.                     return Location;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         public Size Size
  95.         {
  96.             get
  97.             {
  98.                 try
  99.                 {
  100.                     return Element.Size;
  101.                 }
  102.                 catch (StaleElementReferenceException E)
  103.                 {
  104.                     Log.Debug(E.Message, E);
  105.                     Element = Driver.FindElement(FoundBy);
  106.                     return Size;
  107.                 }
  108.             }
  109.         }
  110.  
  111.         public bool Displayed
  112.         {
  113.             get
  114.             {
  115.                 try
  116.                 {
  117.                     return Element.Displayed;
  118.                 }
  119.                 catch (StaleElementReferenceException E)
  120.                 {
  121.                     Log.Debug(E.Message, E);
  122.                     Element = Driver.FindElement(FoundBy);
  123.                     return Displayed;
  124.                 }
  125.             }
  126.         }
  127.  
  128.         public FreshWebElement(IWebElement Element, IWebDriver Driver, By FoundBy)
  129.         {
  130.             this.Element = Element;
  131.             this.Driver = Driver;
  132.             this.FoundBy = FoundBy;
  133.         }
  134.  
  135.         public FreshWebElement(IWebDriver Driver, By FoundBy)
  136.         {
  137.             this.Driver = Driver;
  138.             this.FoundBy = FoundBy;
  139.  
  140.             this.Element = this.Driver.FindElement(this.FoundBy);
  141.         }
  142.  
  143.         public void SetSearch(IWebDriver Driver, By FoundBy)
  144.         {
  145.             this.Driver = Driver;
  146.             this.FoundBy = FoundBy;
  147.         }
  148.  
  149.         public void Clear()
  150.         {
  151.             try
  152.             {
  153.                 Element.Clear();
  154.             }
  155.             catch (StaleElementReferenceException E)
  156.             {
  157.                 Log.Debug(E.Message, E);
  158.                 Element = Driver.FindElement(FoundBy);
  159.                 Clear();
  160.             }
  161.         }
  162.  
  163.         public void SendKeys(string text)
  164.         {
  165.             try
  166.             {
  167.                 Element.SendKeys(text);
  168.             }
  169.             catch (StaleElementReferenceException E)
  170.             {
  171.                 Log.Debug(E.Message, E);
  172.                 Element = Driver.FindElement(FoundBy);
  173.                 SendKeys(text);
  174.             }
  175.         }
  176.  
  177.         public void Submit()
  178.         {
  179.             try
  180.             {
  181.                 Element.Submit();
  182.             }
  183.             catch (StaleElementReferenceException E)
  184.             {
  185.                 Log.Debug(E.Message, E);
  186.                 Element = Driver.FindElement(FoundBy);
  187.                 Submit();
  188.             }
  189.         }
  190.  
  191.         public string GetAttribute(string attributeName)
  192.         {
  193.             try
  194.             {
  195.                 return Element.GetAttribute(attributeName);
  196.             }
  197.             catch (StaleElementReferenceException E)
  198.             {
  199.                 Log.Debug(E.Message, E);
  200.                 Element = Driver.FindElement(FoundBy);
  201.                 return GetAttribute(attributeName);
  202.             }
  203.         }
  204.  
  205.         public string GetCssValue(string propertyName)
  206.         {
  207.             try
  208.             {
  209.                 return Element.GetCssValue(propertyName);
  210.             }
  211.             catch (StaleElementReferenceException E)
  212.             {
  213.                 Log.Debug(E.Message, E);
  214.                 Element = Driver.FindElement(FoundBy);
  215.                 return GetCssValue(propertyName);
  216.             }
  217.         }
  218.  
  219.         public IWebElement FindElement(By by)
  220.         {
  221.             try
  222.             {
  223.                 return Element.FindElement(by);
  224.             }
  225.             catch (StaleElementReferenceException E)
  226.             {
  227.                 Log.Debug(E.Message, E);
  228.                 Element = Driver.FindElement(FoundBy);
  229.                 return FindElement(by);
  230.             }
  231.         }
  232.  
  233.         public ReadOnlyCollection<IWebElement> FindElements(By by)
  234.         {
  235.             try
  236.             {
  237.                 return Element.FindElements(by);
  238.             }
  239.             catch (StaleElementReferenceException E)
  240.             {
  241.                 Log.Debug(E.Message, E);
  242.                 Element = Driver.FindElement(FoundBy);
  243.                 return FindElements(by);
  244.             }
  245.         }
  246.  
  247.         public void Click()
  248.         {
  249.             try
  250.             {
  251.                 Element.Click();
  252.             }
  253.             catch (StaleElementReferenceException E)
  254.             {
  255.                 Log.Debug(E.Message, E);
  256.                 Element = Driver.FindElement(FoundBy);
  257.                 Click();
  258.             }
  259.         }
  260.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement