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
Complete Example
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
<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
ShootingStrategies.viewportPasting(1000));
ashot.takeScreenshot(driver).getImage();
ImageIO.write(source, "PNG", destination);
Complete AShot Example
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
<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.
Incognito Mode
Run browser in private/incognito mode without storing cookies, history, or cache.
opt.addArguments("--incognito");
ChromeDriver driver = new ChromeDriver(opt);
Headless Mode
Run browser without GUI for faster execution in CI/CD environments.
opt.addArguments("--headless");
ChromeDriver driver = new ChromeDriver(opt);
Disable Notifications
Prevent browser notification popups that can interfere with automation.
opt.addArguments("--disable-notifications");
ChromeDriver driver = new ChromeDriver(opt);
Combined Options Example
// 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
Window Management
Security & Privacy
User Experience
Knowledge Check
Knowledge Check
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