加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 我应该使用id来定位元素吗?

发布时间:2020-12-17 10:20:50 所属栏目:安全 来源:网络整理
导读:开始使用Angular和Protractor. 编写一些重要的css选择器会让你感觉不对劲,这些选择器会在你改变某些东西时立即中断. 使用ID会使测试更容易. 我还没有使用任何id属性进行样式设置.使用ID进行测试是否有任何缺点我没有考虑过? 一般规则是尽可能使用ID,假设它
开始使用Angular和Protractor.

编写一些重要的css选择器会让你感觉不对劲,这些选择器会在你改变某些东西时立即中断.
使用ID会使测试更容易.

我还没有使用任何id属性进行样式设置.使用ID进行测试是否有任何缺点我没有考虑过?

一般规则是尽可能使用ID,假设它们在DOM中是唯一的,而不是动态生成的. Quoting Jim Holmes:

Whenever possible,use ID attributes. If the page is valid HTML,then
IDs are unique on the page. They’re extraordinarily fast for
resolution in every browser,and the UI can change dramatically but
your script will still locate the element.

Sometimes IDs aren’t the right choice. Dynamically generated IDs are
almost always the wrong choice when you’re working with something like
a grid control. You rely on an id that is likely tied to the specific
row position and then you’re screwed if your row changes.

此外,通常尝试使用“面向数据”的方法:by.model,by.binding,by.repeater定位器,或者如果依赖类名,请明智地选择它们:不要使用像.col这样的面向布局的类-xs-4或.container-fluid.

另请参阅以下相关主题:

> Best Practices for Watir and Selenium Locators
> best way to detect an element on a web page for seleniumRC in java

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读