Skip to content

Commit ac6bad7

Browse files
authored
Merged content from selectors.md
1 parent 44195cb commit ac6bad7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/guides/selectors.md

+24
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,30 @@ This selector is too brittle. It would break very easily if an application devel
110110
111111
This is the same selector as above, but represented in XPath instead of CSS. It is brittle for the same reasons.
112112
113+
#### XPath selectors should not use @attribute="foo"
114+
115+
This XPath is fragile. It would fail if the attribute was `attribute="foo bar"`. Instead you should use `contains(@attribute, "foo")` where @attribute is any valid attribute such as @text or @class.
116+
117+
#### CSS and XPath selectors should avoid making use of hardcoded indices
118+
119+
Hardcoded values are by definition not flexible. A hardcoded index may change if new code is introduced. Instead, parameterize the selector.
120+
121+
GOOD: .foo:nth-of-type({{index}})
122+
123+
BAD: .foo:nth-of-type(1)
124+
125+
GOOD: button[contains(@id, "foo")][{{index}}]
126+
127+
BAD: button[contains(@id, "foo")][1]
128+
129+
GOOD: #actions__{{index}}__aggregator
130+
131+
BAD: #actions__1__aggregator
132+
133+
#### CSS and XPath selectors MUST NOT reference the @data-bind attribute
134+
135+
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.
136+
113137
#### Use isolation
114138
115139
You should think in terms of "isolation" when writing new selectors.

0 commit comments

Comments
 (0)