0

I have seen this question, which is a combination of wait + findElementBy. But, my question is slightly different, since i need to check for URL (rather than element) after page loaded completely.

I already tried the below solution but it is not working for me:

public void checkCurrentURL(String expectedURL) {
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // 5 seconds delay to load the page
    String realURL = driver.getCurrentUrl();
    System.out.println("------------------------------------URL is: "+realURL);
    Assert.assertTrue(realURL.equals(expectedURL));

}

And here is my selenium version:

       <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.14.0</version>
        </dependency>

Update: Thanks to @Guy when i use the below method:

public void checkCurrentURL(String expectedURL) {

        new WebDriverWait(driver, 2).until(
                new ExpectedCondition<Boolean>() {
                    @Override
                    public Boolean apply(WebDriver d) {
                        return d.executeScript("return document.readyState").equals("complete");
                    }   
                }
            );

        String realURL = driver.getCurrentUrl();
        System.out.println("------------------------------------URL is: "+realURL);
        Assert.assertTrue(realURL.equals(expectedURL));

    }

It complains with:

The method until(Function<WebDriver,T>) in the type WebDriverWait is not applicable for the arguments ()

Here is the repository.

I updated the selnium to 3.14.0 since 3.142.6 does not work for me.

Sal-laS
  • 11,016
  • 25
  • 99
  • 169

1 Answers1

-3

implicitlyWait is used to configure the maximum lookup time when searching WebElement, it's not relevant here and will effect the driver from now on, not only in the method scope.

You can use WebDriverWait and check the document.readyState

new WebDriverWait(driver, pageLoadTimeout).until(
    new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver d) {
            return d.executeScript("return document.readyState").equals("complete");
        }   
    }
);

As a side note, version 3.4.0 is quite old (~2.5 years old). Consider update to version 3.142.6 (the newest not alpha version).

Guy
  • 46,488
  • 10
  • 44
  • 88
  • @SalmanLashkarara it might be due to the Selenium version. I added another solution. – Guy Dec 22 '19 at 11:16
  • @SalmanLashkarara I removed the first `return`, check now. – Guy Dec 22 '19 at 11:28
  • `The method executeScript(String) is undefined for the type WebDriver` – Sal-laS Dec 22 '19 at 11:29
  • @SalmanLashkarara this due to the old Selenium version. If you aren't going to upgrade you need to use `JavascriptExecutor`: `((JavascriptExecutor)driver).executeScript(...);` – Guy Dec 22 '19 at 11:32
  • I updated it to `3.14.0` since, i `3.142.6` does not work for me. Here is my POM: https://github.com/salman-/Friday/blob/master/pom.xml – Sal-laS Dec 22 '19 at 11:35
  • @SalmanLashkarara You need to update `selenium-support` as well. – Guy Dec 22 '19 at 11:38
  • Could you please remove your answer, so i can delete my question? – Sal-laS May 09 '20 at 08:34