Skip to content

Instantly share code, notes, and snippets.

@hatena-iti
Created July 9, 2014 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatena-iti/82c32748923f018c18b8 to your computer and use it in GitHub Desktop.
Save hatena-iti/82c32748923f018c18b8 to your computer and use it in GitHub Desktop.
package jp.co.iti.test;
import org.junit.After;
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.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.remote.Augmenter;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.net.URL;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
import static org.junit.Assert.*;
public class SafariTest {
private WebDriver driver;
@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "iPhone Retina (3.5-inch) Simulator");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "7.1");
capabilities.setCapability("browserName", "safari");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"),
capabilities);
Augmenter augmenter = new Augmenter();
driver = augmenter.augment(driver);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void runTest() throws Exception {
Wait<WebDriver> wait = new WebDriverWait(driver, 30);
driver.get("http://saucelabs.com/test/guinea-pig");
WebElement idElement = driver.findElement(By.id("i_am_an_id"));
assertNotNull(idElement);
assertEquals("I am a div", idElement.getText());
WebElement commentElement = driver.findElement(By.id("comments"));
assertNotNull(commentElement);
commentElement.sendKeys("This is an awesome comment");
getScreenshotAs("safari_capture01.jpg");
WebElement submitElement = driver.findElement(By.id("submit"));
assertNotNull(submitElement);
submitElement.click();
boolean ret = wait.until(textToBePresentInElement(By.id("your_comments"), "This is an awesome comment"));
System.out.println(ret);
WebElement yourCommentsElement = driver.findElement(By.id("your_comments"));
assertNotNull(yourCommentsElement);
assertTrue(driver.findElement(By.id("your_comments")).getText().contains("This is an awesome comment"));
getScreenshotAs("safari_capture02.jpg");
System.out.println(driver.getCurrentUrl());
}
@After
public void tearDown() throws Exception {
driver.quit();
}
private void getScreenshotAs(String path) {
TakesScreenshot ts = (TakesScreenshot) driver;
try {
FileUtils.copyFile(
ts.getScreenshotAs(OutputType.FILE),
new File(path));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment