Telerik Test Studio的新手?免费下载30天试用版

查找页面元素

Telerik测试框架提供了目前市场上最丰富的标记识别基础设施之一。它建立在“getElementById”、“getElementByName”或“XPath”等常见元素标识方法的基础上,并对它们进行扩展,以提供更适合应用程序自动化场景的标识例程。除了维护一组简单易用的api外,Telerik Testing Framework还引入了一种一致的、可扩展的方式来构建标识,并使用“FindParam”对象对其进行持久化。

所有的发现。Byxxx方法现在支持LINQ查询。

了解Telerik的识别方法是如何工作的非常重要,因为了解这些方法将使您能够更快地利用这些识别方法的强大功能来构建健壮的自动化。

元素识别概述

支持以下识别方法:

\s*\s*Education) // x:表示正则表达式element e= Find. bycontent (@"x:^(\s*\s*Education)");//显式设置内容类型。元素e =查找。ByContent("p:
", FindContentType.StartTagContent);//查找具有部分值的TextContent的所有元素:car IList alle = Find. allbycontent ("p:car") //注意:// //InnerText & FindContentType。值得注意的TextContent: // //示例:
Text1
Text2
// // InnerText for div1: Text1Text2{递归}// TextContent of div1: Text1{非递归}// // ByContent默认为TextContent,这是最常见的用法。

方法 描述 例子
Find.ById () 使用标记文档的set 'id'属性搜索包含在标记文档中的元素。当所需的“id”与元素的id匹配时,返回元素—与getElementById相同
//查找id=input1的元素e = Find. byid ("input1");

Find.ByName () 使用标记文档的set 'name'属性搜索包含在标记文档中的元素。当所需的“name”与元素的名称匹配时,将返回该元素
//查找name=goButton的元素e= Find. byname ("goButton")

Find.ByImage ()
Find.AllByImage ()
搜索与预定义图像相似的元素或“所有”元素
//指定搜索将基于的预定义图像System.Drawing.Image imag1 = System.Drawing.Image. fromfile (@"C:\sampleImg.png");//查找与预设图像相似的元素var elementImg1 = Manager.ActiveBrowser.Find。ByImage (img1, 70);//第二个参数是要使用的阈值%,当搜索图像时//转换找到的元素以便能够使用它。需要指定所定位元素的类型。HtmlButton btn1 = elementimgmt . as ();

Find.ByTagIndex () 使用标记名出现索引搜索元素。在指定的发生索引处查找元素并返回它。此方法使用从零开始的索引。
//查找第3次出现的表标签元素表=查找。ByTagIndex("表",2);

Find.ByAttributes ()
Find.AllByAttributes ()
使用属性值的“精确”或“部分”列表搜索元素或“所有”元素(您可以指定1-N个属性/值对)。当所有属性值匹配时,返回元素或元素集合。
//查找属性class=myclass的第一个元素元素e = Find. byattributes ("class=myclass");//查找属性class=myclass且'src'的部分值为foo.gif的第一个元素(~表示部分)元素e =查找。ByAttributes(“= myclass类”、“src = ~ foo.gif ");//查找class=myclass且src有部分foo的所有元素。AllByAttributes(“= myclass类”、“src = ~ foo.gif ");

Find.ByContent ()
Find.AllByContent ()
使用元素内容的“exact”、“partial”或“regex”搜索元素或“All”元素。元素的内容可以是:InnerText, InnerMarkup, OuterMarkup, TextContent(默认),StartTagContent。
//查找TextContent有文字值的元素:Education // l:表示文字元素e = Find. bycontent ("l:Education");//查找TextContent有父值的元素:Education // p:表示部分元素e = Find. bycontent ("p:Education");//查找TextContent匹配regex表达式的元素:// ^(
Find.ByExpression ()
Find.AllByExpression ()
搜索与htmlfindexexpression匹配的元素或“所有”元素。htmlfindexexpression不能使用任何类型的层次表达式,包括标签索引路径表达式、HTML路径表达式和XPath表达式。
//创建一个HtmlFindExpression元素HtmlFindExpression expr = new HtmlFindExpression("id=?sam","class=~bar","textcontent=!foo");//返回所有匹配HtmlFindExpression的元素IList allSams = Find.AllByExpression(expr);

Find.ByXPath ()
Find.AllByXPath ()
使用XPath表达式搜索元素或“所有”元素。WebAii支持。net框架XPath实现。
//查找横幅img元素element img = Find. byxpath ("//body[1]/table[1]/tbody[1]/tr[1]/td[1]/img[1]");//查找id=div的所有时间IList alldiv = Find. allbyxpath ("/descendant::node()[start -with(@id,'div')]");

Find.ByCssSelector ()
Find.AllByCssSelector ()
使用css选择器查询搜索一个元素或“所有”元素。
//查找活动链接htmllanchor activeLink = Find. bycssselector < htmllanchor >("a:active");//查找所有链接IList< htmllanchor > allLinks = Find. allbycssselector < htmllanchor >("a");

Find.AllByTagName () 搜索具有指定标签名称的“所有”元素,并将其作为元素列表返回。
//返回所有img元素IList alling = Find.AllByTagName("img");

Find.ByNodeIndexPath () 使用dom树节点索引路径搜索元素。这种标识是使用类似xpath的方法完成的,该方法使用层次结构中的节点索引简单地描述到特定元素的层次结构路径,而不必在每个级别指定标记名称。在DomTree层次结构的某个特定位置的部分是一致的,但是元素类型发生了变化的情况下,可以选择这种标识方法。例如,如果您试图定位的元素是在span和div之间波动的另一个元素的直接子元素,那么您可以选择这种识别方法来提供一种一致的方法来识别该元素。
给定这个DOM:  (0)     (2) (0)   (1)      //我们可以找到元素:element target = find . bynodeindexpath ("0/2/0/1");

Find.FromCollection () 使用传入集合中的FindParams查找所有元素。
FindParamCollection col = FindParamCollection. loadfromxml (xml);返回this.FromCollection (col);

Find.ByCustom ()
Find.AllByCustom ()
使用自定义谓词搜索元素或“所有”元素。
//查找ID为'input1'的第一个输入按钮HtmlInputButton btn = Find。ByCustom(delegate(HtmlInputButton e) {if (e. baseelement。IdAttributeValue == "input1")返回true;否则返回false;});Assert.IsNotNull (btn);

识别方法

teleerik测试框架识别方法可使用找到作为'Browser'对象的属性公开的对象:Manager.ActiveBrowser.Find.Byxx(…)也可以作为每个TestRegion对象的属性:TestRegion.Find.Byxx(…)

浏览器类的Find对象(根基础标识—标识示例)和TestRegion类的Find对象(区域基础标识—标识示例)之间的区别RGBI),即浏览器执行从根文档元素开始的所有搜索,而TestRegion的Find对象只搜索特定测试区域中包含的元素,并使用TestRegion的打开标记(即)作为基于引用的标识的根元素,如标记名称出现索引和XPath标识。

使用TestRegions,根据每个自动化测试所针对的应用程序区域,您可以使用不同的Find对象来为每个测试提供更高级别的独立性,并使其免受目标区域之外的产品更改的影响。更详细地讨论了这个主题TestRegions简介

参数中使用的匹配语法

接受nameValuePairs参数的Byxxx函数识别以下匹配语法:

  • class=myclass -匹配'class'属性等于'myclass'

  • class=~myclass -匹配'class'属性包含字符串'myclass'。这被称为“部分匹配”语法。

  • 类= !myclass -当'class'属性不包含字符串'myclass'时匹配。

您可以将上述语法混合到单个Byxxx函数中。(如发现。ByAttributes("class=~my", "class=~class", "class=!other")将匹配字符串"class=myclass",但不匹配"class=myotherclass"。)

接受内容字符串参数的Byxxx函数识别以下匹配语法:

  • 对于精确匹配的字符串,在字符串参数前加上'l:'。如。'l:Vacation homes'将与'Vacation homes'完全匹配。如果不使用前缀,这也是默认值。

  • 在字符串参数前加上'p:',用于部分字符串匹配。如。'p:Arizona'将匹配任何包含字符串'Arizona'的内容。

  • 在字符串参数前加上'x:',用于正则表达式匹配。如。“x:(喷火]将匹配任何中间包含字符串'foo'的内容。有关正则表达式的更多信息,请参见这篇MSDN文章

识别样本

为了帮助说明上述识别方法,让我们使用下面的示例应用程序。请注意,此应用程序过度使用了TestRegions,以便演示使用这两种方法可以执行的不同识别方法印度储备银行RGBI

< !DOCTYPE html>   
    < div > < input type = " text " id = " input0 " / > < !——testregion id="Region11"--> 
数据显示
< !——testregion id="Region111"--> 去Google < id = " a1 " href = " http://www.kayak.com " >去Kayak < / > < !——/ testregion > < !——/ testregion > < !——testregion id="Region112"-->
部分数据
一些数据
一些数据部分数据
部分数据 < / div > < div id = " div7 " bla = >“foo: $ $ __fdd”一些数据< / div > < div id = " div8 " > < / div > <一些数据!——/testregion——>
Baidu

下面的代码显示了您可以在测试代码中使用的识别元素的不同方法。注意,在下面的示例中,我们使用Visual Studio的Assert类来演示如何以不同的方式识别不同的对象,并演示如何使用RGBI在TestRegions和整个文档DomTree中使用RBI来确定识别的范围。

//设置主自动化对象的快捷方式。浏览器= mgr.ActiveBrowser;查找rootFind = browser .Find;//所有的testregion都在这里初始化。testregionr1 = browser . regions ["Region1"];TestRegion r11 = browser . regions ["Region11"];TestRegion r111 = browser . regions ["Region111"];TestRegion r1111 = browser . regions ["Region1111"];TestRegion r112 = browser . regions ["Region112"];//***使用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.IsTrue(rootFind.ByAttributes("id=bla") == null); 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.IsTrue(rootFind.AllByTagName("div").Count - 1 == rootFind.AllByXPath("/descendant::node()[starts-with(@id,'div')]").Count); Assert.IsTrue(rootFind.AllByAttributes("href=http://www.kayak.com").Count == 5); Assert.IsTrue(rootFind.AllByAttributes("id=~button").Count == 2); Assert.IsTrue(r1.Find.AllByTagName("div").Count == 10); Assert.IsTrue(r1111.Find.AllByTagName("div").Count == 0); Assert.IsTrue(r111.Find.AllByTagName("a").Count == 2); Assert.IsTrue(r11.Find.AllByAttributes("id=~div").Count == 9); //*** 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"));
将快捷方式设置为主要自动化对象。Dim浏览器作为ArtOfTest.WebAii.Core.Browser = Manager。ActiveBrowser Dim rootFind As Find = browser。找到' All the testregion are initialized here. Dim r1 As TestRegion = brwser.Regions("Region1") Dim r11 As TestRegion = brwser.Regions("Region11") Dim r111 As TestRegion = brwser.Regions("Region111") Dim r1111 As TestRegion = brwser.Regions("Region1111") Dim r112 As TestRegion = brwser.Regions("Region112") '*** Using identification by id. Dim div0 As Element = r1.Find.ById("div0") '*** Using tag name occurrence index. Dim div As Element = r1.Find.ByTagIndex("div", 0) Dim div1 As Element = r112.Find.ByTagIndex("div", 0) ' Some verification to illustrate how the same element that was found ' Imports TestRegion Find objects above, can be also found ' Imports 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"))
在本文中
Baidu
map