New features in Selenium4 - Page Loading options (Eager, Normal, None), Fullscreen and Minimize window
What you will Learn:
Fullscreen and Minimize window – new in Selenium 4
Page Loading options (normal, eager, none)
Code snippets
Fullscreen and Minimize window in Selenium 4
In Selenium 4, we now have a method that will full screen and minimize a window.
Let us automate a simple use case:
Launch https://www.selenium-tutorial.com/courses
Fullscreen the browser window using window().fullscreen()
Minimize the browser window using window().minimize()
Let us first automate steps a and b
Run script, observe below that we have a fullscreen window
Let us now write the minimize() method
Run the script, selenium would minimize the window.
Page Loading options (normal, eager, none)
In the past versions of Selenium, we would have to set a default page load timeout (example, 15 seconds, 20 sec and so on).
From Selenium 4 onwards, we can use 3 different approaches (or the load strategies) to wait for a page to load.
Here is official documentation link to have a glimpse of the 3 new approaches:
https://www.selenium.dev/documentation/webdriver/page_loading_strategy/
Before we take a look at these 3 options, let us first understand little bit about document.readyState by launching the site https://www.way2automation.com/
Once the site is launched, right click on the page and inspect > go to Console as shown below
Execute the command document.readyState as shown above and see the output that is shown ‘complete’. This signifies that the page has been fully loaded.
Next, refresh the page (press F5 key) and hit Enter, you should see document.readyState as ‘loading’ as shown below
If you are not able to see the ‘loading’ state, you can go to ‘Network’ and select ‘Slow 3G’
Once you have done that, go to console > press F5 key and hit Enter, you should see document.readyState as ‘loading’ (try this a few times, you will surely see ‘loading’ state at some point).
Once the page is fully loaded, execute the same command, you will now see ‘complete’ as shown below
So when we select ‘normal’ option, Selenium will wait until the page is fully loaded. This is what the below line means
However, when we select ‘eager’ option, Selenium will partially wait until the page is fully loaded. What this means is that, it does NOT wait for css/images/frames to load. Selenium will only wait for HTML component to fully load
When we select ‘none’ option, Selenium will only wait for the initial page to download. It does NOT even wait for HTML to download.
Let us practically see how these options work. Create a new class and set the chrome options as shown below. You can see a new method ‘setPageLoadStrategy’
Further, we can see 3 options: EAGER, NONE, NORMAL
Let us select NORMAL
We than pass the chrome options in the constructor
Finally we launch the website and quit the browser. Our final code would be:
When you run the script, you would notice that selenium waits for the entire page to download and then quits the browser
Next let us look at EAGER
Run the script, you would notice that selenium only wait for HTML to download and then quits the browser
Next let us look at NONE
Run the script, you would notice that selenium quits the browser as soon as the site comes up
So this is how we use the page load strategy options.
Code snippet (minimize/fullscreen)
package sel4scripts;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class MinimizeMaximiseBrowser {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
//launch a site
driver.get("https://www.selenium-tutorial.com/courses");
Thread.sleep(2000);
//Fullscreen browser window
driver.manage().window().fullscreen();
Thread.sleep(2000);
//Minimize browser window
driver.manage().window().minimize();
}
}
Code snippet (page load strategy)
package sel4scripts;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class PageLoaderOptions {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeOptions co = new ChromeOptions();
//co.setPageLoadStrategy(PageLoadStrategy.NORMAL);
//co.setPageLoadStrategy(PageLoadStrategy.EAGER);
co.setPageLoadStrategy(PageLoadStrategy.NONE);
WebDriver driver = new ChromeDriver(co);
//launch a site with page load strategy
driver.get("https://www.selenium-tutorial.com/courses");
driver.quit();
}
}
Thank you for reading!