File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed
cypress/integration/examples Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -336,6 +336,49 @@ <h3><a href="https://on.cypress.io/should#Function" target="_blank">Should with
336336 })</ code > </ pre >
337337 </ div >
338338
339+ < div class ="col-xs-12 ">
340+ < p >
341+ We < a href ="https://on.cypress.io/conditional-testing "> strongly recommend that your tests are deterministic</ a > .
342+ But sometimes you might need to match text between two elements, and you
343+ do not know what that text should be. Save the value from the first element,
344+ then compare it from a < code > should(cb)</ code > callback.
345+ </ p >
346+ </ div >
347+
348+ < div class ="col-xs-7 ">
349+ < pre > < code class ="javascript "> let text
350+ /**
351+ * Normalizes passed text,
352+ * useful before comparing text with spaces and different capitalization.
353+ * @param {string} s Text to normalize
354+ */
355+ const normalizeText = (s) => s.replace(/\s/g, '').toLowerCase()
356+
357+ cy.get('.two-elements')
358+ .find('.first')
359+ .then(($first) => {
360+ // save text from the first element
361+ text = normalizeText($first.text())
362+ })
363+
364+ cy.get('.two-elements')
365+ .find('.second')
366+ .should(($div) => {
367+ // we can massage text before comparing
368+ const secondText = normalizeText($div.text())
369+ expect(secondText, 'second text').to.equal(text)
370+ })</ code > </ pre >
371+ </ div >
372+
373+ < div class ="col-xs-5 ">
374+ < div class ="well ">
375+ < div class ="two-elements ">
376+ < div class ="first "> Foo Bar</ div >
377+ < div class ="second "> foo b a r</ div >
378+ </ div >
379+ </ div >
380+ </ div >
381+
339382 < div class ="col-xs-12 "> < hr /> </ div >
340383 </ div >
341384 </ div >
Original file line number Diff line number Diff line change @@ -124,6 +124,36 @@ context('Assertions', () => {
124124 } )
125125 } )
126126
127+ it ( 'matches unknown text between two elements' , ( ) => {
128+ /**
129+ * Text from the first element.
130+ * @type {string }
131+ */
132+ let text
133+
134+ /**
135+ * Normalizes passed text,
136+ * useful before comparing text with spaces and different capitalization.
137+ * @param {string } s Text to normalize
138+ */
139+ const normalizeText = ( s ) => s . replace ( / \s / g, '' ) . toLowerCase ( )
140+
141+ cy . get ( '.two-elements' )
142+ . find ( '.first' )
143+ . then ( ( $first ) => {
144+ // save text from the first element
145+ text = normalizeText ( $first . text ( ) )
146+ } )
147+
148+ cy . get ( '.two-elements' )
149+ . find ( '.second' )
150+ . should ( ( $div ) => {
151+ // we can massage text before comparing
152+ const secondText = normalizeText ( $div . text ( ) )
153+ expect ( secondText , 'second text' ) . to . equal ( text )
154+ } )
155+ } )
156+
127157 it ( 'assert - assert shape of an object' , ( ) => {
128158 const person = {
129159 name : 'Joe' ,
You can’t perform that action at this time.
0 commit comments