[Java] Save capture from selenium introduction in Java

3 minute read

table of contents

What is selenium Goal Preparation Installation procedure 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.

Target

Introduce Java selenium, open Chrome → transition to Yahoo news → capture.

Preparation

Creating a Java project

Project name “selenium”

selenium’s DL

Java selenium version3.141.59

Google Chrome DL

Latest edition

Chromedriver DL

Same version as Googl Chrome

JUnit DL

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

Installation procedure

Create a lib folder and exe folder directly under ####1.selenium project image.png

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

Add the jar file copied to the ####3.lib folder to the classpath Add Java build path from selenium project property. Comment 2020-06-18 102953.png Comment 2020-06-18 103442.png

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

You’re ready to go.

test case

I will actually move it. If you refer to this area, you can move it roughly. https://qiita.com/VA_nakatsu/items/0095755dc48ad7e86e2f https://qiita.com/mochio/items/dc9935ee607895420186

Project structure

Comment 2020-06-18 140323.png

・ChromeTest.java

Main processing part. It inherits TestSetting.java. Javascript can be used in selenium.

package test.selenium;

import java.io.IOException;

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
*/
@Test
public void testChrome() throws WebDriverException, IOException{
// Create a capture save folder
String path = TestUtils.mkdir(this.capturePath, "storage folder");

// Transition to YAHOO news page
this.driver.get("https://news.yahoo.co.jp/");

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

// capture screen
TestUtils.screenShot(this.driver, path, "Scussion");

// move screen down
JavascriptExecutor js = (JavascriptExecutor) this.driver;
js.executeScript("window.scrollBy(0,3000)");

// capture screen
TestUtils.screenShot(this.driver, path, "after moving screen");
}
}

####・TestSetting.java It works at the beginning of test execution, creates a capture save folder and opens chrome.

package test.selenium;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

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

import util.TestUtils;



public class TestSetting {

// Place the capture file
protected String capturePath;

protected WebDriver driver;

// Behavior before test execution
@Before
public void initSet() throws IOException{
// Get the execution user's desktop path
String desktopPath = System.getProperty("user.home") + "\\Desktop";

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

// pass Chrome drive path
System.setProperty("webdriver.chrome.driver", "./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);

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

// maximize the window
this.driver.manage().window().maximize();
}

// Behavior after test execution
@After
public void yeild() throws IOException{
// close chrome
this.driver.quit();
}
}

####・TestUtils.java Screenshot processing and capture folder creation processing

package util;

import java.io.IOException;
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 showing the display
*
* @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);
driver.switchTo().defaultContent();
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 capture folder 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 the test case

When ChromeTest.java is executed from “Run” -> “JUnit test”, Chrome starts and a capture save folder is created on the desktop and the captured one is saved. Comment 2020-06-18 150109.png