[What is selenium](What is #seleniumt) Goal Preparation [Installation procedure](# Installation procedure) [Test case](# test case) [Run test case](#Run test case)
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.
Project name "selenium"
Java selenium version3.141.59
Latest edition
Same version as Google Chrome
Download the latest version of junit.jar and hamcrest-core.jar from Download and install.
Add the Java build path from the properties of the selenium project.
You are now ready.
I will actually move it. If you refer to this area, you can move it. https://qiita.com/VA_nakatsu/items/0095755dc48ad7e86e2f https://qiita.com/mochio/items/dc9935ee607895420186
Main processing part. It inherits TestSetting.java. Javascript can be used with 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, "Save 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 the screen
TestUtils.screenShot(this.driver, path, "Screenshot");
//Move down the screen
JavascriptExecutor js = (JavascriptExecutor) this.driver;
js.executeScript("window.scrollBy(0,3000)");
//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.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 {
//Location of file for capture
protected String capturePath;
protected WebDriver driver;
//Operation before test execution
@Before
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("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);
//Launch chrome
this.driver = new ChromeDriver();
//Maximize the window
this.driver.manage().window().maximize();
}
//Operation after test execution
@After
public void yeild() throws IOException{
//Close chrome
this.driver.quit();
}
}
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 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);
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 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;
}
}
When you run ChromeTest.java 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.
Recommended Posts