Selenium WebDriver

Screenshots & Browser Options

Learn to capture screenshots, full page screenshots, and configure browser options like incognito mode, headless mode, and notification settings.

Taking Screenshots in Selenium

Why Screenshots?

Screenshots are essential for debugging test failures, creating test reports, and documenting application behavior during automated testing.

Use Cases

  • • Test failure analysis
  • • Visual regression testing
  • • Documentation purposes
  • • Evidence for bug reports

Screenshot Types

  • • Viewport screenshots
  • • Full page screenshots
  • • Element screenshots
  • • Mobile screenshots

Basic Screenshot Capture

4-Step Process

Step 1: Type Casting
TakesScreenshot ts = (TakesScreenshot)driver;
Step 2: Capture Screenshot
File source = ts.getScreenshotAs(OutputType.FILE);
Step 3: Define Destination
File destination = new File("path/screenshot.png");
Step 4: Copy File
FileUtils.copyFile(source, destination);

Complete Example

package Tutorial16;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.chrome.ChromeDriver;

public class Demo1 {
    public static void main(String[] args) throws IOException {
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.facebook.com/reg");

        // Perform some actions
        driver.findElement(By.name("firstname")).sendKeys("Rohini");

        // Take screenshot
        TakesScreenshot ts = (TakesScreenshot) driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        File destination = new File("screenshots/test.png");
        FileUtils.copyFile(source, destination);

        driver.quit();
    }
}

Required Dependency

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.20.0</version>
</dependency>

Full Page Screenshots with AShot

Why AShot Library?

Regular screenshots only capture the visible viewport. AShot automatically scrolls the page and combines multiple screenshots to create a full page image.

Regular Screenshot
  • • Only visible area
  • • Fixed viewport size
  • • Quick capture
AShot Screenshot
  • • Entire page content
  • • Automatic scrolling
  • • Combined image

5-Step AShot Process

Step 1: Create AShot Object
AShot ashot = new AShot();
Step 2: Set Shooting Strategy
ashot.shootingStrategy(
  ShootingStrategies.viewportPasting(1000));
Step 3: Take Screenshot
BufferedImage source =
  ashot.takeScreenshot(driver).getImage();
Step 4 & 5: Save Image
File destination = new File("path.png");
ImageIO.write(source, "PNG", destination);

Complete AShot Example

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.goibibo.com/");

// Create AShot instance
AShot ashot = new AShot();

// Set shooting strategy with 3 second wait
ashot.shootingStrategy(ShootingStrategies.viewportPasting(3000));

// Take full page screenshot
BufferedImage source = ashot.takeScreenshot(driver).getImage();

// Save to file
File destination = new File("fullpage.png");
ImageIO.write(source, "PNG", destination);

AShot Dependency

<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.2</version>
</dependency>

Chrome Browser Options

ChromeOptions Overview

ChromeOptions class allows you to configure Chrome browser behavior, including running in different modes and disabling features.

Common Use Cases:
• Headless testing
• Incognito mode
• Disable notifications
• Custom window size
• Disable extensions
• Mobile emulation

Incognito Mode

Run browser in private/incognito mode without storing cookies, history, or cache.

ChromeOptions opt = new ChromeOptions();
opt.addArguments("--incognito");
ChromeDriver driver = new ChromeDriver(opt);

Headless Mode

Run browser without GUI for faster execution in CI/CD environments.

ChromeOptions opt = new ChromeOptions();
opt.addArguments("--headless");
ChromeDriver driver = new ChromeDriver(opt);

Disable Notifications

Prevent browser notification popups that can interfere with automation.

ChromeOptions opt = new ChromeOptions();
opt.addArguments("--disable-notifications");
ChromeDriver driver = new ChromeDriver(opt);

Combined Options Example

ChromeOptions options = new ChromeOptions();

// Add multiple arguments
options.addArguments("--headless");
options.addArguments("--disable-notifications");
options.addArguments("--disable-extensions");
options.addArguments("--no-sandbox");
options.addArguments("--disable-dev-shm-usage");

// Create driver with options
ChromeDriver driver = new ChromeDriver(options);

// Test execution works normally
driver.get("https://www.example.com");
System.out.println(driver.getTitle());

Advanced Browser Configuration

Performance Options

--disable-gpu
Disable GPU acceleration
--no-sandbox
Disable Chrome sandbox
--disable-dev-shm-usage
Overcome limited resource problems

Window Management

--start-maximized
Start browser maximized
--window-size=1920,1080
Set specific window size

Security & Privacy

--disable-web-security
Disable web security (testing only)
--ignore-certificate-errors
Ignore SSL certificate errors

User Experience

--disable-infobars
Hide "Chrome is being controlled" message
--disable-extensions
Disable all Chrome extensions

Knowledge Check

Knowledge Check

Question 1 of 5

Which interface is used to take screenshots in Selenium?

Key Points Summary

📸 Screenshot Best Practices

  • • Take screenshots on test failures
  • • Use meaningful file names with timestamps
  • • Store screenshots in organized folders
  • • Use AShot for full page captures

⚡ Performance Tips

  • • Use headless mode for faster execution
  • • Disable unnecessary browser features
  • • Set appropriate timeouts for AShot
  • • Compress screenshots for storage

🔧 Browser Configuration

  • • Use ChromeOptions for customization
  • • Combine multiple arguments effectively
  • • Test options in different environments
  • • Document browser configurations

🚨 Common Issues

  • • Handle IOException for file operations
  • • Ensure destination folders exist
  • • Check file permissions for screenshots
  • • Validate browser option compatibility

SDET Mastery

Master Test Automation

Home
CurriculumPracticeQ&ACheatsheet
🍵Buy me a Chai

Automation Testing Course

Comprehensive course covering Manual Testing, Java Programming, and Selenium WebDriver

🍵Buy me a Chai
Privacy Policy•GitHub
© 2024 Automation Testing Course. All rights reserved.