Try Selenium

Introduction

This is a continuation of Try Mockito. Let's move Selenium and so on.

Selenium Official: https://www.seleniumhq.org/

Modify Maven config file

--Added dependent library to pom.xml

pom.xml


<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.12.0</version>
</dependency>

--Reference - https://search.maven.org/#artifactdetails|org.seleniumhq.selenium|selenium-java|3.12.0|jar

Creating sample code

--Download chrome driver - http://chromedriver.chromium.org/downloads --Access the above URL → Select the latest version of driver → Select the one of your own PC OS

SeleniumMain.java


package com.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SeleniumMain {

	public static void main(String[] args) {
		final String PATH = "(Location where the chrome driver is stored)";
		System.setProperty("webdriver.chrome.driver", PATH);
		WebDriver driver = new ChromeDriver();

		final String URL = "http://www.google.com";
		driver.get(URL);

		WebElement element = driver.findElement(By.name("q"));
		element.sendKeys("selenium");
		element.submit();

		new WebDriverWait(driver, 10).until(new ExpectedCondition<Boolean>() {
			public Boolean apply(WebDriver d) {
				return d.getTitle().toLowerCase().startsWith("selenium");
			}
		});

		System.out.println("Page title is: " + driver.getTitle());
		driver.quit();
	}
}

--Run SeleniumMain.java to open Chrome browser and search for selenium

Convert to a dynamic WEB project

--Select Project → Properties → Project Facets → Select Dynamic Web Module --Added dependent library to pom.xml

pom.xml


<dependency>
  <groupId>tomcat</groupId>
  <artifactId>servlet</artifactId>
  <version>4.1.36</version>
</dependency>

--Reference - https://search.maven.org/#artifactdetails|tomcat|servlet|4.1.36|N%2FA

Creating a file to be tested

--Create an index.jsp file directly under WebContent

index.jsp


<%@ page import="java.util.* "%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	String pageTitle = "Fruits Shop";
	List<String> fruitsList = Arrays.asList("Apple", "Banana", "Cherry");
%>
<!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=UTF-8">
<title><%= pageTitle %></title>
</head>
<body>
	<h1><%= pageTitle %></h1>
	<ul>
		<%
		for (String fruits: fruitsList) {
			out.println("<li>" + fruits + "</li>");
		}
		%>
	</ul>
</body>
</html>

--Deploy to tomcat (8.0) and run --Access http: // localhost: 8080 / maven_sample / index.jsp

Creating test code with Selenium

SeleniumTest.java


package com.example;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.List;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumTest {

	@Test
	public void test() {
		final String PATH = "(Location where the chrome driver is stored)";
		System.setProperty("webdriver.chrome.driver", PATH);
		WebDriver driver = new ChromeDriver();

		final String URL = "http://localhost:8080/maven_sample/index.jsp";
		driver.get(URL);

		String expectPageTitle = "Fruits Shop";
		String pageTitle = driver.getTitle();
		assertThat(pageTitle, is(expectPageTitle));

		WebElement h1 = driver.findElement(By.tagName("h1"));
		assertThat(h1.getText(), is(expectPageTitle));

		List<WebElement> liList = driver.findElements(By.tagName("li"));
		assertThat(liList.size(), is(3));
		assertThat(liList.get(0).getText(), is("Apple"));
		assertThat(liList.get(1).getText(), is("Banana"));
		assertThat(liList.get(2).getText(), is("Cherry"));

		driver.quit();
	}

}

in conclusion

I managed to use Selenium. This will scrape, and ... I'm glad if you can use it as a reference. → Try Selenide

Recommended Posts

Try Selenium
Selenium
Try HiveRunner
Try Mockito
Try DbUnit
try docker-compose
Try Lombok
Try App Clips
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Selenium sample (Java)
Try using sql-migrate
Selenium x Java
Try using SwiftLint
Try using Log4j 2.0
Try Ruby Minitest
Roughly try Java 9