Skip to content

Commit c608c2c

Browse files
authored
Merge pull request #423 from magento-devdocs/db_selectors
Putting selectors.md back
2 parents 8e2fa00 + f5e0799 commit c608c2c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

docs/selectors.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Selectors
2+
3+
These guidelines should help you to write high quality selectors.
4+
5+
### Selectors SHOULD be written in CSS instead of XPath whenever possible
6+
7+
CSS is generally easier to read than XPath. For example, `//*[@id="foo"]` in XPath can be expressed as simply as `#foo` in CSS.
8+
See this [XPath Cheatsheet](https://devhints.io/xpath) for more examples.
9+
10+
### XPath selectors SHOULD NOT use `@attribute="foo"`.
11+
12+
This would fail if the attribute was `attribute="foo bar"`.
13+
Instead you SHOULD use `contains(@attribute, "foo")` where `@attribute` is any valid attribute such as `@text` or `@class`.
14+
15+
### CSS and XPath selectors SHOULD be implemented in their most simple form
16+
17+
* <span class="color:green">GOOD:</span> `#foo`
18+
* <span class="color:red">BAD:</span> `button[contains(@id, "foo")]`
19+
20+
### CSS and XPath selectors SHOULD avoid making use of hardcoded indices
21+
22+
Instead you SHOULD parameterize the selector.
23+
24+
* <span class="color:green">GOOD:</span> `.foo:nth-of-type({{index}})`
25+
* <span class="color:red">BAD:</span> `.foo:nth-of-type(1)`
26+
27+
* <span class="color:green">GOOD:</span> `button[contains(@id, "foo")][{{index}}]`
28+
* <span class="color:red">BAD:</span> `button[contains(@id, "foo")][1]`
29+
30+
* <span class="color:green">GOOD:</span> `#actions__{{index}}__aggregator`
31+
* <span class="color:red">BAD:</span> `#actions__1__aggregator`
32+
33+
### CSS and XPath selectors MUST NOT reference the `@data-bind` attribute
34+
35+
The `@data-bind` attribute is used by KnockoutJS, a framework Magento uses to create dynamic Javascript pages. Since this `@data-bind` attribute is tied to a specific framework, it should not be used for selectors. If Magento decides to use a different framework then these `@data-bind` selectors would break.

0 commit comments

Comments
 (0)