@@ -51,7 +51,7 @@ describe(toHaveText, async () => {
5151 } )
5252 } )
5353
54- test ( 'wait but failure ' , async ( ) => {
54+ test ( 'wait but error ' , async ( ) => {
5555 vi . mocked ( el . getText ) . mockRejectedValue ( new Error ( 'some error' ) )
5656
5757 await expect ( ( ) => thisContext . toHaveText ( el , 'WebdriverIO' , { ignoreCase : true , wait : 500 } ) )
@@ -68,7 +68,7 @@ describe(toHaveText, async () => {
6868 test ( 'success on the first attempt' , async ( ) => {
6969 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
7070
71- const result = await thisContext . toHaveText ( el , 'WebdriverIO' , { ignoreCase : true , wait : 0 } )
71+ const result = await thisContext . toHaveText ( el , 'WebdriverIO' , { ignoreCase : true } )
7272 expect ( result . pass ) . toBe ( true )
7373 expect ( el . getText ) . toHaveBeenCalledTimes ( 1 )
7474 } )
@@ -79,6 +79,12 @@ describe(toHaveText, async () => {
7979 const result = await thisContext . toHaveText ( el , 'WebdriverIO' , { wait : 0 } )
8080
8181 expect ( result . pass ) . toBe ( false )
82+ expect ( result . message ( ) ) . toEqual ( `\
83+ Expect $(\`sel\`) to have text
84+
85+ Expected: "WebdriverIO"
86+ Received: "webdriverio"`
87+ )
8288 expect ( el . getText ) . toHaveBeenCalledTimes ( 1 )
8389 } )
8490
@@ -124,55 +130,55 @@ Received : "WebdriverIO"`)
124130 test ( 'should return true if texts strictly match without trimming' , async ( ) => {
125131 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
126132
127- const result = await thisContext . toHaveText ( el , 'WebdriverIO' , { trim : false , wait : 0 } )
133+ const result = await thisContext . toHaveText ( el , 'WebdriverIO' , { trim : false } )
128134
129135 expect ( result . pass ) . toBe ( true )
130136 } )
131137
132138 test ( 'should return true if actual text + single replacer matches the expected text' , async ( ) => {
133139 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
134140
135- const result = await thisContext . toHaveText ( el , 'BrowserdriverIO' , { wait : 0 , replace : [ 'Web' , 'Browser' ] } )
141+ const result = await thisContext . toHaveText ( el , 'BrowserdriverIO' , { replace : [ 'Web' , 'Browser' ] } )
136142
137143 expect ( result . pass ) . toBe ( true )
138144 } )
139145
140146 test ( 'should return true if actual text + replace (string) matches the expected text' , async ( ) => {
141147 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
142148
143- const result = await thisContext . toHaveText ( el , 'BrowserdriverIO' , { wait : 0 , replace : [ [ 'Web' , 'Browser' ] ] } )
149+ const result = await thisContext . toHaveText ( el , 'BrowserdriverIO' , { replace : [ [ 'Web' , 'Browser' ] ] } )
144150
145151 expect ( result . pass ) . toBe ( true )
146152 } )
147153
148154 test ( 'should return true if actual text + replace (regex) matches the expected text' , async ( ) => {
149155 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
150156
151- const result = await thisContext . toHaveText ( el , 'BrowserdriverIO' , { wait : 0 , replace : [ [ / W e b / , 'Browser' ] ] } )
157+ const result = await thisContext . toHaveText ( el , 'BrowserdriverIO' , { replace : [ [ / W e b / , 'Browser' ] ] } )
152158
153159 expect ( result . pass ) . toBe ( true )
154160 } )
155161
156162 test ( 'should return true if actual text starts with expected text' , async ( ) => {
157163 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
158164
159- const result = await thisContext . toHaveText ( el , 'Web' , { wait : 0 , atStart : true } )
165+ const result = await thisContext . toHaveText ( el , 'Web' , { atStart : true } )
160166
161167 expect ( result . pass ) . toBe ( true )
162168 } )
163169
164170 test ( 'should return true if actual text ends with expected text' , async ( ) => {
165171 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
166172
167- const result = await thisContext . toHaveText ( el , 'IO' , { wait : 0 , atEnd : true } )
173+ const result = await thisContext . toHaveText ( el , 'IO' , { atEnd : true } )
168174
169175 expect ( result . pass ) . toBe ( true )
170176 } )
171177
172178 test ( 'should return true if actual text contains the expected text at the given index' , async ( ) => {
173179 vi . mocked ( el . getText ) . mockResolvedValue ( 'WebdriverIO' )
174180
175- const result = await thisContext . toHaveText ( el , 'iverIO' , { wait : 0 , atIndex : 5 } )
181+ const result = await thisContext . toHaveText ( el , 'iverIO' , { atIndex : 5 } )
176182
177183 expect ( result . pass ) . toBe ( true )
178184 } )
@@ -194,7 +200,7 @@ Received: ""`
194200
195201 vi . mocked ( el . getText ) . mockResolvedValue ( 'webdriverio' )
196202
197- const result = await thisContext . toHaveText ( el , [ 'WDIO' , 'Webdriverio' ] , { wait : 0 , ignoreCase : true } )
203+ const result = await thisContext . toHaveText ( el , [ 'WDIO' , 'Webdriverio' ] , { ignoreCase : true } )
198204 expect ( result . pass ) . toBe ( true )
199205 expect ( el . getText ) . toHaveBeenCalledTimes ( 1 )
200206 } )
@@ -203,7 +209,7 @@ Received: ""`
203209
204210 vi . mocked ( el . getText ) . mockResolvedValue ( ' WebdriverIO ' )
205211
206- const result = await thisContext . toHaveText ( el , [ 'WDIO' , 'WebdriverIO' , 'toto' ] , { wait : 0 , trim : true } )
212+ const result = await thisContext . toHaveText ( el , [ 'WDIO' , 'WebdriverIO' , 'toto' ] , { trim : true } )
207213
208214 expect ( result . pass ) . toBe ( true )
209215 expect ( el . getText ) . toHaveBeenCalledTimes ( 1 )
@@ -379,7 +385,7 @@ Received: "This is example text"`
379385 } )
380386
381387 test ( 'should return true if the received element array matches the expected text array & ignoreCase' , async ( ) => {
382- const result = await thisContext . toHaveText ( els , 'webdriverio' , { ignoreCase : true , wait : 0 } )
388+ const result = await thisContext . toHaveText ( els , 'webdriverio' , { ignoreCase : true } )
383389 expect ( result . pass ) . toBe ( true )
384390 } )
385391
@@ -388,6 +394,50 @@ Received: "This is example text"`
388394 expect ( result . pass ) . toBe ( false )
389395 } )
390396
397+ test ( 'should return false when first element does not match' , async ( ) => {
398+ vi . mocked ( ( await els ) [ 0 ] . getText ) . mockResolvedValueOnce ( 'Wrong' )
399+ vi . mocked ( ( await els ) [ 1 ] . getText ) . mockResolvedValueOnce ( 'webdriverio' )
400+
401+ const result = await thisContext . toHaveText ( els , 'webdriverio' , { message : 'Test' , wait : 0 } )
402+
403+ expect ( result . pass ) . toBe ( false )
404+ expect ( result . message ( ) ) . toEqual ( `\
405+ Test
406+ Expect ${ selectorName } to have text
407+
408+ - Expected - 1
409+ + Received + 1
410+
411+ Array [
412+ - "webdriverio",
413+ + "Wrong",
414+ "webdriverio",
415+ ]`
416+ )
417+ } )
418+
419+ test ( 'should return false when second element does not match' , async ( ) => {
420+ vi . mocked ( ( await els ) [ 0 ] . getText ) . mockResolvedValueOnce ( 'webdriverio' )
421+ vi . mocked ( ( await els ) [ 1 ] . getText ) . mockResolvedValueOnce ( 'Wrong' )
422+
423+ const result = await thisContext . toHaveText ( els , 'webdriverio' , { message : 'Test' , wait : 0 } )
424+
425+ expect ( result . pass ) . toBe ( false )
426+ expect ( result . message ( ) ) . toEqual ( `\
427+ Test
428+ Expect ${ selectorName } to have text
429+
430+ - Expected - 1
431+ + Received + 1
432+
433+ Array [
434+ "webdriverio",
435+ - "webdriverio",
436+ + "Wrong",
437+ ]`
438+ )
439+ } )
440+
391441 test ( 'should shows custom failure message' , async ( ) => {
392442 const result = await thisContext . toHaveText ( els , 'webdriverio' , { message : 'Test' , wait : 0 } )
393443
@@ -435,7 +485,7 @@ Expect ${selectorName} to have text
435485 } )
436486
437487 test ( 'should return true if the received element array matches the expected text array & ignoreCase' , async ( ) => {
438- const result = await thisContext . toHaveText ( els , [ 'webdriverio' , 'get started' ] , { ignoreCase : true , wait : 0 } )
488+ const result = await thisContext . toHaveText ( els , [ 'webdriverio' , 'get started' ] , { ignoreCase : true } )
439489 expect ( result . pass ) . toBe ( true )
440490 } )
441491
@@ -490,6 +540,39 @@ Expect ${selectorName} to have text
490540 "WebdriverIO",
491541- "get started",
492542+ "Get Started",
543+ ]`
544+ )
545+ } )
546+
547+ // TODO legacy behavior to be removed in future major release
548+ test ( 'should return true when trying to match non-indexed + more texts than elements (legacy behavior to deprecated)' , async ( ) => {
549+ vi . mocked ( ( await els ) [ 0 ] . getText ) . mockResolvedValueOnce ( 'webdriverio1' )
550+ vi . mocked ( ( await els ) [ 1 ] . getText ) . mockResolvedValueOnce ( 'webdriverio2' )
551+
552+ const result = await thisContext . toHaveText ( els , [ 'webdriverio2' , 'webdriverio1' , 'webdriverio' ] )
553+
554+ expect ( result . pass ) . toBe ( true )
555+ } )
556+
557+ test ( 'should return false when trying to match non-indexed + more texts than elements (legacy behavior to deprecated) but nothing match' , async ( ) => {
558+ vi . mocked ( ( await els ) [ 0 ] . getText ) . mockResolvedValueOnce ( 'webdriverio1' )
559+ vi . mocked ( ( await els ) [ 1 ] . getText ) . mockResolvedValueOnce ( 'webdriverio2' )
560+
561+ const result = await thisContext . toHaveText ( els , [ 'webdriverio' , 'webdriverio' , 'webdriverIO' ] )
562+
563+ expect ( result . pass ) . toBe ( false )
564+ expect ( result . message ( ) ) . toEqual ( `\
565+ Expect ${ selectorName } to have text
566+
567+ - Expected - 3
568+ + Received + 2
569+
570+ Array [
571+ - "webdriverio",
572+ - "webdriverio",
573+ - "webdriverIO",
574+ + "webdriverio1",
575+ + "webdriverio2",
493576 ]`
494577 )
495578 } )
0 commit comments