简体   繁体   中英

How can I setup a “LoadPage” on Selenium in my whole code project

I'm programming a test automation but every simple line of code I'm using the "Thread.Sleep", i know that is a bad practice insert these command every line to wait for load my page application, My question is: How can I setup a piece of code that means WAIT FOR LOAD in the all my Java project? Because actually i'm using Thread.Sleep but really isn't efficient

Seems like Selenium has just the right solution for you. You should read about using waits in Selenium, which would exactly wait for the element to load for the duration of time you want to wait and then carry on with the execution.

You can read about this here on the Selenium documentation . You can read in detail also here (it has a Java implementation) or here (example code is a Python implementation).

Let me know if need any more explanation.

Here is the Answer to your Question:

1. Thread.Sleep :

As you mentioned I'm using the "Thread.Sleep", i know that is a bad practice is pretty correct. Thread.Sleep() degrades the performance of execution. So Best Practices suggests to avoid Thread.Sleep() and use the other alternatives either Implicit Wait , Explicit Wait or Fluent Wait .

2. Implicit Wait :

Implicit Wait is a method to make the test wait for the webpage's components to load, so that they can be available during our tests. When an implicit wait is implemented it tells WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0.Once we set the Implicit Wait , it is set for the life of the WebDriver object's instance. However, an Implicit Wait may slow down our tests when an application responds normally, as it will wait for each element to appear in the DOM and increase the overall execution time.

An Example:

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

3. Explicit Wait :

Explicit Wait provides us more flexibility where we can wait for some predefined or custom conditions and then carry on with what we want. Through Explicit Wait we can write custom code or conditions for wait before proceeding further in the code. An explicit wait can only be implemented in cases where synchronization is needed and the rest of the script is working fine. The Selenium WebDriver provides WebDriverWait and ExpectedCondition classes for implementing an explicit wait. The ExpectedCondition class provides a set of predefined conditions to wait before proceeding further in the code.

An Example:

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your_xpath")));

4. Fluent Wait :

Fluent Wait instance is implemented through defining the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. The user may configure the total wait along with polling interval to ignore specific types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

An Example:

       Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
           .withTimeout(30, TimeUnit.SECONDS)
           .pollingEvery(250, TimeUnit.MILLISECONDS)
           .ignoring(NoSuchElementException.class);

       WebElement element = wait.until(new Function<WebDriver, WebElement>() 
       {
         public WebElement apply(WebDriver driver) 
         {

             WebElement ele = driver.findElement(By.id("element_id"));

             String value = ele.getAttribute("attribute");

             if(value.equalsIgnoreCase("WebDriver"))
             {
                 return ele; 
             }

             else
             {
                 System.out.println("Value is : "+value);
                 return null;
             }
         }
       });

Hence the solution for you to WAIT FOR LOAD will be to introduce Explicit Wait and Fluent Wait as per the specific element and specific conditions you want to handle.

Here you will find some more documentation here about different waits.

Let me know if this Answers your Question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM