Selenium : How To Avoid Page Loading on Test Execution

If you site Is large then full page can take more time to get loaded completely. In such sites, Single action (like click on link on home page) can take more time as page takes more time to get fully loaded because WebDriver will wait for page get loaded successfully. Can we avoid or neglect this page loading to perform click action even page loading Is In process? Yes we can do It In Firefox driver.

Earlier we learnt many examples on how to create Firefox custom profile for selenium web driver test on run time to Download Different FilesHandle SSL CertificateDisable JavaScript,etc. We will use same concept here to avoid page loading to click on button.

We can create Firefox driver's custom profile and set preference webdriver.load.strategy = unstable for new created profile will do our task as shown bellow.

//Create custom profile and set preference webdriver.load.strategy = unstable
FirefoxProfile fp = new FirefoxProfile();
fp.setPreference("webdriver.load.strategy", "unstable");
  
//Load firefox driver with custom profile.
driver =new FirefoxDriver(fp);

I have created full sample example to show you how click on button element during page load In selenium webdriver test execution.

package STTA.MavenProject1;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class AvoidPageLoad {
 WebDriver driver;

 @BeforeTest
 public void setup() throws Exception {
  // Create custom profile and set preference webdriver.load.strategy = unstable
  FirefoxProfile fp = new FirefoxProfile();
  fp.setPreference("webdriver.load.strategy", "unstable");

  // Load firefox driver with custom profile.
  driver = new FirefoxDriver(fp);
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/01/textbox.html");
 }

 @Test
 public void clickBeforeLoad() {
  // This action will not wait for page to load completely.
  // Click action will be performed during page load.
  driver.findElement(By.xpath("//button[@onclick='myFunctionf()']")).click();
  System.out.println("Button got clicked");
 }
}

When you run above example, Webdriver will click on "Show Me Prompt" button even page loading Is In process as shown In bellow Image.

So It has't waited to page load fully and clicked on button during page loading Is In process. This way we can avoid page loading to perform actions In selenium WebDriver to save test execution time.

1 comment:

  1. Although this will negate the full load, is this stable enough to handle last loaded elements on more complex pages?

    ReplyDelete