Windows + Java 環境で Selenium3 を使ってテストの自動化を行います。
Maven で Selenium のテストプロジェクト作り、サービスのテストを実行します。
Contents
目的
リグレッションテストを行えるようにします。
(追加、改修により既存機能への影響がないかを確認する回帰テスト)
環境準備
- Java(JDK)
- Maven
環境変数の設定
JAVA_HOME
1 |
C:\Program Files\Java\jre1.8.0_111 |
Path(パスを通したところに設置)
1 |
C:\work\apache-maven-3.3.9\bin |
確認
1 |
C:>mvn --version |
Mavenの archetype:generate を使ってプロジェクトを作ります。
1 |
cd C:\work\pleiades\workspace |
maven-archetype-quickstart をベースに作成
1 2 3 4 5 6 7 8 9 10 11 |
mvn archetype:generate Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 888: maven-archetype-quickstart Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): 1: 1 Choose a number: 6: 6 Define value for property 'groupId': : com.vistylee(任意のプロダクト) Define value for property 'artifactId': : vistylee-test(任意のID) Define value for property 'version': 1.0-SNAPSHOT: : 0.1.0(任意のバージョン) Define value for property 'package': ...: (何も入力せずENTER) Y: : Y |
vistylee-testプロジェクトが作成されます。
移動
1 |
<span style="font-size: 1.0625rem;">cd </span><span style="font-size: 1.0625rem;">vistylee-test</span> |
pom.xml を確認
1 |
C:\work\pleiades\workspace\vistylee-test\pom.xml |
pom.xmlを編集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.vistylee</groupId> <artifactId>selenium-java</artifactId> <version>3.0.1</version> <scope>test</scope> </dependency> </dependencie> |
pomの準備は完了
Eclipseプロジェクトを作る
1 |
c:\work>cd vistylee-test |
1 |
mvn eclipse:eclipse |
テストコードの記述をします。
Chrome Driver
Selenium-WebDriverを使ってGoogleChromeを操作します。
Chrome Driverのダウンロード
下記から Chrome 用の WebDriverをダウンロードできます。
Downloads - ChromeDriver - WebDriver for Chrome
ダウンロード後に任意の場所に移動させます。
移動後に C:\work\tools\chromedriver.exe など任意の場所のパスを通します。
Mavenの設定
Chrome Driverを使えるようにMavenの設定をします。
pom.xmlのシステムプロパティにChrome Driverのpath設定を記述します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.15</version> <configuration> <systemPropertyVariables> <webdriver.chrome.driver>C:\work\tools\chromedriver.exe</webdriver.chrome.driver> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> |
テスト実行
1 |
mvn -DargLine="-Dwebdriver.chrome.driver=C:\work\tools\chromedriver.exe" test |
Eclipse をインストール
Java 用 Selenium Client & WebDriver のダウンロード
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<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.vistylee</groupId> <artifactId>vistylee-test</artifactId> <version>0.1.0</version> <packaging>jar</packaging> <name>lab1</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.0.1</version> <scope>test</scope> </dependency> </dependencies> </project> |
テストコード1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.vistylee; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; /** * Hello world! * */ public class App { public static void main( String[] args ) { System.setProperty("webdriver.gecko.driver", "C:/work/tools/geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("http://vistylee.com/Test/"); WebElement element = driver.findElement(By.name("api")); String aaa = "curl -d '{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"getMypage\",\"params\":{\"header\":{\"apiVer\" :\"0.02\",\"appVer\":\"9999.01.01\",\"deviceId\":\"a1\",\"hash\":\"720ba2b7d3fa2f38afd9d37fbdc88e40\", \"os\":\"iOS\"},\"body\":{}}}' -H \"Content-Type: application/json\" https://vistylee.com/Api"; element.sendKeys(aaa); element.submit(); System.out.println("Page title is: " + driver.getTitle()); (new WebDriverWait(driver, 30)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().startsWith(aaa); } }); System.out.println("Page title is: " + driver.getTitle()); //driver.quit(); } } |
テストコード2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.<span style="font-size: 0.9375rem;">vistylee;</span> import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest() { //System.setProperty("webdriver.gecko.driver", "C:/work/tools/geckodriver.exe"); //WebDriver driver = new FirefoxDriver(); System.setProperty("webdriver.chrome.driver", "C:/work/tools/chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://vistylee.com/Test/"); WebElement element = driver.findElement(By.name("api")); String aaa = "aaabbbccc"; element.sendKeys(aaa); element.submit(); //driver.quit(); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Test */ public void testApp() { assertTrue( true ); } } |