Home > front end >  How can I check whether my page is fully loaded in selenium c#
How can I check whether my page is fully loaded in selenium c#

Time:05-31

I need to check whether my page is fully loaded using selenium c# and not using TestNG. If my page is not loaded, then I don't want my test to fail. I want my test to retry the same test case. I am using MSTest and not Nunit. Any help is much appreciated

CodePudding user response:

You can try something like below:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;

public class PagaLoadJS{

   public static void main(String[] args) {

      System.setProperty("webdriver.chrome.driver",

      "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");

      WebDriver driver = new ChromeDriver();

      String url = "https://www.tutorialspoint.com/index.htm";

      driver.get(url);

      // Javascript executor to return value


      JavascriptExecutor j = (JavascriptExecutor) driver;
      j.executeScript("return document.readyState")
      .toString().equals("complete");

      // get the current URL

      String s = driver.getCurrentUrl();

      // checking condition if the URL is loaded

      if (s.equals(url)) {

         System.out.println("Page Loaded");

         System.out.println("Current Url: "   s);

      }

      else {

         System.out.println("Page did not load");

      }

      driver.quit();

   }

}

CodePudding user response:

If you're expecting the page to load, you can use wait conditions on the driver to wait for a period of time until you've expected the page (or part of it) to have loaded.

In C#, you can do that that with the following line of code where you need the page to be present.

new WebDriverWait(driver, TimeSpan.FromSeconds(5)).Until(driver => driver.Title.Equals("Homepage"));

You can change the driver.Title.Equals() to any function that will eventually return a true value to verify your page exists (could be a UI element using the FindElementById() method for example.

Source: https://www.jamescroft.co.uk/wait-conditions-in-selenium-with-c-sharp/

  • Related