public void UnavoidableDuplicateTakesFirst() { // test file should be just two tags that look exactly the same Assert.That(_wb.Document != null, "_wb.Document == null"); HtmlDocument doc = _wb.Document.OpenNew(true); string html = File.ReadAllText(@"C:\Work\TestRecorder3\tests\html\UnavoidableDuplicates.html"); if (doc != null) { doc.Write(html); } HtmlElementCollection collection = GetInputElements(); int counter = collection.Cast <HtmlElement>().Count(element => element.GetAttribute("type") == "hidden" && element.GetAttribute("value") == "second"); Assert.AreEqual(2, counter, "Test not set up properly-- need two tag matches"); var hiddenElement = collection.Cast <HtmlElement>().FirstOrDefault(element => element.GetAttribute("type") == "hidden" && element.GetAttribute("value") == "second"); Assert.IsNotNull(hiddenElement, "hiddenElement == null"); var findCollection = new FindAttributeCollection((IHTMLElement)hiddenElement.DomElement); Assert.That(findCollection.AttributeList.Count == 2, "Different attribute count than expected"); Assert.IsNotNull(findCollection.AttributeList.Find(a => a.FindName == "type"), "does not contain type attribute"); Assert.IsNotNull(findCollection.AttributeList.Find(a => a.FindName == "value"), "does not contain value attribute"); }
internal static List <HtmlElement> getElementsByTagAndClassName(this HtmlDocument doc, string tag = "", string className = "") { List <HtmlElement> lst = new List <HtmlElement>(); bool empty_tag = String.IsNullOrEmpty(tag); bool empty_cn = String.IsNullOrEmpty(className); if (empty_tag && empty_cn) { return(lst); } HtmlElementCollection elmts = empty_tag ? doc.All : doc.GetElementsByTagName(tag); if (empty_cn) { lst.AddRange(elmts.Cast <HtmlElement>()); return(lst); } for (int i = 0; i < elmts.Count; i++) { if (elmts[i].GetAttribute("className") == className) { lst.Add(elmts[i]); } } return(lst); }
private void LoadCompleteEventHandler(object sender, WebBrowserDocumentCompletedEventArgs e) { HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("input"); foreach (HtmlElement input in elements) { if (input.GetAttribute("type").ToLower() == "text" || input.GetAttribute("type").ToLower() == "password") { input.GotFocus += (o, args) => VirtualKeyBoardHelper.AttachTabTip(); input.LostFocus += (o, args) => VirtualKeyBoardHelper.RemoveTabTip(); } } if (webBrowser1.Document.Title.Contains("Resultados del catálogo")) { HtmlElementCollection uls = webBrowser1.Document.GetElementsByTagName("form"); HtmlElementCollection links = null; HtmlElementCollection inputs = null; List <HtmlElement> inputsf = new List <HtmlElement>(); HtmlElement resultform = uls.Cast <HtmlElement>().Where(elem => elem.Id == "hitlist").ToList().FirstOrDefault(); if (resultform != null && resultform.Children.Count > 0) { links = resultform.GetElementsByTagName("a"); inputs = resultform.GetElementsByTagName("input"); } //foreach (HtmlElement i in inputs) // if (i.Id!=null && i.Id.Contains("VIEW")) // inputsf.Add(i); if (links != null && links.Count > 0) { foreach (HtmlElement a in links) { a.Click -= A_Click; a.Click += A_Click; } } } //foreach (HtmlElement ipf in inputsf) //{ // ipf.Click += A_Click; //} //if (inputs != null) //{ // var htmlElements = inputs as HtmlElement[] ?? inputs.ToArray(); // if(htmlElements.Any()) // foreach (HtmlElement i in htmlElements) // { // i.Click += A_Click; // } //} }
public void DetermineFinderForNoNameOrId() { HtmlElementCollection collection = GetInputElements(); // get the first hidden element var hiddenElement = collection.Cast <HtmlElement>().FirstOrDefault(element => element.GetAttribute("type") == "hidden"); Assert.IsNotNull(hiddenElement, "hiddenElement == null"); var findCollection = new FindAttributeCollection((IHTMLElement)hiddenElement.DomElement); Assert.AreEqual(2, findCollection.AttributeList.Count, "More attributes than expected"); Assert.AreEqual("value", findCollection.AttributeList[0].FindName, "Not found on value"); }
public void FindOnDuplicateName() { HtmlElementCollection collection = GetInputElements(); // get the first hidden element var radioElement = collection.Cast <HtmlElement>().FirstOrDefault(element => element.GetAttribute("type") == "radio"); Assert.IsNotNull(radioElement, "radioElement == null"); // set the pattern to exclude id var pattern = new List <string> { "name", "title", "href", "url", "src", "value", "style", "text" }; var findCollection = new FindAttributeCollection((IHTMLElement)radioElement.DomElement, pattern); Assert.That(findCollection.AttributeList.Count >= 2, "Different attribute count than expected"); Assert.IsNotNull(findCollection.AttributeList.Find(a => a.FindName == "name"), "does not contain name attribute"); Assert.IsNotNull(findCollection.AttributeList.Find(a => a.FindName == "value"), "does not contain value attribute"); }
public void WriteRadioClick() { var generator = new WatiNCSharp(GetNUnitTemplate()); var wb = new WebBrowser(); var strm = new MemoryStream(); wb.DocumentStream = strm; if (wb.Document == null) { return; } HtmlDocument doc = wb.Document.OpenNew(true); string html = File.ReadAllText(@"C:\Work\TestRecorder3\tests\html\main.html"); if (doc != null) { doc.Write(html); } HtmlElementCollection collection = wb.Document != null?wb.Document.GetElementsByTagName("input") : null; if (collection != null) { var radio = collection.Cast <HtmlElement>().FirstOrDefault(element => element.GetAttribute("type") == "radio"); if (radio != null) { var radioElement = new ActionClick(new BrowserWindow("window"), (IHTMLElement)radio.DomElement); generator.ActionToCode(radioElement); } } Assert.AreEqual(1, generator.Properties.Count, "different than 1 property"); Assert.IsNotNull(generator.Properties[0], "property is blank"); Assert.That(generator.Properties[0].PropertyCode.Contains("_browser.RadioButton(Find"), "Radio code not working"); Assert.AreEqual(1, generator.Code.Count, "different than 1 code line"); Assert.AreEqual("radioRadio1.Click();", generator.Code[0], "other than valid code"); }
// the main automation logic async Task DoAutomationAsync(CancellationToken ct) { await NavigateAsync(ct, () => NavigateAction("http://localhost:81/test.html"), 10000); // timeout in 10s // do the DOM automation HtmlElementCollection all = wb.Document.GetElementsByTagName("button"); // throw if none or more than one element found HtmlElement btn = all.Cast <HtmlElement>().Single( el => el.InnerHtml == "ACCEPT the terms of use"); // simulate a click which causes navigation await NavigateAsync(ct, () => btn.InvokeMember("click"), 10000); // timeout in 10s // form submitted and new page loaded, log the page's HTML string html = ((dynamic)this.wb.Document.DomDocument).documentElement.outerHTML; Trace.Write(html); // could continue with another NavigateAsync // othrwise, the automation completed }
// the main automation logic async Task DoAutomationAsync(CancellationToken ct) { await NavigateAsync(ct, () => this.webBrowser.Navigate("http://localhost:81/test.html"), 10000); // timeout in 10s // page loaded, log the page's HTML Trace.WriteLine(GetBrowserDocumentHtml()); // do the DOM automation HtmlElementCollection all = webBrowser.Document.GetElementsByTagName("button"); // throw if none or more than one element found HtmlElement btn = all.Cast <HtmlElement>().Single( el => el.InnerHtml == "ACCEPT the terms of use"); ct.ThrowIfCancellationRequested(); // simulate a click which causes navigation await NavigateAsync(ct, () => btn.InvokeMember("click"), 10000); // timeout in 10s // form submitted and new page loaded, log the page's HTML Trace.WriteLine(GetBrowserDocumentHtml()); // could continue with another NavigateAsync // othrwise, the automation session completed }
public static List <HtmlElement> ToList(this HtmlElementCollection collection) => collection.Cast <HtmlElement>().ToList();
/// <summary> /// 通过指定的表达式获取元素 /// </summary> /// <param name="判断表达式">返回真即表示获取</param> /// <returns>元素集合</returns> public static IEnumerable <HtmlElement> 通过表达式获取元素(this HtmlElementCollection o, Predicate <HtmlElement> 判断表达式) { return(o.Cast <HtmlElement>().Where(q => 判断表达式(q))); }