Thursday, September 12, 2019

Post#25.Page Load Strategy


Page Load Strategy

When Page Loading takes too much time and you need to stop downloading additional subresources (images, css, js etc) you can change the pageLoadStrategy through the webdriver.
We want to reduce the number of calls to various resources like Images, JS, so on.
Page Load Strategy helps the user to load the webpage faster, and pageLoadStrategy support following values.

  • 1. normal This strategy causes Selenium to wait for the full page loading (html content and subresources downloaded and parsed).
  • 2. eager This strategy causes Selenium to wait for the DOMContentLoaded event (html content downloaded and parsed only).
  • 3. none his strategy causes Selenium to return immediately after the initial page content is fully received (html content downloaded).
By default, when Selenium loads a page with normal pageLoadStrategy.
package pack2;
import org.openqa.selenium.By;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class ChromeOptionsEx
{
            public static void main(String[] args) {
                        System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
                        ChromeOptions fo = new ChromeOptions();
                        // set the page load strategy
                        fo.setPageLoadStrategy(PageLoadStrategy.EAGER);
                        // create object to chrome driver, register options class
                        WebDriver driver = new ChromeDriver(fo);
                        driver.get("https://www.amazon.com");
            }
}


No comments:

Post a Comment