Skip to content

Instantly share code, notes, and snippets.

@poad
Created May 24, 2014 18:39
Show Gist options
  • Save poad/40dc353909ce811bdc9a to your computer and use it in GitHub Desktop.
Save poad/40dc353909ce811bdc9a to your computer and use it in GitHub Desktop.
Selenium2 WebDriverを使ってみる ref: http://qiita.com/poad1010/items/77512f4c629b5985c16b
package com.github.poad.example.selenium;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* Google検索してみるサンプル
*
*/
public class TestMain {
private WebDriver driver;
@Before
public void setUp() {
// Firefoxを起動
this.driver = new FirefoxDriver();
}
@Test
public void test() {
// google.co.jp を開く
this.driver.get("http://google.co.jp/");
// 検索フォームの要素を取得
WebElement searchForm = this.driver.findElement(By.id("lst-ib"));
Assert.assertNotNull(searchForm);
// 検索フォームに google と入力する
searchForm.sendKeys("google");
// 検索ボタン要素を取得
WebElement searchBtn = this.driver.findElement(By.cssSelector(".jsb > center:nth-child(1) > input:nth-child(1)"));
// 検索を実行する
searchBtn.click();
// 検索結果ページからGoogleのロゴ要素(のホームへのリンク要素)を取得
WebElement homeLink = this.driver.findElement(By.cssSelector("a#logo"));
Assert.assertEquals("a", homeLink.getTagName());
}
@After
public void tearDown() {
// Firefoxを終了
this.driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment