Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
add non-breaking space tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 17, 2020
1 parent 649a8bb commit 9dec37d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions cypress/integration/non-breaking-space.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Non-breaking space

Dealing with ` ` character aka [Non-breaking space](https://en.wikipedia.org/wiki/Non-breaking_space).

When using [cy.contains](https://on.cypress.io/contains) command Cypress automatically converts the ` ` entity into space character. Thus `cy.contains` finds element that include ` `.

<!-- fiddle Without whitespace -->
```html
<div data-testid="testattr">
<div><span>GBP 0.50</span></div>
</div>
```

```js
// both commands work
cy.contains ('[data-testid=testattr]', 'GBP 0.50').should('be.visible')
cy.get('[data-testid=testattr]')
.filter(':contains("GBP 0.50")')
.should('have.length', 1)
```
<!-- fiddle-end -->

But if the HTML contains the whitespace special character `&nbsp;` then checking elements by text is tricky. See [#9530](https://github.com/cypress-io/cypress/issues/9530) for example. This is because [.filter](https://on.cypress.io/filter) uses jQuery [:contains selector](https://api.jquery.com/contains-selector/) which uses literal text match.

<!-- fiddle With whitespace / cy.contains works -->
```html
<div data-testid="testattr">
<div><span>GBP&nbsp;0.50</span></div>
</div>
```

```js
cy.contains ('[data-testid=testattr]', 'GBP 0.50').should('be.visible')
```
<!-- fiddle-end -->

<!-- fiddle With whitespace / use Unicode in .filter -->
```html
<div data-testid="testattr">
<div><span>GBP&nbsp;0.50</span></div>
</div>
```

```js
// we can filter by text before the &nbsp;
cy.get('[data-testid=testattr]')
.filter(':contains("GBP")')
.should('have.length', 1)

cy.get('[data-testid=testattr]')
// the following filter DOES NOT WORK
// .filter(':contains("GBP 0.50")')
// the following filter DOES NOT WORK
// .filter(':contains("GBP&nbsp;0.50")')
// but the Unicode representation of non-breaking space character works
.filter(':contains("GBP\u00a00.50")')
.should('have.length', 1)
```
<!-- fiddle-end -->

## Result

![Non-breaking space tests](../../images/non-breaking-space.png)
Binary file added images/non-breaking-space.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9dec37d

Please sign in to comment.