[JAVA] From setup to usage of Selenium wrapper Selenide

Introduction

Now that I know Selenium's easy-to-use wrapper Selenide, I've summarized everything from setup to sample creation.

Even the sample that actually works is written, so if you want to make it work, please try it.

The sample is a simple Servlet that just displays the text in the text box, and the image of the automated test is as follows. The implementation of the Servlet is described in Appendix 2 at the end of this article.

image.png

Reference URL

The URLs I referred to are as follows. Everywhere was a very good reference. Since the version of Selenide has been updated and the link has been broken, this article was created with the information as of 2018/01.

What is Selenide

Selenide is a wrapper for Selenium, a UI testing framework. As for the impression I used, it seemed to be quick and the learning cost was low.

Annotating the excerpt from the description of Official page http://selenide.org/

Selenide is a framework for test automation powered by Selenium WebDriver  
that brings the following advantages:

- Concise fluent API for tests
- Ajax support for stable tests
- Powerful selectors
- Simple configuration

(Selenide is a Selenium WebDriver-based automated testing framework.
It has the following advantages:
 -Concise and fluid test API
 -Supports Ajax for stable testing
 -Powerful selector function *
* Selectors such as ID, CSS, and query can be used.
 -Easy setting)

It looks like.

Settings for using Selenide

If you are using maven, add the following to pom.xml.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>selenideexample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>selenideexample</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
<!--Here is the setting of Selenide-->
	<dependency>
	    <groupId>com.codeborne</groupId>
	    <artifactId>selenide</artifactId>
	    <version>4.9.1</version>
	</dependency>
<!--This is the Selenide setting-->
	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.12</version>
	</dependency>
  </dependencies>
</project>

Other settings such as gradle and ivy are described in detail in 1) of the reference URL.

By the way, the latest version of Selenide can be found by searching on Google and maven Selenide. You can find it at https://mvnrepository.com/artifact/com.codeborne/selenide. You can also copy the description of dipendency of maven and gradle here.

Test code

Preparation before that

It is necessary to deploy Selenium WebDriver and ChromeDriver.exe in advance. See Appendix 3 and code comments at the end of this article. It is also written in 3) of the reference URL. (However, the download link was broken.)

JUnit code

The Java test code is below.

EchoJspTest.java


package com.example;

import org.junit.BeforeClass;
import org.junit.Test;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Condition;
//import static com.codeborne.selenide.Condition.*;//You may static import.
import com.codeborne.selenide.Selenide;
//import static com.codeborne.selenide.Selenide.*;//You may static import.
import com.codeborne.selenide.WebDriverRunner;

public class EchoJspTest {
	
	@BeforeClass
	public static void beforeClass() {
		//By default, FireFox is used, so Chrome is set to be used.
		Configuration.browser = WebDriverRunner.CHROME;
		
		//Advance preparation
		// 1) <project>/lib/make.(lib can be any name and location)
		// 2)Java Web Driver client-combined-3.8.1.jar and libs<project>/lib/Place in.
		//Java driver(jar)Is http://docs.seleniumhq.org/download/You can download it from.
		//    "Selenium Client & WebDriver Language Bindings"Click the Language Java Download link in.
		// 3) 2)Add all of the jars to the library path.
		// 4) <project>/lib/chromedirver.Put the exe.
		//exe is https://sites.google.com/a/chromium.org/chromedriver/You can download it from downloads.
		
		//Chromedriver as below.Specify the exe path in the system properties.
		System.setProperty("webdriver.chrome.driver", "./lib/chromedriver.exe");
	}
	
	@Test
	public void echoButtonTest() {
		//Selenide when statically imported.And Condition.Erase.
		//It's more readable, but I dare to use normal import for learning.
		Selenide.open("http://localhost:8080/selenideexample/EchoServlet");
		Selenide.$("#input_text").val("Hello World");
		Selenide.$("#echo_button").click();
		Selenide.$("#output_text").shouldHave(Condition.text("Hello World"));
	}

}

In the echoButtonTest method Select the text box with the ID input_text and enter the value, After that, click the button with the ID echo_button, Then check the value of the field with the ID output_text doing.

