6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Seleniumをやってみる

Last updated at Posted at 2018-06-04

はじめに

Mockitoをやってみるの続きです。
Seleniumとやらを動かしてみます。

Selenium公式: https://www.seleniumhq.org/

Mavenの設定ファイルの修正

  • pom.xmlに依存ライブラリを追記
pom.xml
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.12.0</version>
</dependency>

サンプルコードの作成

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 = "(chromedriverの格納されている場所)";
		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();
	}
}
  • SeleniumMain.javaを実行すると、Chromeブラウザを開いてseleniumを検索する

動的WEBプロジェクトへ変換

  • プロジェクトを選択→プロパティ→プロジェクトファセット→動的Webモジュールを選択
  • pom.xmlに依存ライブラリを追記
pom.xml
<dependency>
  <groupId>tomcat</groupId>
  <artifactId>servlet</artifactId>
  <version>4.1.36</version>
</dependency>

テスト対象ファイルの作成

  • WebContent直下にindex.jspファイルを作成する
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>

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 = "(chromedriverの格納されている場所)";
		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();
	}

}

おわりに

なんとかSeleniumを使えました。
これでスクレイピングがで、き、、、
参考になれば幸いです。
Selenideをやってみる

6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?