0
1

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 1 year has passed since last update.

【Selenide + Eclipse + Servlet/JSP】コピペでできるテスト自動化

Last updated at Posted at 2023-12-24

はじめに

本記事ではselenide + Eclipse + Servlet/JSPを使ったテスト自動化の導入方法について説明します。特にテスト自動化ツールを導入したことのない人向けに、行うべき作業を細かく説明しているので、テスト自動化を導入したいけど何からやっていいかわからないという方は参考にしてください

前提条件

  • Fire Foxがインストールされている(インストールしていない人はインストールしてください)
  • Eclipseを使ってServlet/JSPのプロジェクトを実行できる

目次

  1. とりあえずやってみる
  2. 参考文献

とりあえずやってみる

プロジェクトの作成

ファイル→新規→動的Webプロジェクトをクリック

image.png

プロジェクト名にAutomationTestingを入力して完了をクリック

image.png

プロジェクトの構成変更

AutomationTestingプロジェクトを右クリック→構成→Maven プロジェクトへ変換をクリック(構成は↓をクリックしてメニューをスクロールしないとでてきません)

image.png

完了をクリック

image.png

xmlの編集

AutomationTesting/pom.xmlを右クリック→次で開く→テキストエディターをクリック

image.png

内容を以下の通り編集

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>AutomationTesting</groupId>
  <artifactId>AutomationTesting</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
	    <dependency>
    	    <groupId>com.codeborne</groupId>
        	<artifactId>selenide</artifactId>
        	<version>4.9.1</version>
    	</dependency>
    	<dependency>
        	<groupId>junit</groupId>
        	<artifactId>junit</artifactId>
        	<version>4.12</version>
    	</dependency>
  </dependencies>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

Selenideを使ったテストコードの作成

Javaリソース/srcを右クリック→新規→クラスをクリック

image.png

パッケージをtest、名前をExecTestにして完了をクリック

image.png

ExecTestの内容を以下のように変更

ExecTest.java
package test;

import static com.codeborne.selenide.Condition.*;

import org.junit.Test;

import com.codeborne.selenide.Selenide;


public class ExecTest {
	@Test
	public void executeTest() {
		// test.jspにアクセス
	    Selenide.open("http://localhost:8080/AutomationTesting/test/test.jsp");
	    // Hello Worldという内容が書かれているかチェック
	    Selenide.$(".test").shouldHave(text("Hello World"));
	}
}

テストの対象となるページの作成

WebContentを右クリック→新規→フォルダーをクリック

image.png

フォルダー名をtestにして完了をクリック

image.png

先ほど作成した完了フォルダーを右クリック→新規→JSP ファイルをクリック

image.png

ファイル名をjspとして完了をクリック

image.png
test.jspの内容を以下のように変更

test.jsp
<!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>
    <div class='test'>
    	Hello World
    </div>
</body>
</html>

動作確認のため実行ボタン→実行→サーバーで実行をクリック

image.png

完了をクリック

image.png

Hello Worldがでればok

image.png

テストの実行

ExecTest.javaが開かれた状態で実行ボタン→実行→JUnitテストをクリック→JUnitのタブで「実行1/1」となっていれば成功です

image.png

これで作業は完了です。お疲れさまでした。

参考文献

本記事の執筆にあたっては以下の記事を参考にしました。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?