■ スナップショットを撮る
import static org.junit.Assert.*; import java.io.File; import org.apache.commons.io.FileUtils; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class FirefoxSample { WebDriver driver; @Before public void setUp() throws Exception { this.driver = new FirefoxDriver(); } @After public void tearDown() throws Exception { this.driver.quit(); } @Test public void test() { this.driver.get("http://www.yahoo.co.jp/"); this.takeScreenshot(driver); } // ★ここ★ private void takeScreenshot(WebDriver driver) { try { FileUtils.copyFile( ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE), new File("C:/temp/sample01.png")); } catch (java.io.IOException e) { } } }
参考文献
http://blog.asial.co.jp/1180
http://qiita.com/myhr/items/dff7cee30182ab56f737
■ frameの移動
// <frameset rows="100,*"><frame src="index.html" name="frameName">... driver.switchTo().frame("frameName");
■ 別Windowを開く
// 現在のWindowHandleをとっておく String currentWindowHandle = driver.getWindowHandle(); // 別Windowを開く driver.findElement(By.linkText("link")).click(); // 別Windowに移動 // window.open("http://www.yahoo.co.jp/", "WindowName" , "width=210,height=160"); driver.switchTo().window("WindowName"); // 何らかの処理 // 元に戻す driver.switchTo().window(currentWindowHandle);
参考文献
http://satussy.blogspot.jp/2012/09/selenium-webdrivertargetblank.html
■ 活性/非活性の確認
boolean isEnabled = driver.findElement(By.id("buttonID")).isEnabled();
■ Maxlengthの確認
String maxlength = driver.findElement(By.xpath("//input[contains(@id,'idOfInput')]")).getAttribute("maxlength"); assertEquals("255", maxlength);
参考文献
http://tanmaysarkar.com/check-maxlength-and-readonly-property-of-inputbox-with-selenium-webdriver/
■ フォーカス位置の確認
boolean isOK = driver.findElement(By.id("textbox1")).equals(driver.switchTo().activeElement()); assertTrue(isOK);
参考文献
http://sqa.stackexchange.com/questions/12202/get-element-by-cursor-position-in-selenium
■ JavaScriptのダイアログ確認
Alert
button = driver.findElement(By.id("btnAlert")); button.click(); Alert alert = driver.switchTo().alert(); System.out.println("alert message: " + alert.getText()); alert.accept();
Confirm Dialog
button = driver.findElement(By.id("btnConfirm")); button.click(); Alert confirm = driver.switchTo().alert(); System.out.println("confirm message: " + confirm.getText()); // click "CANCEL" button. confirm.dismiss(); button.click(); // click "OK" button. confirm.accept(); Prompt button = driver.findElement(By.id("btnPrompt")); button.click(); Alert prompt = driver.switchTo().alert(); // set the prompt message. prompt.sendKeys("XXXX"); prompt.accept(); button.click(); prompt = driver.switchTo().alert(); prompt.sendKeys("YYYY"); prompt.dismiss();
参考文献
http://www.yoheim.net/blog.php?q=20111102
http://kencharos.hatenablog.com/entry/20111027/1319719613
■ Cookieを扱うには...
driver.get("http://www.yahoo.co.jp/"); for (Cookie cookie : this.driver.manage().getCookies()) { System.out.println(cookie.getName() + " : " + cookie.getValue()); } System.out.println("CookieName : " + driver.manage().getCookieNamed("CookieName"));
関連記事
Selenium ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2015/04/11/235336
Selenium WebDriver + Java ~ 初期設定編 ~
https://dk521123.hatenablog.com/entry/2015/05/12/230924