量角器中Selenium定位器的完整指南(示例)

在测试网站的功能时,特别是Web元素(例如单选按钮,文本框,下拉列表等),您需要确保能够访问这些元素。 Selenium定位器正是出于这个目的,通过使用此命令,我们可以识别这些Web元素DOM(文档对象模型),以通过脚本执行Selenium测试自动化 。

这些Selenium定位器对于脚本编写至关重要,因为它们错了,您的自动化脚本将无法工作。 因此,您的Selenium测试自动化工作将依赖于任何测试框架中的这些Selenium定位器。 Protractor是一个Angular JS框架,具有许多Selenium定位符,可以在运行时使用特定的By方法来标识。

在本量角器测试教程中 ,我们将深入讨论“量角器”中的Selenium定位器,以及如何使用定位器与应用程序进行交互并获取当前的运行状态。 因此,让我们开始吧。

使用量角器和Selenium自动化跨浏览器测试

量角器中Selenium定位器的重要性是什么?

让我们开始讨论量角器测试教程,方法是讨论选择定位器时应牢记的各种功能,以确保其性能。 在大多数情况下,量角器中的Selenium定位器已被证明是良好且可靠的。 它提供的一些功能包括:

1.良好的可读性:
量角器中的Selenium定位器易于阅读和使用。 定位器通过使用户可以访问它们来为测试代码提供足够的灵活性。

2.减少维护:

  • 量角器中的Selenium定位器以优化的方式开发,因此需要较少的维护费用。
  • 定位器的结构设计精巧,因此即使元素位置发生变化,也无需更新定位器。 仅当Web应用程序的功能发生任何更改时,才需要进行修改。

3.提高速度:
这是Selenium定位器最重要的属性之一,因为此功能决定了Web应用程序的性能。 量角器中的Selenium定位器具有唯一的ID,这使其比其他Selenium定位器相对更快。 有时,元素定位的速度还取决于浏览器的内部处理速度。

量角器中Selenium定位器的目的

继续我们的量角器测试教程,我们将讨论Selenium定位器的重要性。 为了在Protractor中编写良好的端到端测试,要记住的重要一点是为网页找到合适的文档对象模型(DOM)元素。 它倾向于通过实例化的对象向全球导出定位器工厂。 由于量角器基于Selenium接口构建,因此量角器中的Selenium定位器与Selenium WebDriver关联的定位器具有相当的可比性。 因此,很有趣的是,该框架也支持量角器中的Selenium定位器。

量角器中Selenium定位器的工作

接下来,在本量角器测试教程中,我们将讨论Selenium定位器如何在量角器中工作。 定位器在量角器中的运行方式是通过导出全局函数(即“ element”),该函数输入定位器并向我们提供ElementFinder。

另一方面,ElementFinder提供了一种与元素进行通信并使用各种操作方法(例如getText(),click()和sendKeys())获取有关元素的详细信息的基本方法。 这些方法非常流行,在执行Selenium测试自动化时经常使用。

“元素”功能的主要目的是定位单个元素。 要定位多个元素,请使用'element.all'函数。

还有其他几种方法可以用来在Protractor中找到元素,也可以使用Angular JavaScript框架中的元素定位策略,例如by.model(),by.repeater(),by.binding()等。

量角器中的Selenium定位剂清单

现在,在量角器测试教程的这一部分中,让我们熟悉一些主要用于定位DOM元素的全局变量,并提供示例,以更好地了解量角器中的这些Selenium定位器。 这些是与“ by”关键字相关的一组元素定位器策略,例如by.className,by.css,by.model等。一些最常用的方法是:

  • by.className
  • by.id
  • 由CSS
  • by.linkText
  • 按名字
  • by.partialLinkText
  • by.tagName
  • by.xpath

1. by.className
className定位器是量角器中使用最广泛的Selenium定位器之一。 ts的目的是检查页面中具有class属性的元素,然后进一步对特定于其类名的元素进行分类。

例如:-
XML文档样本:

/* The list contains a class for positive or negative */<ul class="digits"><li class="positive">5</li><li class="negative">-6</li>
</ul>

使用的定位器:-

/* The locator that returns the expected element for the specified class */var even = browser.findElement(by.className(positive));
expect(even.getText()).toBe('5');
// making use of our locator in our test script //
expect(browser.findElement(by.className('even'))).toBe('-6');

2. by.id

Id定位符用于根据XML文档结构中定义的id属性在网页中发现元素。

例如 :-
XML文档样本:

/* The list contains class for positive or negative */<ul class="digits"><li class="negative">-6</li><li class="positive" id="positiveNumber">5</li>
</ul>

使用的定位器:-

/* The locator that returns the expected element for the specified id */// making use of our locator in our test script //
var positive = browser.findElement(by.id('positiveNumber'));
expect(positive.getText()).toBe('5');

3. by.css

CSS定位器根据CSS选择器(即用于区分网页上现有元素的标识符值)来帮助识别元素并对其进行分类。 当我们没有选择基于类名或ID进行选择时,这些Selenium定位器也是量角器中最优选的替代物之一。

例如 :-
XML文档样本:

/* The list contains class for two color i.e blue or red */<ul class="color"><li class="first">blue</li><li class="second" id="secondColor">red</li>
</ul>

使用的定位器:-

/* The locator that returns the expected element for the specified CSS */var first = browser.findElement(by.css('.first'));
expect(first.getText()).toBe('blue');
// making use of our locator in our test script //
expect(browser.findElement(by.css('#secondColor'))).toBe('red');

4. by.linkText

所述的目的LINKTEXT定位器是以识别对应于所述锚固元件即<一>标记在DOM匹配的字符串的文本。 它仅对超链接有效,并且默认情况下,如果网页上的链接文本存在多个元素,则选择第一个链接文本。

例如 :-
XML文档样本:

/* The anchor tag which contains the website name and its URL */<a href="http://www.lambdatest.com">LambdaTest</a><a href="http://www.lambdatest.com">LambdaTest</a>

使用的定位器:-

/* The locator that returns the expected element for the link i.e. Lambda Test*/// making use of our locator in our test script //
var myLink = element(by.linkText(‘LambdaTest'));expect(myLink.getTagName()).toBe('a');

5.借名

名称定位器用于发现名称属性中具有特定值的元素。
例如 :-

XML文档样本:

/* The list contains a class for two names i.e John and Mark */<ul><li name="developer">John</li><li name="tester">Mark</li></ul>

使用的定位器:-

/* The locator that returns the expected element for the specified name */// making use of our locator in our test script //
var developer = browser.findElement(by.name('developer'));// making use of our locator in our test script //
var tester = browser.findElement(by.name('tester'));expect(developer.getText()).toBe('John');expect(tester.getText()).toBe('Mark');

6. by.partialLinkText

partialLinkText定位器用于需要在链接文本元素中查找包含字符串或字符串部分的元素的情况。

例如 :-

XML文档样本:

/* The list contains anchor tag which has the required text */<ul><li><a href="http://www.lambdatest.com"> Selenium test automation Cloud</a></li><li>Online Selenium Grid for Automated Testing</li>
</ul>

使用的定位器:-

// /* The locator that returns the expected element i.e. gives us the 'a' element value ‘Selenium test automation Cloud’ and navigates to the same link */// making use of our locator in our test script //
var myLink = browser.findElement(by.partialLinkText('Cloud'));myLink.click();

7. by.tagName

tagName定位符用于定位具有特定标签名称的元素。 它在网页中查找具有任何标签名称的元素,例如<a>,<div>,<p>等。它的功能类似于XML文档结构中使用的getElementsByTagName函数。

例如:-

XML文档样本:

/* The list contains anchor tag which has the required text */<a href="http://www.lambdatest.com">LambdaTest</a>

使用的定位器:-

// /* The locator that returns the expected element i.e. gives us the 'a' tag  value and that matches with the text given */ //// making use of our locator in our test script //
expect(element(by.tagName('a')).getText()).toEqual('LambdaTest');

8. by.xpath

Xpath定位器用于查找提供的XML Xpath Selector的匹配元素。 处理XPath Selenium定位器时要注意的重要事项是,要搜索整个XML文档模型并为其元素化,我们必须以“ //”开始XPath定位器。

例:

 XPath = //*[ @ value='Inbox'] - matches with Inbox  xpath= //button[ @name="Gma"] - matches with Gmail 

例如 :-

XML文档样本:

/* The list contains anchor tag which has the required text */<ul><li><a href="http://www.lambdatest.com">Selenium test automation Cloud </a> </li><li> Online Selenium Grid for Automated Testing </li>
</ul>

使用的定位器:-

// /* The locator that returns the expected element i.e. gives us the 'a' tag  value with the help of XPath and that matches with the text given */// making use of our locator in our test script //
var xpathEle = browser.findElement(by.xpath('//ul/li/a'));expect(xpathEle.getText()).toBe(‘Selenium test automation Cloud’);

特定于角度的Selenium定位器

现在,在此量角器测试教程中,让我们看一下Angular中使用的一些Selenium定位器,但它们也可用于量角器框架。

  • 按型号
  • by.buttonText
  • by.partialButtonText
  • by.exactBinding
  • 绑定
  • by.exactRepeater
  • 中继器
  • by.cssContainingText
  • 选项
  • by.deepCss
  • by.addLocator

1.按型号

模型定位器标识具有与ng-model属性关联的确切文本的元素。

例如 :-

XML文档样本:

// /* The XML input type contains the text with the model attribute */ //<input type="text" ng-model="developer.name">

使用的定位器:-

// /* The locator finds the element with the model attribute and returns the value */ //// making use of our locator in our test script //
element(by.model('developer.name')).click();

2. by.buttonText

buttonText定位器查找与按钮标签具有相同文本或在标签的子元素内部的元素匹配。

例如 :-

XML文档样本:

// /* The XML contains the button with the required value */ //<button> Selenium test automation Cloud </button>

使用的定位器:-

// /* The locator finds the element with the button tag and returns the value */ //// making use of our locator in our test script //
element(by.buttonText('Selenium test automation Cloud'));

3. by.partialButtonText

partialButtonTextlocator查找与包含文本部分的元素的匹配,即在按钮标签或标签子元素内部的部分匹配。

例如 :-

XML文档样本:

// /* The XML contains the button with the required value */ //<button> Selenium test automation Cloud </button>

使用的定位器:-

// /* The locator finds the element with the button tag and returns the value */ //// making use of our locator in our test script //
element(by.partialButtonText('Cloud'));

4. by.exactBinding

compareBinding定位器用于使用提供的确切字符串/文本值来定位ng-bind属性。 它不会检查文本中是否有任何部分匹配。

例如:-

XML文档样本:

// /* The XML input type contains the text with the bind attribute */ //<p ng-bind="developer.name"></p>

使用的定位器:-

// /* The locator finds the element with the bind attribute and returns the value */ //// making use of our locator in our test script //
expect(element(by.exactBinding('developer.name')).isPresent()).toBe(true);

5.绑定

该绑定定位器用于使用给定的文本值来定位ng-bind属性。 它还有助于查找部分匹配的文本,即,如果某个属性与给定的定位器匹配,则该元素将由我们的定位器找到,因此将返回相关的匹配元素。

例如:-

XML文档样本:

// /* The XML input type contains the text with the bind attribute */ //<p ng-bind="developer.name">John</p>

使用的定位器:-

// /* The locator finds the element with the bind attribute and returns the value */ //// making use of our locator in our test script //
var eleName = element(by.binding(developer));
expect(eleName.getText()).toBe('John');

6. by.exactRepeater

compareRepeater定位器标识具有与ng-repeat属性关联的确切文本的元素。 它不会检查文本中是否有任何部分匹配。

例如:-

XML文档样本:

// /* The XML input type contains the text with the bind attribute */ //<li ng-repeat="dev in developer_names"></li><li ng-repeat="test in tester_names"></li>

使用的定位器:-

// /* The locator finds the element with the bind attribute and returns the exact value */ //// making use of our locator in our test script //expect(element(by.exactRepeater('dev in developer_names')).isPresent()).toBe(true);

7. by.repeater

转发器定位器用于查找具有ng-repeat属性的元素。 它还有助于查找部分匹配的文本,即,如果某个属性与给定的定位器匹配,则该元素将由我们的定位器找到,因此将返回相关的匹配元素。

例如:-

XML文档样本:

// /* The XML input type contains the text with the repeater attribute */ //<tr ng-repeat="developer_info"><td>{{dev.id}}</td><td>{{dev..name}}</td><td>{{dev.salary}}</td>
</tr>

使用的定位器:-

// /* The locator finds the element with the repeater attribute and returns the value */ //// making use of our locator in our test script //var devID = element(by.repeater('developer_info').row(0));
expect(devID.getText()).toBe('2');var devName = element(by.repeater('developer_info').row(1));
expect(devName.getText()).toBe('Mark');

8. by.cssContainingText

cssContainingText定位器通过具有特定文本字符串CSS查找元素,即,它结合了CSS定位器和文本元素定位器的功能来标识该元素。

例如:-

XML文档样本:

// /* The XML input type contains the text with the css text */ //<ul><li class="color">Blue</li><li class="color">Red</li>
</ul>

使用的定位器:-

// /* The locator finds the element and returns the value for the Blue color but not the Red color */ //// making use of our locator in our test script //var blue = element(by.cssContainingText('.color', 'Blue'));

9. by.options

选项定位器标识与属性ng-option关联的元素。

例如 :-

XML文档样本:

// /* The XML input type contains the text with the option attribute */ //<select ng-options="Colors options in the custom collection"><option value="0">Blue Color</option><option value="1">Red Color</option><option value="2">Green Color</option>
</select>

使用的定位器:-

// /* The locator finds the element with the options attribute and returns the value */ //// making use of our locator in our test script //var colorOptions = element.all(by.options('Colors options in the custom collection'));
expect(colorOptions.count()).toEqual(Green);

10. by.deepCss

量角器中的deepCss定位器用于发现阴影DOM元素,默认情况下,使用标准元素Selenium定位器不容易发现该阴影DOM元素。

例如:-

XML文档样本:

// /* The XML input type contains the text and returns the fetched value */ //<div><span id="outerspan">   //outerspan<"shadow tree">           //shadow tree<span id="span1"></span><"shadow tree"><span id="span2"></span></></>
</div>

使用的定位器:-

// /* The locator finds the element with the options attribute and returns the value */ //// making use of our locator in our test script //var mySpan = element.all(by.deepCss('span'));  //myspan
expect(mySpan.count()).toEqual(7);
// making use of our locator in our test script //var checkSpans = element.all(by.css('span');  //verify span
expect(checkSpans.count()).toEqual(5);

在量角器测试教程的下一部分中,我们将讨论如何将量角器与其他强大的工具集成。 在此之前,如果您想设置一个量角器来运行Selenium自动化脚本,则可以查阅我们的支持文档。

11. by.addLocator

量角器中的addLocator用于创建自定义定位器,并稍后在配置中加载它们。

例如 :-

XML文档样本:

// /* The XML input type contains the text and returns the fetched value */ //<button ng-click="viewResults()">View</button>

使用的定位器:-

// /* The locator finds the element with the options attribute and returns the value */ //// making use of our locator in our test script //by.addLocator('Selenium Grid',function(buttonText, opt_parentElement, opt_rootSelector) {
var using = opt_parentElement || document,
buttons = using.querySelectorAll(‘Automate testing’);
return Array.prototype.filter.call(buttons, function(button) {
return button.textContent === buttonText;
});
});

结论

正如我们在此量角器测试教程中看到的那样,由于量角器是基于Selenium构建的,并且主要用于Angular网站,因此它从它们继承属性。 这就是我们在量角器中使用Selenium定位器的原因,它为框架增添了更多魅力,并在充分使用时使其更强大。 另一方面,量角器不仅仅是Selenium定位器,保护器测试还有许多其他方面和功能可用于市场,这增加了更多的内容,我们最终将在量角器测试教程中进行介绍。


翻译自: https://www.javacodegeeks.com/2020/04/complete-guide-to-selenium-locators-in-protractor-examples.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/331207.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

MySQL的自然联结+外部联结(左外连接,右外连接)+内部联结

【0】README0.1&#xff09;本文旨在review MySQL的自然联结外部联结&#xff08;左外连接&#xff0c;右外连接&#xff09;内部联结 的相关知识&#xff1b;【1】自然联结1&#xff09;自然联结定义&#xff1a;无论何时对表进行联结&#xff0c;应该至少有一个列出现不止一个…

MySQL 添加列+修改列+删除列

【0】REAMDE 0.1&#xff09;本文部分文字描述转自 http://blog.163.com/zhangjie_0303/blog/static/99082706201191911653778/ 0.2&#xff09;本文旨在review mysql 对列的相关操作&#xff1a;如添加&#xff0c;修改&#xff0c;删除以及重命名表名等操作&#xff1b; 【1】…

compose应用_带有PostgreSQLDocker Compose for Spring Boot应用程序

compose应用在此博客文章中&#xff0c;您将学习如何使用PostgreSQL配置Spring Boot应用程序以与Docker Compose一起运行。 这篇博客文章涵盖&#xff1a; Spring Boot应用程序Dockerfile配置&#xff0c;在依赖项和资源之间进行了清晰的分离 用于通过PostgreSQL运行应用程序…

单例模式面试题

转载自 单例模式面试题&#xff08;特点、理解&#xff09; (1)单例模式特点&#xff08;什么是单例模式&#xff09;&#xff1f;  a.单例类只能有一个实例。  b.单例类必须自己创建自己的唯一实例。  c.单例类必须给所有其他对象提供这一实例。 (2)单例模式的作用&#x…

MySQL的source命令不加分号和delimiter的使用

【0】README 0.1&#xff09;本文旨在 review source 命令&#xff0c; 这一直是我的痛&#xff0c;为什么一直导入 sql 文件不成功&#xff0c;一直没有写 blog 吧他 记录下来&#xff08;事实上&#xff0c;也间接证明我就是个小白&#xff09;&#xff1b; 0.2&#xff09…

selenium自动化测试_维持Selenium测试自动化的完美方法

selenium自动化测试毫无疑问&#xff0c; 自动浏览器测试已改变了软件开发的工作方式。 如果不是Selenium&#xff0c;我们将无法像我们一样使用各种各样的无错误的Web应用程序。 但是有时&#xff0c;甚至IT部门也误解了自动化一词。 大多数人认为计算机将为他们完成所有测试…

单例模式的优与劣

转载自 大话设计模式(四)单例模式的优与劣前言首先来明确一个问题&#xff0c;那就是在某些情况下&#xff0c;有些对象&#xff0c;我们只需要一个就可以了&#xff0c;比如&#xff0c;一台计算机上可以连好几个打印机&#xff0c;但是这个计算机上的打印程序只能有一个&…

MySQL存储过程+游标+触发器

【0】README0.1&#xff09;本文旨在 arrange mysql 存储过程及如何在存储中使用游标 的相关知识&#xff1b;0.2&#xff09;delimieter的用法&#xff1a;参见 http://blog.csdn.net/pacosonswjtu/article/details/51407756&#xff1b;【1】存储过程基础1&#xff09;intro…

java迭代器退出迭代_使用Java迭代器修改数据时要小心

java迭代器退出迭代随着本学期的结束&#xff0c;我想我会分享一个关于如何非常熟悉Java迭代器的小故事。 现实世界语境 就上下文而言&#xff0c;我开设了第二年软件组件课程&#xff0c;这是尝试进入该专业的学生的最后障碍。 当然&#xff0c;这门课程对学生来说压力很大&a…

pojo 带参构造函数_带有Java Pojo作为输入输出示例的AWS Lambda函数

pojo 带参构造函数在上一个教程中&#xff0c;我们看到了如何使用Java创建AWS Lambda函数&#xff0c;我们传递了String作为输入&#xff0c;还返回了String作为Output。如果您是第一次创建lambda函数&#xff0c;我建议先阅读该教程。 在本教程中&#xff0c;我们将看到如何传…

MySQL检索数据(过滤+通配符+正则表达式)

【0】README0.1&#xff09;本文部分文字描述转自“MySQL 必知必会”&#xff0c;旨在review “MySQL的基础知识”&#xff1b;【1】检索数据1&#xff09;检索单个列&#xff1a;select a_name from table_name;2&#xff09;检索多个列&#xff1a;select a_name,b_name from…

创建者模式

转载自 设计模式之创建者模式创建者模式又叫建造者模式&#xff0c;是将一个复杂的对象的构建与它的表示分离&#xff0c;使得同样的构建过程可以创建不同的表示。创建者模式隐藏了复杂对象的创建过程&#xff0c;它把复杂对象的创建过程加以抽象&#xff0c;通过子类继承或者重…

buildpack_使用Buildpack容器化Spring Boot应用程序

buildpack在本文中&#xff0c;我们将看到如何使用Buildpacks容器化Spring Boot应用程序。 在先前的一篇文章中&#xff0c;我讨论了Jib 。 Jib允许我们在不使用Dockerfile的情况下将任何Java应用程序构建为Docker映像。 现在&#xff0c;从Spring Boot 2.3开始&#xff0c;我们…

MySQL创建字段+数据处理函数+汇总数据(聚集函数)+分组数据

【0】README0.1&#xff09;本文部分文字描述转自“MySQL 必知必会”&#xff0c;旨在review“MySQL创建字段数据处理函数汇总数据&#xff08;聚集函数&#xff09;分组数据” 的基础知识&#xff1b;【1】创建计算字段1&#xff09;problemsolution1.1&#xff09;problem&am…

apache.camel_Apache Camel 3.2 – Camel的无反射配置

apache.camel在Apache Camel项目中&#xff0c;我们正在努力开发下一个即将发布的Apache Camel 3.2.0版本。 我们在Camel 3中努力研究的问题之一就是使其变得更小&#xff0c;更快。 其中一个方面是配置管理。 您可以按照12要素原则以多种方式完全配置Camel&#xff0c;以使配…

MySQL数据检索+查询+全文本搜索

【0】README0.1&#xff09;本文部分文字描述转自“MySQL 必知必会”&#xff0c;旨在review“MySQL数据检索查询全文本搜索” 的基础知识&#xff1b;【1】使用子查询1&#xff09;查询定义&#xff1a;任何sql 语句都是查询。但此术语一般指 select语句&#xff1b;SQL 还允许…

selenium自动化测试_49自动化测试中最常见的Selenium异常

selenium自动化测试开发人员将始终在编写代码时牢记不同的场景&#xff0c;但是在某些情况下&#xff0c;实现可能无法按预期工作。 相同的原则也适用于测试代码&#xff0c;该代码主要用于测试现有产品的功能&#xff0c;发现bug并确保产品100&#xff05;不受bug影响。 正确…

MySQL的CRUD操作+使用视图

【0】README0.1&#xff09;本文部分文字描述转自“MySQL 必知必会”&#xff0c;旨在review“MySQL数据检索查询全文本搜索” 的基础知识&#xff1b;【1】插入数据1&#xff09;insert是用来插入&#xff08;或添加&#xff09;行到数据库表的。插入可以用以下几种方式使用&a…

mega x_[MEGA DEAL] 2020年完整的Java Master Class Bundle(96%)

mega x通过超过62个小时的培训来掌握最流行的编程语言&#xff0c;从而树立信誉良好的开发人员职业 嘿&#xff0c;怪胎&#xff0c; 这一周&#xff0c;我们JCG促销专区 &#xff0c;我们有另一个极端的报价 。我们正在提供一个巨大的96&#xff05;off的完整2020 Python编程…

tomcat(11)org.apache.catalina.core.StandardWrapper源码剖析

【0】README0.0&#xff09;本文部分文字描述转自 “how tomcat works”&#xff0c;旨在学习 “tomcat(11)StandardWrapper源码剖析” 的基础知识&#xff1b;0.1&#xff09;StandardWrapper 是 Catalina中对Wrapper接口的标准实现&#xff1b;要知道&#xff0c;tomcat 中有…