gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:10:42 25 4
gpt4 key购买 nike

从 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
  • 关于angularjs - 我应该使用 id 来定位元素吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30864066/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com