I am using selenium Web Driver for testing. The issue is that when my url changes getCurrentURL gives me current URL. I am using driver.sleep method for this. But issue with driver.sleep method is that I have to pass fixed time to it to make it run after my URL has changed. Is there any method which runs as my URL changes (looking for some alternative of sleep which does not need explicit declaration of time)?
Asked
Active
Viewed 8,072 times
1
-
Did you look in the docs? It's right in there. – JeffC May 13 '19 at 15:48
-
It's about implementation of Selenium Webdriver in Javascript – Saeed Ahmad May 14 '19 at 04:55
3 Answers
3

Guy
- 46,488
- 10
- 44
- 88
-
Actually I want to check if browser takes me to the other url or not. If it takes me to next route I will show 'test passed' and if it doesn't I will show 'test failed'. I have implemented your solution it says `(node:800) UnhandledPromiseRejectionWarning: NoSuchSessionError: Tried to run command without establishing a connection` – Saeed Ahmad May 14 '19 at 04:47
1
Your approach to find a way to wait for a specific behavior instead of using a fixed amount of time is definitely correct.
The way you do it depends on how you are implementing it. For example, if you are using the Protractor framework, the following should work:
var EC = browser.ExpectedConditions;
browser.wait(EC.urlContains('Expected URL'), timeout); // Checks that the current URL contains the expected text
browser.wait(EC.urlIs('Expected URL'), timeout); // Checks that the current URL matches the expected text
Any way, hopefully you can adapt this logic to your case.

Anselmo
- 11
- 3
0
We can achieve this by using polling technique. Write a loop to check every 2 seconds where current URL is changed or not. Here is the sample code.
String currentUrl = driver.getCurrentUrl();
String newUrl ;
do {
newUrl = driver.getCurrentUrl();
Thread.sleep(5);
}while(newUrl.contentEquals(currentUrl));
//if control came out of loop , then you have new url

Shyamk327
- 26
- 7