Published March 11, 2018 by

How to handle Lazy Loading Webpage

How to handle Lazy Loading Webpage

In this post, we'll learn about lazy Loading & How to handle Lazy Loading.


What is Lazy Loading?

There is a new technology in the web development, which is called Lazy Loading. This makes your web page lighter and load fast. 

How it works is that it loads the page only what is visible on the screen, and on scrolling down the page, it agains loads the rest of the page. So it loads only the visible part of the webpage, not the whole web page as a whole. 

How to handle Lazy Loading?

So solution to your problem is to scroll down the web page where your element is visible. Make sure you do not scroll it down completely at a go, as if you directly reach to the bottom of the page, then again it will not load the middle web page. 

So your interested element will be active only when you load that part of the page.

Here to scroll the page we are using Java- Script

The Syntax is as follows:-

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

The Below Code shows the lazy loading handling on Pinterest. 

The Source Code for this topic is available on GitHub Repository, You can get it from this URL:-




































package com.lazyloading.ui.test;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class LazyLoading {

WebDriver driver;
int scrollLimit = 10;
// Note:- Use your Email-Id & Password below to run this script
String emailId = "emailId";
String password = "password";
@BeforeMethod
public void setup() {
//Launch browser and go to a URL
System.setProperty("webdriver.chrome.driver", "/Users/ShubhamPatil/Desktop/chromedriver");
driver = new ChromeDriver();
driver.get("https://www.pinterest.com");
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}

@Test
public void test() throws InterruptedException {
//Login to Pinterest site
driver.findElement(By.id("email")).sendKeys(emailId);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.xpath("//div[text()='Continue']")).click();

//Verify Home page
Assert.assertTrue(driver.findElement(By.xpath("//div[text()='Home']")).isDisplayed(), "Home Page is not displayed");

// Call to scroll Method
scrollDown();
}

// Scroll Method, Scroll the page using Java Script
public void scrollDown() throws InterruptedException {
for (int i = scrollLimit; i >= 0; i--) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
// Wait to load the scrolled page
Thread.sleep(1000);
}
}


}
The Source Code for this topic is available on GitHub Repository, You can get it from this URL:-


If You like my post please like & Follow the blog, And get upcoming interesting topics.

Popular Posts