Skip to content

Instantly share code, notes, and snippets.

@tomoTaka01
Created November 11, 2012 06:07
Show Gist options
  • Save tomoTaka01/4053894 to your computer and use it in GitHub Desktop.
Save tomoTaka01/4053894 to your computer and use it in GitHub Desktop.
Web test by selenium(with radio, select menu)
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
public class HelloSampleTest {
private static final String HELLO_XHTML = "http://localhost:8080/HelloSample/faces/Hello.xhtml";
private static final String filePath = "/Users/tomo/HelloSampleTest/";
private static WebDriver driver = null;
private final int timeoutInSeconds = 5;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
driver = new FirefoxDriver(); // ★テスト実行前に一度インスタンス作成
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
if (driver != null){
driver.close(); //★全テスト完了後にインスタンス破棄
}
}
@Test
public void hello() throws IOException {
// ★1最初の画面起動
driver.get(HELLO_XHTML);
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.titleIs("Hello"));
WebElement name = driver.findElement(By.id("form1:name"));
// ★2ここでnameテキストに「selenium」と入力
name.sendKeys("selenium");
// ★3ラジオボタン「Hello」を選択
WebElement greetSel = driver.findElement(By.xpath("//input[@name='form1:greetSel' and @value='1']"));
greetSel.click();
// ★4セレクトメニュー「English」を選択
Select lanSel = new Select(driver.findElement(By.name("form1:language")));
lanSel.selectByValue("1");
// ★5画面スクリーンショット(入力画面のエビデンス)
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(filePath + "Hello_1entry.png"));
WebElement greetBtn = driver.findElement(By.id("form1:greet"));
// ★6「greet」ボタンをクリック
greetBtn.click();
// ★7画面遷移をtitleで確認
wait.until(ExpectedConditions.titleIs("Greeting"));
WebElement element = driver.findElement(By.tagName("h1"));
// ★8h1タグの内容を検証
Assert.assertEquals("Hello selenium", element.getText());
// ★9画面スクリーンショット(greet画面のエビデンス)
file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(filePath + "Hello_2greet.png"));
}
@Test
public void こんにちは() throws IOException {
// ★1最初の画面起動
driver.get(HELLO_XHTML);
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.titleIs("Hello"));
WebElement name = driver.findElement(By.id("form1:name"));
// ★2ここでnameテキストに「selenium」と入力
name.clear(); // 入力内容をクリア
name.sendKeys("selenium");
// ★3ラジオボタン「Hello」を選択
WebElement greetSel = driver.findElement(By.xpath("//input[@name='form1:greetSel' and @value='1']"));
greetSel.click();
// ★4セレクトメニュー「Japanese」を選択
Select lanSel = new Select(driver.findElement(By.name("form1:language")));
lanSel.selectByValue("2");
// ★5画面スクリーンショット(入力画面のエビデンス)
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(filePath + "こんにちは_1entry.png"));
WebElement greetBtn = driver.findElement(By.id("form1:greet"));
// ★6「greet」ボタンをクリック
greetBtn.click();
// ★7画面遷移をtitleで確認
wait.until(ExpectedConditions.titleIs("Greeting"));
WebElement element = driver.findElement(By.tagName("h1"));
// ★8h1タグの内容を検証
Assert.assertEquals("こんにちは selenium", element.getText());
// ★9画面スクリーンショット(greet画面のエビデンス)
file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File(filePath + "こんにちは_2greet.png"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment