参考にさせて頂いたページ
自動テストはじめませんか?#1 | Developers.IO
Selenium 2.0 を使ってJavaからWeb画面をテスト - Taste of Tech Topics
IEDriverServerをダウンロード
http://www.seleniumhq.org/download/
のページの「The Internet Explorer Driver Server」の項目からダウンロードする。
32bit用と64bit用があるみたい。
私は今回IEDriverServer_x64_2.45.0.zip
をダウンロードしました。
seleniumをセットアップするときに使用したselenium-java-2.45.0.zip
とバージョンが同じの方が良いのかもしれないし、そうでないかもしれない。
IEの設定をする
ツール > インターネットオプション をクリック
セキュリティタブをクリック
- インターネット
- ローカルイントラネット
- 信頼済みサイト
- 制限付きサイト
のすべてについて、「保護モードを有効にする」にチェックを入れてから「適用」をクリックする。
それからIEを閉じる
HogeTest.javaの記述内容
以下のような感じにした。
package com.example.tests;
import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.junit.*;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HogeTest {
private WebDriver driver;
@Before
public void setUp(){
// Firefoxを起動する
// driver = new FirefoxDriver();
// Chromeを起動する
// System.setProperty("webdriver.chrome.driver","./driver/chromedriver.exe");
// driver = new ChromeDriver();
// IEを起動する
System.setProperty("webdriver.ie.driver", "./driver/IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
@Test
public void testSample(){
String[][] params;
// キャプチャを取得したいページ数
params = new String[3][];//params[0],params[1],params[2],の3つの配列を用意する
String[] param;
//params[0]の設定
param = new String[2];
param[0] = "https://www.google.co.jp/";
param[1] = generateFileName(param[0]);
params[0] = param;
//params[1]の設定
param = new String[2];
param[0] = "http://www.yahoo.co.jp/";
param[1] = generateFileName(param[0]);
params[1] = param;
//params[2]の設定
param = new String[2];
param[0] = "http://blog.asial.co.jp/1180";
param[1] = generateFileName(param[0]);
params[2] = param;
try {
for (int i = 0; i < params.length; i++) {
String[] option = params[i];
capture(option[0], option[1]);
}
} catch (WebDriverException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}//end of @Test
@After
public void tearDown(){
//ブラウザを閉じる
driver.quit();
}
public void capture(String url, String filename) throws Exception{
driver.get(url);
FileUtils.copyFile(
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE),
new File(filename)
);
}
public static String generateFileName(String str1){
String regex1 = "(http[s]*://[a-z]*.[a-z]*.[a-z]*.[a-z]*/)";
Pattern p1 = Pattern.compile(regex1);
Matcher m1 = p1.matcher(str1);
String str2 = m1.replaceAll("");
String regex2 = "(/)";
Pattern p2 = Pattern.compile(regex2);
Matcher m2 = p2.matcher(str2);
String str3 = m2.replaceAll("__");
return str3+".png";
}
}
上記のような感じにした。
これまでのコードに対して、
import org.openqa.selenium.ie.InternetExplorerDriver;
と
// IEを起動する
System.setProperty("webdriver.ie.driver", "./driver/IEDriverServer.exe");
driver = new InternetExplorerDriver();
を追記した感じ。
あとはいつもどおり実行すれば動く。
IE8でも動きました。
これで3つのブラウザで動くようになった。
FirefoxとChromeを起動する部分についてもコメント化して残しています。
なんかAndroidとかでも可能らしいけど、また今度。
今回はここまで。