public void FindElementsByID() { Manager.LaunchNewBrowser(); ActiveBrowser.NavigateTo(Path.Combine(TestContext.TestDeploymentDir, TESTPAGE)); // Set the short-cuts to the main automation objects. Browser brwser = Manager.ActiveBrowser; Find rootFind = brwser.Find; // All the testregion are initialized here. TestRegion r1 = brwser.Regions["Region1"]; TestRegion r11 = brwser.Regions["Region11"]; TestRegion r111 = brwser.Regions["Region111"]; TestRegion r1111 = brwser.Regions["Region1111"]; TestRegion r112 = brwser.Regions["Region112"]; //*** Using identification by id. Element div0 = r1.Find.ById("div0"); //*** Using tag name occurrence index. Element div = r1.Find.ByTagIndex("div", 0); Element div1 = r112.Find.ByTagIndex("div", 0); // Some verification to illustrate how the same element that was found // using TestRegion Find objects above, can be also found // using the main Browser Find object. Assert.IsTrue(div.Equals(rootFind.ByTagIndex("div", 0))); Assert.IsTrue(div0.Equals(rootFind.ByTagIndex("div", 1))); //*** Using attribute identification. Assert.IsTrue(div1.Equals(rootFind.ByAttributes("id=div1"))); Assert.IsNull(rootFind.ByAttributes("id=bla")); Assert.IsNotNull(rootFind.ByAttributes("href=http://www.kayak.com")); //*** Using partial attribute identification. Assert.IsTrue(rootFind.ByAttributes("bla=~__").Equals(rootFind.ById("div7"))); Assert.IsNull(rootFind.ByAttributes("id=~div7", "bla=~wow")); Assert.IsNotNull(rootFind.ByAttributes("onclick=~clicked();", "id=~button2")); //*** Using 'All' elements identification. // Note here that the first 'div' does not have any id that contains 'div' hence the '- 1'. Assert.AreEqual(rootFind.AllByTagName("div").Count - 1, rootFind.AllByXPath("/descendant::node()[starts-with(@id,'div')]").Count); Assert.AreEqual(5, rootFind.AllByAttributes("href=http://www.kayak.com").Count); Assert.AreEqual(2, rootFind.AllByAttributes("id=~button").Count); Assert.AreEqual(10, r1.Find.AllByTagName("div").Count); Assert.AreEqual(0, r1111.Find.AllByTagName("div").Count); Assert.AreEqual(2, r111.Find.AllByTagName("a").Count); Assert.AreEqual(9, r11.Find.AllByAttributes("id=~div").Count); //*** Using NodeIndexPath identification. Assert.IsTrue(r1.Find.ByNodeIndexPath("0/1/1").IdAttributeValue.Equals("input1")); Assert.IsTrue(rootFind.ByNodeIndexPath("1/0/0").TagName.Equals("div", StringComparison.OrdinalIgnoreCase)); //*** Using name Assert.IsNull(r1.Find.ByName("bla")); }
public void FindAllByxx() { // // The Find.AllByxx methods allow you to find multiple elements on the page using certain criteria // // Example 1: Get all table elements on the page. // IList <Element> tables = Find.AllByTagName("table"); Assert.IsTrue(tables.Count == 2); // Example 2: Get all table cells that contain a 'yes' // IList <Element> cells = Find.AllByContent("yes"); Assert.IsTrue(cells.Count == 5); // Example 3: Find all table rows that contain a scope=row attribute IList <Element> rows = Find.AllByAttributes("scope=row"); // partial compare (~) is also supported Assert.IsTrue(rows.Count == 3); // Example 4: Same as #1 above but using an XPath tables = Find.AllByXPath("/descendant::table"); Assert.IsTrue(tables.Count == 2); }
public void HtmlSelectExamples() { /// You can find the HtmlSelect control via a number of methods. Use any of the following /// Find the control by assigned name HtmlSelect mySelect1 = Find.ByName <HtmlSelect>("ctl00$MainContent$drpMake"); /// Or find the control by it's assigned ID HtmlSelect mySelect2 = Find.ById <HtmlSelect>("ctl00_MainContent_drpMake"); /// Using a partial match to find the control even if the name changes slightly HtmlSelect mySelect3 = Find.ByAttributes <HtmlSelect>("id=~_drpMake"); /// Find all the controls matching a pattern which can then be iterated through IList <HtmlSelect> mySelectList = Find.AllByAttributes <HtmlSelect>("id=~_product"); /// Verify the select contains the expected options Assert.AreEqual(76, mySelect1.Options.Count); Assert.IsTrue(mySelect1.Options[0].Text.Equals("- Select -")); Assert.IsTrue(mySelect1.Options[1].Text.Equals("Alfa Romeo")); Assert.IsTrue(mySelect1.Options[2].Text.Equals("AM General")); Assert.IsTrue(mySelect1.Options[3].Text.Equals("AMC")); /// Iterate through the list of selects foreach (HtmlSelect aSelect in mySelectList) { if (aSelect.ID.Contains("color")) { /// Found the Color drop down /// Validate the options for it /// Iterate through all the options looking for something foreach (HtmlOption anOption in aSelect.Options) { Assert.IsTrue(anOption.Text.StartsWith("Color : ")); } } else if (aSelect.ID.Contains("size")) { /// Found the Size drop down /// Validate the options for it /// Iterate through all the options looking for something foreach (HtmlOption anOption in aSelect.Options) { Assert.IsTrue(anOption.Text.StartsWith("Size : ")); } } else if (aSelect.ID.Contains("option")) { /// Found the Option drop down /// Validate the options for it /// Iterate through all the options looking for something foreach (HtmlOption anOption in aSelect.Options) { Assert.IsTrue(anOption.Text.StartsWith("Option : ")); } } else { Assert.Fail("Unrecognized Select drop down found"); } } }
private string ReturnSearchCategoriesCounts(string queryWord) { string result = ""; ActiveBrowser.NavigateTo("http://telerikacademy.com/"); var searchField = Find.ById <HtmlInputText>("SearchTerm"); var submitSearchButon = Find.ById <HtmlInputSubmit>("SearchButton"); searchField.Text = queryWord; submitSearchButon.Click(); int coursesCount; int usersCount; int tracksCount; try { coursesCount = Find.AllByAttributes <HtmlDiv>("class=SearchResultsCategory").Where(div => div.InnerText.Contains("Курсове")).FirstOrDefault().Find.AllByTagName <HtmlListItem>("li").Count(); } catch { coursesCount = 0; } try { usersCount = Find.AllByAttributes <HtmlDiv>("class=SearchResultsCategory").Where(div => div.InnerText.Contains("Потребители")).FirstOrDefault().Find.AllByTagName <HtmlListItem>("li").Count(); } catch { usersCount = 0; } try { tracksCount = Find.AllByAttributes <HtmlDiv>("class=SearchResultsCategory").Where(div => div.InnerText.Contains("Тракове")).FirstOrDefault().Find.AllByTagName <HtmlListItem>("li").Count(); } catch { tracksCount = 0; } result += usersCount + "," + coursesCount + "," + tracksCount; return(result); }
public void SearchForXaml() { // Start browser and navigate to Telerik Academy home page Manager.LaunchNewBrowser(); ActiveBrowser.NavigateTo("http://telerikacademy.com/"); // Enter "XAML" and click Search button Find.ById <HtmlInputText>("SearchTerm").Text = "XAML"; Find.ById <HtmlInputSubmit>("SearchButton").Click(); // Locate title var title = Find .ByAttributes <HtmlContainerControl>("class=SearchResultsListTitle"); // Locate cources list var courcesList = Find .AllByAttributes <HtmlDiv>("class=SearchResultsCategory") .Where(category => category.InnerText.Contains("Курсове")) .FirstOrDefault() .Find.ByExpression <HtmlUnorderedList>(new HtmlFindExpression("tagname=ul")); // Locate tracks list var tracksList = Find .AllByAttributes <HtmlDiv>("class=SearchResultsCategory") .Where(category => category.InnerText.Contains("Тракове")) .FirstOrDefault() .Find.ByExpression <HtmlUnorderedList>(new HtmlFindExpression("tagname=ul")); // Verify results title Assert.AreEqual("Вашето търсене за \"XAML\" върна следните резултати" , title.TextContent); // Verify results Assert.AreEqual(3, courcesList.Items.Count(), "Count of cources is wrong"); Assert.AreEqual(2, tracksList.Items.Count(), "Count of tracks is wrong"); }
public void FindingControls() { // Find the first table on the page. HtmlTable outertable = Find.ByTagIndex <HtmlTable>("table", 0); Assert.IsTrue(outertable.Rows.Count == 3); // Find the first table inside the outer table // // Note: HtmlContainerControls have a Find object // associated with them that scopes all the Find.Byxx searches // to elements contained within them only. // So even if you have multiple controls with similar contained // elements, this will help avoid any conflicts. // Also, note how we are referencing the innertable using index 0 // since it is the first table inside our outer table. // HtmlTable innerTable = outertable.Find.ByTagIndex <HtmlTable>("table", 0); // Special Find for tables. HtmlTableCell cell = innerTable.Find.TableCell("TD21"); Assert.IsTrue(cell.TextContent.Equals("TD21")); // Find all HtmlInputImage controls with 'src' containing partial value 'logo' IList <HtmlInputImage> imgCtrls = Find.AllByAttributes <HtmlInputImage>("src=~logo"); // There should only be one HtmlInputText control. All controls // That match the attribute list but fail validation by the HtmlInputText control // will be ignored. Assert.AreEqual(1, imgCtrls.Count); // Find the <div> section containing the Eastern US Division sales report HtmlDiv EasternUSDivision = Find.ByContent <HtmlDiv>("Eastern US Division", FindContentType.TextContent); Assert.IsNotNull(EasternUSDivision); // Traverse the control tree upwards too. You can easily // Find the container control of a certain type from its children. // Find the owner form of the submit button HtmlInputSubmit submitButton = Find.ById <HtmlInputSubmit>("submit"); HtmlForm form = submitButton.Parent <HtmlForm>(); // Get the name of the frame targeted by the form string myFrame = form.Target; Assert.AreEqual("_self", myFrame); // Find the 3rd text input field on the form. Index is 0 based, so we enter 2 to mean the 3rd one. HtmlInputText input = form.Find.ByTagIndex <HtmlInputText>("input", 2); Assert.IsNotNull(input); // Find the table that contains this cell. HtmlTable table = cell.Parent <HtmlTable>(); Assert.IsTrue(table.ID.Equals(innerTable.ID)); // Find the parent table of this inner table. HtmlTable table2 = table.Parent <HtmlTable>(); Assert.IsTrue(table2.ID.Equals(outertable.ID)); // Find the form this table is contained in. HtmlForm form1 = table2.Parent <HtmlForm>(); Assert.IsTrue(form1.ID.Equals("form1")); // Note: if Find.IgnoreFindAllControlMismatch is set to false, the // Find.Allxx<>() method will throw when encounters a control that matches the search // criteria but fails to be the desired HTMLControl. // TIP: The above example is to show the functionality of the Find.Byxx<> function. // It is not recommended to use loose attribute finds like the one above since it will // match many controls on the page that will be ignored. The better the find constraints // are the more performant the search is. // For example, the above Find can be enhanced by doing ("id=~1","type=text") }
public static ICollection <TControl> AllByTitleContent <TControl>(this Find find, string target) where TControl : Control, new() { return(find.AllByAttributes <TControl>(string.Concat("title=~", target))); }