Skip to content

Commit

Permalink
feat: add attribute with a dot example
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 3, 2021
1 parent 13ac0b9 commit 35152af
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/commands/assertions.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,32 @@ cy.get('[data-cy=subject-example]') // jQuery element

<!-- fiddle-end -->

#### Escape special characters

Sometimes an attribute can have a special character like `.` or `:` in it. If you are just checking the attribute name, you do not need to escape them.

<!-- fiddle Implicit Assertions / .should() - have.attr with a dot -->

```html
<div id="escape-attribute" attr.aria-label="Attribute example">
Example
</div>
```

```js
// confirm the element has the attribute
cy.get('#escape-attribute').should('have.attr', 'attr.aria-label')
// confirm the element has the attribute and that attribute
// has the specific value
cy.get('#escape-attribute').should(
'have.attr',
'attr.aria-label',
'Attribute example',
)
```

<!-- fiddle-end -->

#### `have.attr` assertion chain

<!-- fiddle Implicit Assertions / .should() - have.attr matching part of the string -->
Expand Down
22 changes: 22 additions & 0 deletions docs/commands/querying.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ cy.get('#specific-href a[href="index.html"]')

<!-- fiddle-end -->

### Escape the attribute

Sometimes an attribute can have a special character like `.` or `:` in it. Please escape the attribute using the `\\` character.

<!-- fiddle cy.get / escape special characters in attribute -->

```html
<div id="escape-attribute" attr.aria-label="Attribute example">
Example
</div>
```

```js
cy.get('[attr\\.aria-label="Attribute example"]')
.should('have.id', 'escape-attribute')
// ignore the newline characters by using the assertion "include.text"
// rather than the assertion "have.text"
.and('include.text', 'Example')
```

<!-- fiddle-end -->

### Attribute prefix

Let's get the element with ID starting with "local-example" prefix
Expand Down

0 comments on commit 35152af

Please sign in to comment.