Selenium with Java: Screenshots, Headless Mode, SSL, Ad Block, and Extensions

Selenium is a powerful tool for automating web applications for testing purposes. Combining Selenium with Java enhances its capabilities, especially when managing advanced scenarios like taking screenshots, running tests in headless mode, handling SSL certificates, blocking ads, and managing browser extensions. In this post, we'll explore how to implement these features using Selenium with Java.

1. Taking Screenshots in Selenium

Screenshots are crucial in test automation for capturing the state of the application under test, especially when a test fails. Selenium provides a way to capture screenshots in just a few lines of code.

Code Example:

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.io.FileHandler;
import java.io.File;

public class ScreenshotExample {
    public static void takeScreenshot(WebDriver driver, String filePath) {
        try {
            TakesScreenshot screenshot = (TakesScreenshot) driver;
            File srcFile = screenshot.getScreenshotAs(OutputType.FILE);
            FileHandler.copy(srcFile, new File(filePath));
            System.out.println("Screenshot saved at: " + filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Explanation:

  • TakesScreenshot is an interface that Selenium WebDriver provides to capture the current browser window's screenshot.
  • The screenshot is saved in the location specified by the filePath.

Usage:

ScreenshotExample.takeScreenshot(driver, "path/to/screenshot.png");


2. Running Tests in Headless Mode

Headless mode allows Selenium to run the browser without a graphical interface, which is useful for running tests on a server or in a CI/CD pipeline. Both Chrome and Firefox support headless modes.

Chrome Headless Mode:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class HeadlessChrome {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        WebDriver driver = new ChromeDriver(options);

        driver.get("https://www.example.com");
        System.out.println("Title: " + driver.getTitle());
        driver.quit();
    }
}

Firefox Headless Mode:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class HeadlessFirefox {
    public static void main(String[] args) {
        FirefoxOptions options = new FirefoxOptions();
        options.setHeadless(true);
        WebDriver driver = new FirefoxDriver(options);

        driver.get("https://www.example.com");
        System.out.println("Title: " + driver.getTitle());
        driver.quit();
    }
}

Explanation:

  • --headless argument is used to initiate headless mode in Chrome, while setHeadless(true) does the same for Firefox.
  • This allows the browser to run without the need for a display, improving performance for automated testing environments.

3. Handling SSL Certificates

When testing websites with untrusted SSL certificates, browsers typically show a warning page. Selenium provides options to handle this scenario gracefully.

Chrome:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class HandleSSLChrome {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.setAcceptInsecureCerts(true);  // Accept insecure SSL certificates
        
        WebDriver driver = new ChromeDriver(options);
        driver.get("https://untrusted-root.badssl.com/");
        System.out.println("Title: " + driver.getTitle());
        driver.quit();
    }
}

Firefox:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class HandleSSLFirefox {
    public static void main(String[] args) {
        FirefoxOptions options = new FirefoxOptions();
        options.setAcceptInsecureCerts(true);  // Accept insecure SSL certificates
        
        WebDriver driver = new FirefoxDriver(options);
        driver.get("https://untrusted-root.badssl.com/");
        System.out.println("Title: " + driver.getTitle());
        driver.quit();
    }
}

Explanation:

  • setAcceptInsecureCerts(true) ensures that the browser will bypass SSL certificate errors and proceed with the test.


4. Blocking Ads with Selenium

Blocking ads can improve the performance of your tests and help to focus only on the content you are testing. To block ads, we can install ad-blocker extensions in the browser.

Chrome with AdBlocker Extension:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;

public class AdBlockerExample {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File("path/to/adblocker.crx"));  // Add AdBlocker extension

        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.example.com");
        System.out.println("Title: " + driver.getTitle());
        driver.quit();
    }
}

Explanation:


  • Use addExtensions() to add the path of the ad blocker extension (.crx file) to the browser session.

5. Adding Extensions to Browser

Beyond blocking ads, you might want to install other extensions to the browser during your tests. This can be done by providing the .crx file of the extension.

Example:


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;

public class BrowserExtensions {
    public static void main(String[] args) {
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File("path/to/extension.crx"));  // Add any extension

        WebDriver driver = new ChromeDriver(options);
        driver.get("https://www.example.com");
        System.out.println("Title: " + driver.getTitle());
        driver.quit();
    }
}


No comments:

Post a Comment

FOLLOWERS