How to validate page url

How do I validate page url? For example?

Step : I opened a browser and navigaed to https://www.cnn.com/
WebUI.openBrowser(’’)
WebUI.navigateToUrl(‘https://www.cnn.com/’)

Step2: Click on US link
WebUI.click(findTestObject(‘Object Repository/New Folder (8)/a_US’))

On Step 2 I landed on US News – Top national stories and latest headlines - CNN

Now I would like to write a code to verify and compare the url. I would appreciate if you could provide me the code.

God bless you!

With WebUI.getUrl() you can get the URL string which you are currently on.

String currentUrl = WebUI.getUrl()
if (currentUrl == 'https://www.cnn.com/us') {
    println "OK"
} else {
    println "NG"
}

Thank you, however it is not really validating the url and still the script is passing even when I intentionally provide wrong url. Here is what I have written.

WebUI.openBrowser(‘https://missionbio.com/company/newsroom/’)
WebUI.click(findTestObject(‘Object Repository/a_Mission Bio Launches Single-cell Multi-om_53ab2a’))
WebUI.switchToWindowIndex(1)
currentUrl = WebUI.getUrl()
if (currentUrl == ‘https://missionbio.com/press/single-cell-multi-omics/’) {
println “OK”
} else {
println “NG”
}
WebUI.delay(2)
WebUI.closeBrowser()

The current url suppose to be https://missionbio.com/press/single-cell-multi-omics-launch/ and I provided a different url in the code and it should fail.

import com.kms.katalon.core.exception.StepFailedException as StepFailedException

// ...

  if(currentUrl != "whatever") {
    throw new StepFailedException("Bad URL")
  }

// rest of test

I apologize. It is still not working. Would you mind copy/paste the code? I appreciate for your help! All I need is to validate the page URL whether it is expected result or not .

WebUI.openBrowser(‘https://missionbio.com/company/newsroom/’)
WebUI.click(findTestObject(‘Object Repository/a_Mission Bio Launches Single-cell Multi-om_53ab2a’))
WebUI.switchToWindowIndex(1)
currentUrl = WebUI.getUrl()
//…
if(currentUrl != “https://missionbio.com/press/single-cell-multi-omics-launch/”) {
throw new StepFailedException(“Bad URL”)
}
//
WebUI.delay(2)
WebUI.closeBrowser()

Put the import statement at the top of your file.

Delete all the // lines.

Then what you have will work.

assert currentUrl == 'https://missionbio.com/press/single-cell-multi-omics-launch/'

this should work out of the box without any extra import needed.

@aluri

Let us understand your problem more clearly.

  1. Please share the latest version of your test case script, do not trim any lines such as import statements
  2. Please enclose your code with triple back-ticks for code formatting: スクリーンショット 2020-10-30 21.56.26
  3. Please share the definition of your Test Obect a_Mission Bio Launches Single-cell Multi-om_53ab2a (XPath expression or CSS Selector)
  4. What do you want your code to do when it find the current URL is NOT identical to the URL you expect? What message do you want to see? Do you want the code to continue or stop further processing?

As you well know, assert is a blunt instrument with no fine detail or control. Well… //mostly// :nerd_face:

well yeah … agreed.
however, i mostly used katalon for API testing, so my mind works like:

  • check if response is 200 otherwise there is no point to go further
  • check if response is a valid json otherwise there is no point to go further
    … and so on

most of the time i want to stop immediately the script if something went wrong.

if i need a custom message i can simply do:

assert fail_something_here : "test failed because i am lazy to write if/else"

if the testcase requires to catch a specific exception (could be the case for json schema validation where i may want to filter the displayed messages) i will rather go to write a custom keyword for it so if i have to pimp it further i will do in only one place, not in 100 if/else or try/catch spread across various testcases.
whatever is not treated by my keword i want to be thrown with full stack so i can further debug it.

so … as you are a firefox addicted, i am assert addicted. it just works for me :stuck_out_tongue:

1 Like