Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show how to compare text from two elements #188

Merged
merged 1 commit into from Jan 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/commands/assertions.html
Expand Up @@ -336,6 +336,49 @@ <h3><a href="https://on.cypress.io/should#Function" target="_blank">Should with
})</code></pre>
</div>

<div class="col-xs-12">
<p>
We <a href="https://on.cypress.io/conditional-testing">strongly recommend that your tests are deterministic</a>.
But sometimes you might need to match text between two elements, and you
do not know what that text should be. Save the value from the first element,
then compare it from a <code>should(cb)</code> callback.
</p>
</div>

<div class="col-xs-7">
<pre><code class="javascript">let text
/**
* Normalizes passed text,
* useful before comparing text with spaces and different capitalization.
* @param {string} s Text to normalize
*/
const normalizeText = (s) => s.replace(/\s/g, '').toLowerCase()

cy.get('.two-elements')
.find('.first')
.then(($first) => {
// save text from the first element
text = normalizeText($first.text())
})

cy.get('.two-elements')
.find('.second')
.should(($div) => {
// we can massage text before comparing
const secondText = normalizeText($div.text())
expect(secondText, 'second text').to.equal(text)
})</code></pre>
</div>

<div class="col-xs-5">
<div class="well">
<div class="two-elements">
<div class="first">Foo Bar</div>
<div class="second">foo b a r</div>
</div>
</div>
</div>

<div class="col-xs-12"><hr /></div>
</div>
</div>
Expand Down
30 changes: 30 additions & 0 deletions cypress/integration/examples/assertions.spec.js
Expand Up @@ -124,6 +124,36 @@ context('Assertions', () => {
})
})

it('matches unknown text between two elements', () => {
/**
* Text from the first element.
* @type {string}
*/
let text
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be introducing TypeScript into this repo's example code or tests. This is supposed to support super beginners to our project. I wouldn't assume them to have any TypeScript knowledge.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not typescript, just JSDoc comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😯


/**
* Normalizes passed text,
* useful before comparing text with spaces and different capitalization.
* @param {string} s Text to normalize
*/
const normalizeText = (s) => s.replace(/\s/g, '').toLowerCase()

cy.get('.two-elements')
.find('.first')
.then(($first) => {
// save text from the first element
text = normalizeText($first.text())
})

cy.get('.two-elements')
.find('.second')
.should(($div) => {
// we can massage text before comparing
const secondText = normalizeText($div.text())
expect(secondText, 'second text').to.equal(text)
})
})

it('assert - assert shape of an object', () => {
const person = {
name: 'Joe',
Expand Down