diff --git a/app/commands/assertions.html b/app/commands/assertions.html index 863e155c8..39dfe92d0 100644 --- a/app/commands/assertions.html +++ b/app/commands/assertions.html @@ -336,6 +336,49 @@
+ We strongly recommend that your tests are deterministic.
+ 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 should(cb)
callback.
+
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)
+})
+