2

My test scenario is that:

  1. You go to url1
  2. you click button
  3. you open another tab with url2

How to I assert the url2?

I use assert(browser).toHaveUrlContaining('') But it assert the url1, so my test was fail

Albina
  • 1,901
  • 3
  • 7
  • 19
Linh Ho
  • 21
  • 3

1 Answers1

0

You can try using the browser switchwindow method as you can see here: https://webdriver.io/docs/api/browser/switchWindow/

By doing that I assume that your test would be something like:

  1. You go to url1
  2. You click button
  3. You open another tab with url2
  4. You switch tab (browser.switchWindow('https://theUrlYouWant.com'))
  5. Assert browser.getUrl() is 'https://theUrlYouWant.com'

Let me know if this helps!

  • Hi, it works. But if you are able to switch to the url2, you technically don't need to assert it again – Linh Ho Mar 15 '23 at 11:16
  • Hi! Glad it helped. I understand your point. I would just like to give you two insights on this: 1) when reading or reviewing the implemented test case, it will be clearer for the reviewer to understand the purpose of the test. 2) depending on how the browser.switchwindow method behaves when the url is not found, it makes sense to leave the assertion (e.g. imagine that if the method didn't find the url it wouldn't raise an error, by not having the assertion the test would pass even when it should'nt). Did it make sense? Have a nice day! – SuperZezoide Mar 16 '23 at 11:01