I think it will be easier to understand if you also look at the above image and the JSP in the appendix.

point

--BeforeClass () does the following: --By default, FireFox is used, so I try to use Chrome. --The path of ChromeDriver.exe is set in the system properties. --The test code is implemented as follows. --Open the URL with open ("\ <url >") and --Element selection (using "$" for jQuery-like), --Execute the event (click (), pressEnter (), etc.) and --Evaluate with Condition (.value ("\ <expected value >"), .text ("\ <expected value >")).

Summary

As you can see on the official page, the API is easy to understand and you can easily write the test, and you can implement the test without worrying about opening and closing the browser, timeout, etc.

appendix

1. File structure of the entire project

The location of the above code and Servlet code, JSP, Web.xml, pom.xml is as follows.


|   pom.xml   ........... ★pom.xml file
|
+---lib   ...............★ Web Driver and chorome driver.Where to put the exe
|   |   chromedriver.exe
|   |   client-combined-3.8.1.jar
|   |
|   `---libs
|           byte-buddy-1.7.5.jar
|           commons-codec-1.10.jar
|           commons-exec-1.3.jar
|           commons-logging-1.2.jar
|           gson-2.8.2.jar
|           guava-23.0.jar
|           httpclient-4.5.3.jar
|           httpcore-4.4.6.jar
|
+---src
|   +---main
|   |   `---java
|   |       `---com
|   |           `---example
|   |                   EchoServlet.java   .....★ Servlet
|   |
|   `---test
|       `---java
|           `---com
|               `---example
|                       EchoJspTest.java   .....★ Test case
|
`---WebContent
    +---META-INF
    |       MANIFEST.MF
    |
    `---WEB-INF
        |   echo.jsp   ......................... ★JSP
        |   web.xml   .......................... ★web.xml
        |
        `---lib

The web.xml is as follows.

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0" 
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<servlet>
		<servlet-name>selenide-example</servlet-name>
		<servlet-class>com.example.EchoServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>selenide-example</servlet-name>
		<url-pattern>/EchoServlet</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>echo.jsp</welcome-file>
	</welcome-file-list>
</web-app>

2. Test Servlet and JSP code

The Servlet simply displays with Get, fills the values with POST, and calls doGet.

EchoServlet.java


package com.example;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EchoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/echo.jsp");
		dispatcher.forward(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String val = req.getParameter("echo");
		System.out.println(val);
		req.setAttribute("echo", val);
		doGet(req, resp);
	}
}

The JSP is as shown in the top image. \ <div id = "output_text" > to take the text It is described as.

echo.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Echo</title>
</head>
<body>
	<form method="post" action="./EchoServlet">
		<input id="input_text" type="text" name="echo"/>
		<br/>
		<button id="echo_button" type="submit">Echo</button>
		<br/>
		<div id="output_text"><c:out value="${echo}" /></div>
	</form>
</body>
</html>

3. Set up Selenium WebDriver and ChromeDriver.exe

As described in the test code preparation, if you want to start with Chrome, do the following setup.

  1. Download the Selenium jar From http://docs.seleniumhq.org/download/ Located under Selenium Client & WebDriver Language Bindings Click Java Download (not at the top)  image.png
  2. Add the downloaded jar to your library path (For Eclipse, add it from Build Path with Add Jar on the Libraries tab.)  image.png
  3. Download ChromeDirver.exe and place it anywhere under your project Download from https://sites.google.com/a/chromium.org/chromedriver/downloads

that's all.

Recommended Posts

From setup to usage of Selenium wrapper Selenide
From introduction to usage of byebug
From introduction to use of ActiveHash
From pulling docker-image of rails to launching
Summary of moss when updating from JMockit 1.4 to 1.30
From the introduction of devise to the creation of the users table
How to write Scala from the perspective of Java
The story of migrating from Paperclip to Active Storage
Java development for beginners to start from 1-Vol.1-eclipse setup
[Eclipse] Summary of environment settings * Updated from time to time
How to prevent editTextPreference of android PreferenceFragmentCompat from breaking
[Ubuntu20.04] From ROS-noetic installation to SLAM simulation of turtlebot3