Skip to content

Commit be6b819

Browse files
authored
Editorial review. Grammar and formatting.
1 parent dbcb122 commit be6b819

File tree

1 file changed

+63
-56
lines changed

1 file changed

+63
-56
lines changed

docs/guides/selectors.md

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,136 @@
1-
# How To Write Good Selectors
1+
# How To write good selectors
22

3-
Selectors are an atomic unit of test writing. They fit into the hierarchy like this: MFTF tests make use of action groups, which are made up of actions, which interact with page objects, which contain elements, which are specified by selectors. Because they are the building blocks, we must take care when writing them.
3+
Selectors are the atomic unit of test writing. They fit into the hierarchy like this: MFTF tests make use of action groups > which are made up of actions > which interact with page objects > which contain elements > which are specified by selectors. Because they are fundamental building blocks, we must take care when writing them.
44

55
## What is a selector?
66

7-
A "selector" is like an address to an element in the Document Object Model (DOM). It locates those elements and allows MFTF to interact with them. By element we mean things such as input fields, buttons, tables, divs, etc. By interact we mean actions such as click, fill field, etc.
7+
A "selector" works like an address to an element in the Document Object Model (DOM). It specifies page elements and allows MFTF to interact with them.
8+
By 'element' we mean things such as input fields, buttons, tables, divs, etc.
9+
By 'interact' we mean actions such as click, fill field, etc.
810

911
Selectors live inside of MFTF page objects and are meant to be highly re-usable amongst all tests. They can be written in either CSS or XPath.
1012

1113
## Why are good selectors important?
1214

13-
Good selectors are important because they are the most re-used component of functional testing. They are the lowest building blocks of tests, the foundation. If they are unstable then everything else built on top of them will inherit that instability.
15+
Good selectors are important because they are the most re-used component of functional testing. They are the lowest building blocks of tests; the foundation. If they are unstable then everything else built on top of them will inherit that instability.
1416

1517
## How do I write good selectors?
1618

17-
We could cover this subject with an infinite amount of documentation and some lessons only come from experience. But this guide will explain some DOs and DONTs to help you along the way towards mastery.
19+
We could cover this subject with an infinite amount of documentation and some lessons only come from experience. This guide explains some DOs and DONTs to help you along the way towards selector mastery.
1820

1921
### Inspecting the DOM
2022

21-
To write a selector you need to be able to see the DOM and find the element within it. Fortunately you don't have to look at the entire DOM every time. Nor do you have to read it from top to bottom. Instead you can make use of your browsers built in developer tools or go a step further and try out some popular browser extensions.
23+
To write a selector you need to be able to see the DOM and find the element within it. Fortunately you do not have to look at the entire DOM every time. Nor do you have to read it from top to bottom. Instead you can make use of your browsers built-in developer tools or go a step further and try out some popular browser extensions.
2224

