Capture and save from selenium installation in Java

table of contents

[What is selenium](What is #seleniumt) Goal Preparation [Installation procedure](# Installation procedure) [Test case](# test case) [Run test case](#Run test case)

What is selenium

Selenium is a browser automation tool. You can test the operation of the website by operating the browser automatically.


Introduced Java selenium, opened Chrome → transitioned to Yahoo news → captured.


Creating a Java project

Project name "selenium"

selenium [DL] link-1

Java selenium version3.141.59

Google Chrome [DL] link-2

Latest edition

Chromedriver [DL] link-3

Same version as Google Chrome

JUnit [DL] link-4

Download the latest version of junit.jar and hamcrest-core.jar from Download and install.

Installation procedure

1. Create lib folder and exe folder directly under selenium project


2. Copy all the .jar files in the libs folder in the downloaded and the client-combined-3.141.59.jar to the created lib folder.

3. Add the jar file copied to the lib folder to the classpath

Add the Java build path from the properties of the selenium project. コメント 2020-06-18 102953.png コメント 2020-06-18 103442.png

4. Copy the downloaded Chromedriver.exe to the created exe folder.

You are now ready.

test case

I will actually move it. If you refer to this area, you can move it.

Project structure

コメント 2020-06-18 140323.png


Main processing part. It inherits Javascript can be used with selenium.

package test.selenium;


import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriverException;

import util.TestUtils;

public class ChromeTest extends TestSetting {

	 *Test execution process
	 * @throws WebDriverException
	 * @throws IOException
	public void testChrome() throws WebDriverException, IOException{
		//Create a capture save folder
		String path = TestUtils.mkdir(this.capturePath, "Save folder");

		//Transition to YAHOO news page

		//Transition to IT news list screen
		this.driver.findElement(By.cssSelector("#snavi > ul.yjnHeader_sub_cat > li:nth-child(7)")).click();

		//Capture the screen
		TestUtils.screenShot(this.driver, path, "Screenshot");

		//Move down the screen
		JavascriptExecutor js = (JavascriptExecutor) this.driver;

		//Capture the screen
		TestUtils.screenShot(this.driver, path, "After moving the screen");


It works at the beginning of the test run, creates a capture save folder and opens chrome.

package test.selenium;

import java.util.HashMap;
import java.util.Map;

import org.junit.Before;
import org.openqa.selenium.WebDriver;

import util.TestUtils;

public class TestSetting {

	//Location of file for capture
	protected String capturePath;

	protected WebDriver driver;

	//Operation before test execution
	public void initSet() throws IOException{
		//Get the desktop path of the executing user
		String desktopPath = System.getProperty("user.home") + "\\Desktop";

		//Create a capture save folder on your desktop
		this.capturePath = TestUtils.mkdir(desktopPath, "Capture save folder");

		//Pass the Chrome driver path
		System.setProperty("", "./exe/chromedriver.exe");

		//Change the download destination to the specified folder
		Map<String, Object> dir = new HashMap<String, Object>();
		dir.put("download.default_directory", this.capturePath);

		ChromeOptions option = new ChromeOptions();
		option.setExperimentalOption("dir", dir);

		//Launch chrome
		this.driver = new ChromeDriver();

		//Maximize the window

	//Operation after test execution
	public void yeild() throws IOException{
		//Close chrome


Screenshot processing and capture folder creation processing

package util;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;

public class TestUtils {

	 *Screenshot of the display part
	 * @param driver
	 * @param path
	 * @param filename
	 * @throws IOException
	public static void screenShot(WebDriver driver, String path, String filename) throws IOException{

		driver.manage().timeouts().implicitlyWait(30,  TimeUnit.SECONDS);
		TakesScreenshot ts = (TakesScreenshot) new Augmenter().augment(driver);
		Path from = Paths.get(ts.getScreenshotAs(OutputType.FILE).toURI());
		Path to = Paths.get(path + "\\" + filename + ".png ");
		Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);

	 *Create a folder for capture in the specified location
	 * @param dirpath
	 * @param dirname
	 * @return
	 * @throws IOException
	public static String mkdir(String dirpath, String dirname) throws IOException{
		String path = Paths.get(dirpath, dirname).toString();

		if(Files.notExists(Paths.get(dirpath,  dirname))) {
			Files.createDirectories(Paths.get(dirpath, dirname));
		return path;

Run test case

When you run from "Run"-> "JUnit Test", Chrome will start and a capture save folder will be created on the desktop and the captured one will be saved. コメント 2020-06-18 150109.png

Recommended Posts

Capture and save from selenium installation in Java
Generate OffsetDateTime from Clock and LocalDateTime in Java
Reverse Enum constants from strings and values in Java
JSON in Java and Jackson Part 1 Return JSON from the server
Implement Java Interface in JRuby class and call it from Java
Correct the character code in Java and read from the URL
Get attributes and values from an XML file in Java
Encoding and Decoding example in Java
[For beginners] Run Selenium in Java
StringBuffer and StringBuilder Class in Java
Use Chrome Headless from Selenium / Java
Understanding equals and hashCode in Java
Hello world in Java and Gradle
Study Deep Learning from scratch in Java.
JSON in Java and Jackson Part ③ Embed JSON in HTML and use it from JavaScript
Difference between final and Immutable in Java
Call Java method from JavaScript executed in Java
Reverse Key from Value in Java Map
[Java] for Each and sorted in Lambda
Using JavaScript from Java in Rhino 2021 version
Gradle automatically generates version number from git and uses it in Java
Read xlsx file in Java with Selenium
Difference between getText () and getAttribute () in Selenium
Get history from Zabbix server in Java
Arrylist and linked list difference in java
Program PDF headers and footers in Java
Learn Flyweight patterns and ConcurrentHashMap in Java
Java Direction in C ++ Design and Evolution
Java to C and C to Java in Android Studio
Reading and writing gzip files in Java
Difference between int and Integer in Java
java installation
Discrimination of Enums in Java 7 and above
Java: Download the file and save it in the location selected in the dialog [Use HttpClient]
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation
GetInstance () from a @Singleton class in Groovy from Java
Regarding the transient modifier and serialization in Java
Create barcodes and QR codes in Java PDF
Detect similar videos in Java and OpenCV rev.2
Difference between next () and nextLine () in Java Scanner
How to get Class from Element in Java
Text extraction in Java from PDF with pdfbox-2.0.8
Get unixtime (seconds) from ZonedDateTime in Scala / Java
Detect similar videos in Java and OpenCV rev.3
Add, read, and delete Excel comments in Java
Check static and public behavior in Java methods
[Java] Understand in 10 minutes! Associative array and HashMap
Basics of threads and Callable in Java [Beginner]
[Deep Learning from scratch] in Java 3. Neural network
Automatically modify and save coding conventions in Eclipse
Distinguish between positive and negative numbers in Java
Java adds and removes watermarks in word documents
Detect similar videos in Java and OpenCV rev.1
Represents "next day" and "previous day" in Java / Android
Questions in java exception handling throw and try-catch
Upload and download notes in java on S3
Encrypt / decrypt with AES256 in PHP and Java
[Java] Get KFunction from Method / Constructor in Java [Kotlin]
Use Shift Right and Shift Left properly in Selenium
[Android development] Get an image from the server in Java and set it in ImageView! !!
Java installation jdk