|
| 1 | +using OpenQA.Selenium; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Collections.ObjectModel; |
| 5 | +using System.Drawing; |
| 6 | +using Selenium.WebDriver.SimpleExtensions; |
| 7 | +using System.Linq; |
| 8 | +using System.Xml.Linq; |
| 9 | + |
| 10 | +namespace Selenium.WebDriver.Linq |
| 11 | +{ |
| 12 | + public class LinqWebElement : WebElement |
| 13 | + { |
| 14 | + private readonly IWebElement _webElement; |
| 15 | + |
| 16 | + public LinqWebElement(IWebElement webElement,OpenQA.Selenium.WebDriver parentDriver, string id) : base(parentDriver, id) |
| 17 | + { |
| 18 | + _webElement = webElement; |
| 19 | + } |
| 20 | + |
| 21 | + public string Id |
| 22 | + { |
| 23 | + get { return _webElement.GetAttribute("id"); } |
| 24 | + set |
| 25 | + { |
| 26 | + IJavaScriptExecutor js = (IJavaScriptExecutor)base.WrappedDriver; |
| 27 | + js.ExecuteScript($"arguments[0].setAttribute('id', '{value}')", _webElement); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public string Class |
| 32 | + { |
| 33 | + get { return _webElement.GetAttribute("class"); } |
| 34 | + set |
| 35 | + { |
| 36 | + IJavaScriptExecutor js = (IJavaScriptExecutor)base.WrappedDriver; |
| 37 | + js.ExecuteScript($"arguments[0].setAttribute('class', '{value}')", _webElement); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public string Style |
| 42 | + { |
| 43 | + get { return _webElement.GetAttribute("style"); } |
| 44 | + set |
| 45 | + { |
| 46 | + IJavaScriptExecutor js = (IJavaScriptExecutor)base.WrappedDriver; |
| 47 | + js.ExecuteScript($"arguments[0].setAttribute('style', '{value}')", _webElement); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public LinqWebElement Parent => _webElement.GetParent().AsLinqWebElement(); |
| 52 | + |
| 53 | + public IEnumerable<LinqWebElement> Children => _webElement.GetChildren().Select(s => s.AsLinqWebElement()); |
| 54 | + |
| 55 | + public IEnumerable<LinqWebElement> Siblings => _webElement.GetSiblings().Select(s => s.AsLinqWebElement()); |
| 56 | + |
| 57 | + public void Delete() |
| 58 | + { |
| 59 | + _webElement.Delete(); |
| 60 | + } |
| 61 | + |
| 62 | + public void SetAttribute(string attribute, string value) |
| 63 | + { |
| 64 | + _webElement.SetAttribute(attribute, value); |
| 65 | + } |
| 66 | + |
| 67 | + public bool HasAttribute(string attribute) |
| 68 | + { |
| 69 | + if (string.IsNullOrEmpty(attribute)) |
| 70 | + return false; |
| 71 | + |
| 72 | + return Attributes.Any(a=>a.Key == attribute.ToLower()); |
| 73 | + } |
| 74 | + |
| 75 | + public void SetText(string text) |
| 76 | + { |
| 77 | + _webElement.SetText(text); |
| 78 | + } |
| 79 | + |
| 80 | + public void AppendHtml(string html) |
| 81 | + { |
| 82 | + _webElement.AppendHtml(html); |
| 83 | + } |
| 84 | + |
| 85 | + public Dictionary<string,string> Attributes |
| 86 | + { |
| 87 | + get |
| 88 | + { |
| 89 | + var html = _webElement.GetAttribute("outerHTML"); |
| 90 | + return HtmlElementHelper.GetAttributes(html); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments