[Java] A quick introduction to integration testing with Selenide + Chrome Driver <Updated in May 2020>

Introduction

After developing the function of the application, you do ** integration test (integration test) ** to see how it works well with other functions and how it behaves. This time, let's automate a simple integration test of a Web application developed in Java instead of visually (it is natural) ** Selenide + [ChromeDriver]( I'm going to do hands-on using https://chromedriver.chromium.org) **. スクリーンショット 2020-05-22 0.38.50.png

Development environment

macOS Catalina 10.15.4 IntelliJ IDEA 2018.3.6 Google Chrome 81.0.4044.138 Selenide 5.2.2 aShot 1.5.3

Preparation

Describe the library to be used in pom.xml.

pom.xml

<dependency>
    <groupId>com.codeborne</groupId>
    <artifactId>selenide</artifactId>
    <version>5.2.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>ru.yandex.qatools.ashot</groupId>
    <artifactId>ashot</artifactId>
    <version>1.5.3</version>
</dependency>

Then download the driver to use the Chrome Driver. (Link below) https://chromedriver.chromium.org

  1. Click Downloads スクリーンショット 2020-05-22 0.20.42.png
  2. Select the driver according to ** Chrome version ** (this time 81.0.4044.138) スクリーンショット 2020-05-22 0.30.41.png
  3. For MacOS, select mac64.zip, download it, and store it in any location (** remember the path for later use **) スクリーンショット 2020-05-22 0.32.41.png

basic configuration

・ Chrome driver path setting -Creating a ChromeDriver instance Now you can operate Chrome automatically using Selenide.

System.setProperty("webdriver.chrome.driver", "<Driver path>/chromedriver");
WebDriver webDriver = new ChromeDriver();

Close the window by writing close () on the last line.

webDriver().close();

basic operation

The basic operations required for the test are explained below.

Transition to link

webDriver.get("https://hoge.com/");

There is another method.

webDriver.navigate().to("https://hoge.com/");

[Supplement] You can think of get () and navigate (). To () as almost the same method. The difference between the two comes out when working with SPAs (single page applications). In the SPA, get () refreshes the page and transitions, while navigate (). To () transitions without refreshing the page. get () does not retain browser history or cookies, but navigate (). To does. In SPA, navigate (). To () can be used to perform tests that are closer to human operation.

Back / forward

Return

webDriver.navigate().back();

move on

webDriver.navigate().forward();

update

update

webDriver.navigate().refresh();

Get element

Get HTML id element

webDriver.findElement(By.id("hoge"));

You can specify the class element, but it is recommended to get the id because the acquisition result is often not unique.

Button click

Get the HTML id element of the button and click

webDriver.findElement(By.id("hoge")).click();

Form input

Get the HTML id element of the form and enter it with sendkeys ()

webDriver.findElement(By.id("hoge")).sendKeys("Hogehoge");

The behavior is input character by character as if it were actually input on the screen.

Clear the value entered in the form

Get the HTML id element of the form and clear it with clear ()

webDriver.findElement(By.id("hoge")).clear();

This is convenient when you want to delete the existing input contents in the value update process.

Take a screenshot

The page size is minimized so that the full screen can be screened on the first line. If you do not minimize it, Chrome will automatically adjust the window size, so the entire screen will not look good. The third line specifies the path of the image.


webDriver.manage().window().setPosition(new Point(-2000, 0));

Screenshot screenshot = new AShot()
        .shootingStrategy(ShootingStrategies.viewportPasting(100))
        .takeScreenshot(webDriver);

ImageIO.write(screenshot.getImage(), "PNG", new File("<Image storage path>/<Image name>.png "));

(Bonus) Example of integration test code

【test scenario】

  1. Access the login screen of the hoge application
  2. Enter the administrator user's email address and password in the form
  3. Click the login button
  4. Click the edit button for post 1 in the post list
  5. Clear the post name on the form
  6. Enter post 2 in the post name
  7. Click the refresh button
  8. Screenshot of the screen after the update is completed

System.setProperty("webdriver.chrome.driver", "hoge/chromedriver");
WebDriver webDriver = new ChromeDriver();

webDriver.get("https://hoge.com/home");
webDriver.findElement(By.id("email")).sendKeys("[email protected]");
webDriver.findElement(By.id("password")).sendKeys("pass");
webDriver.findElement(By.id("loginButton")).click();
webDriver.navigate().to("https://hoge.com/edit?topicId=1");
webDriver.findElement(By.id("topicName")).clear();
webDriver.findElement(By.id("topicName")).sendKey("Post 2");
webDriver.findElement(By.id("updateButton")).click();
webDriver.manage().window().setPosition(new Point(-2000, 0));
Screenshot screenshot = new AShot()
    .shootingStrategy(ShootingStrategies.viewportPasting(100))
    .takeScreenshot(webDriver);
ImageIO.write(screenshot.getImage(), "PNG", new File("hoge/screenshot.png "));

webDriver.close();

in conclusion

Even if you search with selenide, information on Python instead of Java will come out, so I summarized it. The example of integration testing is an imaginary test of an imaginary web application. Thank you very much.

reference

· Difference between webdriver.get () and webdriver.navigate ()Selenium Quick Reference

Recommended Posts

[Java] A quick introduction to integration testing with Selenide + Chrome Driver <Updated in May 2020>
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
[Personal memo] How to interact with a random number generator in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
Selenium Web Driver with user-data-dir set to crash in Chrome headless mode
I want to ForEach an array with a Lambda expression in Java
Introduction to algorithms with java --Search (depth-first search)
[Introduction to Java] How to write a Java program
Split a string with ". (Dot)" in Java
Introduction to algorithms with java --Search (breadth-first search)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
For my son who started studying Java with "Introduction to Java" in one hand
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
Two ways to start a thread in Java + @
Create a CSR with extended information in Java
Introduction to algorithms with java --Search (bit full search)
How to display a web page in Java
Code to escape a JSON string in Java
Try to create a bulletin board in Java
A quick review of Java learned in class
I tried to break a block with java (1)
How to save a file with the specified extension under the directory specified in Java to the list
[Java] Introduction to Java
Introduction to java
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
Submit a job to AWS Batch with Java (Eclipse)
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
I tried to make a login function in Java
[How to insert a video in haml with Rails]
Quickly implement a singleton with an enum in Java
Output true with if (a == 1 && a == 2 && a == 3) in Java (Invisible Identifier)
A quick review of Java learned in class part3
How to convert a file to a byte array in Java
Try to solve a restricted FizzBuzz problem in Java
[Java] How to start a new line with StringBuilder
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3