2325
See these links for more information about built-in browser developer tools:
24-
* [Chrome Developer Tools](https://developers.google.com/web/tools/chrome-devtools/)
25-
* [Firefox Developer Tools](https://developer.mozilla.org/en-US/docs/Tools)
26+
27+
* [Chrome Developer Tools](https://developers.google.com/web/tools/chrome-devtools/)
28+
* [Firefox Developer Tools](https://developer.mozilla.org/en-US/docs/Tools)
2629

2730
See these links for common browser addons that may offer advantages over browser developer tools:
28-
* [Live editor for CSS, Less & Sass - Magic CSS](https://chrome.google.com/webstore/detail/live-editor-for-css-less/ifhikkcafabcgolfjegfcgloomalapol?hl=en)
29-
* [XPath Helper](https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en)
31+
32+
* [Live editor for CSS, Less & Sass - Magic CSS](https://chrome.google.com/webstore/detail/live-editor-for-css-less/ifhikkcafabcgolfjegfcgloomalapol?hl=en)
33+
* [XPath Helper](https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en)
3034

3135
### CSS vs XPath
3236

33-
There are many similarities and many differences between CSS and XPath. It is too much to cover in this guide alone. You should search out additional material on your own. In general:
37+
There are similarities and differences between CSS and XPath. Both are powerful and complex in ways that are outside of the scope of this document.
38+
In general:
3439

35-
* CSS is more stable, easier to read, and easier to maintain (typically).
36-
* XPath provides several powerful tools and it has been around the longest so it’s well documented.
37-
* XPath can be less stable and potentially unsupported by certain actions in Selenium.
40+
* CSS is more stable, easier to read, and easier to maintain (typically).
41+
* XPath provides several powerful tools and it has been around the longest so it is well documented.
42+
* XPath can be less stable and potentially unsupported by certain actions in Selenium.
3843

3944
### Priority
4045

41-
The best and most simple selector will always be `#some-id-here`. If only we were so lucky to have this every time. When writing selectors, you should prioritize looking for these starting points to build your selector from.
46+
The best and most simple selector will always be to use an element ID: `#some-id-here`. If only we were so lucky to have this every time. When writing selectors, you should prioritize looking for these starting points to build your selector from.
4247

43-
1. ID, Name, Class, or anything else that is unique to the element
48+
1. ID, name, class, or anything else that is unique to the element
4449
2. Complex CSS selectors
4550
3. XPath selectors
46-
4. If none of the above work for you, then the last resort is to ask a developer to add a unique ID or Class to the element you're trying to select.
51+
4. If none of the above work for you, then the last resort is to ask a developer to add a unique ID or class to the element you are trying to select.
4752

48-
Important: Notice we prefer the use of CSS selectors above XPath selectors when possible.
53+
We suggest the use of CSS selectors above XPath selectors when possible.
4954

50-
### Do's and Don'ts
55+
### Do and Do not
5156

52-
Let's start with the Don'ts because they're more important.
57+
There are correct ways of writing selectors and incorrect ways. These suggestions will help you write better selectors.
5358

54-
#### Don't #1
59+
#### Incorrect - copy selector/xpath
5560

5661
DO NOT right click on an element in your browser developer tools and select "Copy selector" or "Copy XPath" and simply use that as your selector. These auto-generated selectors are prime examples of what not to do.
5762

5863
These are bad:
5964

60-
```
65+
```css
6166
#html-body > section > div > div > div > div
6267
```
6368

64-
```
69+
```xpath
6570
//*[@id='html-body']/section/div/div/div/div
6671
```
6772
68-
They both include unnecessary hierarchical details. As written, we're looking for a `div` inside of a `div` inside of a `div` inside of... you get the picture. But if an application developer adds another `div` parent tomorrow, even for stylistic reasons, then this selector will break. Furthermore, when reading it, it's not even clear what was the intended target in the first place.
73+
Both include unnecessary hierarchical details. As written, we are looking for a `div` inside of a `div` inside of a `div` inside of... you get the picture. If an application developer adds another `div` parent tomorrow, for whatever reason, this selector will break. Furthermore, when reading it, it is not clear what the intended target is. It may also grab other elements that were not intended.
6974
70-
#### Don't #2
75+
#### Do not be too general
7176
72-
DO NOT make your selectors too generic. If a selector is too generic, there is a high probability that it will match multiple elements on the page. Maybe not today. But probably tomorrow when the application under test changes.
77+
DO NOT make your selectors too generic. If a selector is too generic, there is a high probability that it will match multiple elements on the page. Maybe not today, but perhaps tomorrow when the application being tested changes.
7378
7479
These are bad:
7580
76-
```
81+
```html
7782
input[name*='firstname']
7883
```
7984
80-
The `*=` means `contains`. So the selector is saying find an input whose name contains the string "firstname". But if a future change adds a new element to the page whose name also contains "firstname", then this selector will now match two elements and that's bad.
85+
The `*=` means `contains`. The selector is saying 'find an input whose name contains the string "firstname"'. But if a future change adds a new element to the page whose name also contains "firstname", then this selector will match two elements and that is bad.
8186
82-
```
87+
```css
8388
.add
8489
```
8590
86-
Similarly here, this will match any element which contains the class `.add`. This is brittle and susceptible to breaking when new elements/styles are added to the page.
91+
Similarly here, this will match all elements which contains the class `.add`. This is brittle and susceptible to breaking when new elements/styles are added to the page.
8792
88-
#### Don't #3
93+
#### Being too specific
8994
90-
DO NOT make your selectors too specific either. If a selector is too specific, there is a high probability that it will break due to even minor changes to the application under test.
95+
DO NOT make your selectors too specific either. If a selector is too specific, there is a high probability that it will break due to even minor changes to the application being tested.
9196
9297
These are bad:
9398
94-
```
99+
```css
95100
#container .dashboard-advanced-reports .dashboard-advanced-reports-description .dashboard-advanced-reports-title
96101
```
97102
98103
This selector is too brittle. It would break very easily if an application developer does something as simple as adding a parent container for style reasons.
99104
100-
```
105+
```xpath
101106
//*[@id='container']/*[@class='dashboard-advanced-reports']/*[@class='dashboard-advanced-reports-description']/*[@class='dashboard-advanced-reports-title']
102107
```
103108
104109
This is the same selector as above, but represented in XPath instead of CSS. It is brittle for the same reasons.
105110
106-
#### Do #1
111+
#### Use isolation
107112
108113
You should think in terms of "isolation" when writing new selectors.
109114
110-
For example, let's say you have a login form that contains a username field, a password field, and a Sign In button. First isolate the parent element. Perhaps it's `#login-form`. Then target the child element under that parent element: `.sign-in-button` The result is `#login-form .sign-in-button`.
115+
For example, say you have a login form that contains a username field, a password field, and a 'Sign In' button. First isolate the parent element. Perhaps it's `#login-form`. Then target the child element under that parent element: `.sign-in-button` The result is `#login-form .sign-in-button`.
111116
112-
Thinking like this will reduce the amount of DOM that you need to worry about.
117+
Using isolation techniques reduces the amount of DOM that needs to be processed. This makes the selector both accurate and efficient.
113118
114-
#### Do #2
119+
#### Use advanced notation
115120
116-
If you need to interact with the parent element but it's too generic, and the internal contents are unique then you need to:
121+
If you need to interact with the parent element but it is too generic, and the internal contents are unique then you need to:
117122
118-
1. Target the unique internal contents first
119-
2. Then jump to the parent element using `::parent`
123+
1. Target the unique internal contents first.
124+
1. Then jump to the parent element using `::parent`.
120125
121-
For example let's imagine you want to find a table row that contains the string "Jerry Seinfeld". You can use the following XPath selector:
126+
Imagine you want to find a table row that contains the string "Jerry Seinfeld". You can use the following XPath selector:
122127
123-
```
128+
```xpath
124129
//div[contains(text(), 'Jerry Seinfeld')]/parent::td/parent::tr
125130
```
126131
132+
Note in this instance that CSS does not have an equivalent to `::parent`, so XPath is a better choice.
133+
127134
### CSS Examples
128135
129136
Examples of common HTML elements and the corresponding selector to find that element in the DOM:
@@ -148,7 +155,7 @@ Whitespace|Descendant Combinator|Allows you to combine 2 or more selectors.|`#id
148155
`+`|Adjacent Sibling Combinator|Allows you to select an element THAT FOLLOWS DIRECTLY AFTER another specified element.|`#idname + .classname`
149156
`~`|General Sibling Combinator|Allows you to select an element THAT FOLLOWS (directly or indirectly) another specified element.|`#idname ~ .classname`
150157
151-
Examples of CSS attribute operators and their purpose
158+
Examples of CSS attribute operators and their purpose:
152159
153160
Symbol|Purpose|Example
154161
---|---|---
@@ -157,29 +164,29 @@ Symbol|Purpose|Example
157164
`~=`|Returns all elements that CONTAINS the given words delimited by spaces in the value.|`[attribute~='value']`
158165
`$=`|Returns all elements that ENDS WITH the substring in the value.|`[attribute$='value']`
159166
`^=`|Returns all elements that BEGIN EXACTLY WITH the substring in the value.|`[attribute^='value']`
160-
`!=`|Returns all elements that either DOESN’T HAVE the given attribute or the value of the attribute is NOT EQUAL to the value.|`[attribute!='value']`
167+
`!=`|Returns all elements that either DOES NOT HAVE the given attribute or the value of the attribute is NOT EQUAL to the value.|`[attribute!='value']`
161168
162169
### XPath Examples
163170
164171
#### `/` vs `//`
165172
166173
The absolute XPath selector is a single forward slash `/`. It is used to provide a direct path to the element from the root element.
167174
168-
WARNING: The `/` selector is brittle and should only be used sparingly.
175+
WARNING: The `/` selector is brittle and should be used sparingly.
169176
170-
Here's an example of what NOT to do, but this demonstrates how the selector works:
177+
Here is an example of what NOT to do, but this demonstrates how the selector works:
171178
172-
```
179+
```xpath
173180
/html/body/div[2]/div/div[2]/div[1]/div[2]/form/div/input
174181
```
175182
176-
In the BAD example above we are specifying a very precise path to an input element in the DOM.
183+
In the BAD example above, we are specifying a very precise path to an input element in the DOM, starting from the very top of the document.
177184
178185
Similarly, the relative XPath selector is a double forward slash `//`. It is used to start searching for an element anywhere in the DOM.
179186
180187
Example:
181188
182-
```
189+
```xpath
183190
//div[@class=’form-group’]//input[@id='user-message']
184191
```
185192
@@ -191,7 +198,7 @@ Example #1:
191198
192199
Given this HTML:
193200
194-
```
201+
```html
195202
<tr>
196203
<td>
197204
<div>Unique Value</div>
@@ -201,15 +208,15 @@ Given this HTML:
201208
202209
We can locate the `<tr>` element with this selector:
203210
204-
```
211+
```xpath
205212
//*[text()='Unique Value']/../..
206213
```
207214
208215
Example #2:
209216
210217
Given this HTML:
211218
212-
```
219+
```html
213220
<tr>
214221
<td>
215222
<a href=“#”>Edit</a>
@@ -222,13 +229,13 @@ Given this HTML:
222229
223230
We can locate the `<a>` element with this selector:
224231
225-
```
232+
```xpath
226233
//div[text()='Unique Value']/../..//a
227234
```
228235
229236
#### Attribute Selectors
230237
231-
Attribute selectors allow you to select an element that match a specific attribute value.
238+
Attribute selectors allow you to select elements that match a specific attribute value.
232239
233240
Examples:
234241
@@ -243,7 +250,7 @@ src|`<img src='/img.png'/>`|`//*[@src='/img.png']`
243250
244251
#### `contains()` Selector
245252
246-
The `contains()` selector allows you to select an element that contains an attribute value search string.
253+
The `contains()` selector allows you to select an element that contains an attribute value string.
247254
248255
Examples:
249256

0 commit comments

Comments
 (0)