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) **.
macOS Catalina 10.15.4 IntelliJ IDEA 2018.3.6 Google Chrome 81.0.4044.138 Selenide 5.2.2 aShot 1.5.3
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
・ 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();
The basic operations required for the test are explained below.
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.
Return
webDriver.navigate().back();
move on
webDriver.navigate().forward();
update
webDriver.navigate().refresh();
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.
Get the HTML id element of the button and click
webDriver.findElement(By.id("hoge")).click();
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.
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.
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 "));
【test scenario】
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();
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.
· Difference between webdriver.get () and webdriver.navigate () ・ Selenium Quick Reference
Recommended Posts