我应该使用 id 来定位元素吗?
Should I use ids to locate elements?
从 Angular 和量角器开始。
编写一些沉重的 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.
此外,通常尝试使用 "data-oriented" 方法 :by.model
、by.binding
、by.repeater
定位器,或如果您依赖 class 名称,请明智地选择它们:不要使用像 .col-xs-4
或 .container-fluid
.
这样面向布局的 classes
另请参阅这些相关主题:
- Best Practices for Watir and Selenium Locators
- best way to detect an element on a web page for seleniumRC in java
从 Angular 和量角器开始。
编写一些沉重的 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.
此外,通常尝试使用 "data-oriented" 方法 :by.model
、by.binding
、by.repeater
定位器,或如果您依赖 class 名称,请明智地选择它们:不要使用像 .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