Skip to content

Instantly share code, notes, and snippets.

@tomoTaka01
Created November 3, 2012 23:56
Show Gist options
  • Save tomoTaka01/429e4376f1af716e9f9f to your computer and use it in GitHub Desktop.
Save tomoTaka01/429e4376f1af716e9f9f to your computer and use it in GitHub Desktop.
my first selenium (for HelloSample)
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
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.WebDriverWait;
public class HelloSampleTest {
@Test
public void test() throws IOException {
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost:8080/HelloSample/faces/Hello.xhtml"); // ★1最初の画面起動
final int timeoutInSeconds = 5;
WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
wait.until(ExpectedConditions.titleIs("Hello"));
WebElement name = driver.findElement(By.id("form1:name"));
name.sendKeys("selenium"); // ★2ここでnameテキストに「selenium」と入力
WebElement greetBtn = driver.findElement(By.id("form1:greet"));
greetBtn.click(); // ★3「greet」ボタンをクリック
wait.until(ExpectedConditions.titleIs("Greeting")); // ★4画面遷移をtitleで確認
WebElement element = driver.findElement(By.tagName("h1"));
Assert.assertEquals(element.getText(), "Hello selenium"); // ★5h1タグの内容を検証
// ★この時点の画面をスクリーンショットをとって保存
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("test.png"));
driver.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment