测试自动化的HTML元素选择。元素ID或数据属性 [英] Test automation html element selectors. Element ID or DataAttribute

查看:384
本文介绍了测试自动化的HTML元素选择。元素ID或数据属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在把一些ID的对UI测试自动化元素。这些标识的仅被用于测试。我应该添加数据的属性,而不是可能使它更具可读性的未来开发(数据testHandle =myButton的),或者我应该坚持的ID。

I'm currently placing some ID's on elements for UI Test automation. These ID's are only being used for testing. Should I be adding data-attributes instead possibly making it more readable to future developers(data-testHandle="mybutton") or should I stick with ID's.

w3.org说:

自定义数据属性是用来存储自定义数据的私有
  网页或应用,其中没有更合适
  属性或元素

Custom data attributes are intended to store custom data private to the page or application, for which there are no more appropriate attributes or elements.

我倾向于保持IDS,但是我的某些部分认为,未来开发商会认为的没有使用的ID并删除它们。

I'm leaning towards keeping ids but some part of me thinks that future developers would think the ID's aren't used and remove them.

在这里的最佳做法。谢谢你。

Any best practices here. Thanks.

推荐答案

这是接近于意见为主,通过这里是应该帮助做出选择的总结。

This is close to being opinion-based, by here is the summary that should help to make a choice.

为什么你会使用 ID 属性:

Why would you use an ID attribute:


  • 这是一个的通用的和熟悉的给大家做测试自动化的的方式来定位元素

  • 这是一般的最快的方式来定位页面上的元素,因为硒得到它归结为执行的document.getElementById()经过优化由现代浏览器(虽然,通常是终端到终端的UI测试的性能并不重要)

  • 这是一个的内置的定位在每个硒语言结合

  • 如果你使用Firebug或Chrome开发人员工具 - CSS选择器和XPath生成工具通常会提供使用 ID S中的元素尽可能<中/更强大的定位器LI>
  • 您将建立短CSS选择器和XPath前pressions。例如。 #myid .someclass ,而不是 [自动化ID =本身份识别码] .someclass

  • this is a common and familiar to everybody doing test automation way to locate elements
  • this is generally the fastest way to locate elements on a page because selenium gets it down to executing document.getElementById() which is optimized by the modern browsers (though, usually performance of the end-to-end UI tests is not critical)
  • it is a built-in locator in every selenium language binding
  • if you would use Firebug or Chrome Developer Tools - the CSS selector and XPath generation tools would generally provide more robust locators using the ids of the element whenever possible
  • you would build shorter CSS selectors and XPath expressions. E.g. #myid .someclass as opposed to [automation-id=myid] .someclass.

为什么你会使用自定义属性:


  • 如果你想补充,比方说,自动化-ID 属性所有所需的元素,你会有些空间/它范围的自动化测试 - 每个人都知道什么是这对于刚刚从属性名称。意思是,你会大大降低有意改变属性的开发商的机会,而不是一个 ID 属性,它可以和通常用于应用程序客户端逻辑,以及(参考来这个接听)

  • if you would add, say, automation-id attributes to all the desired elements, you would somewhat namespace/scope it to the test automation - everybody would know what is this for just from the attribute name. Meaning, you would dramatically decrease chances of a developer changing the attribute intentionally as opposed to an id attribute, which can and is usually used for application client-side logic as well (reference to this and this answer)

另外,这里有一些相关的主题:

Also, here are some relevant threads:

  • Is adding IDs to everything standard practice when using Selenium?
  • Which is the best and fastest way to find the element using webdriver? By.XPath or By.ID or anything else? And why?
  • Something Better than IDs for Identifying Elements in Selenium Tests

这篇关于测试自动化的HTML元素选择。元素ID或数据属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