|
| 1 | +import static org.junit.Assert.*; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.LinkedList; |
| 7 | + |
| 8 | +import org.apache.commons.io.FileUtils; |
| 9 | +import org.junit.After; |
| 10 | +import org.junit.Before; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | +import org.junit.runners.Parameterized; |
| 14 | +import org.openqa.selenium.By; |
| 15 | +import org.openqa.selenium.OutputType; |
| 16 | +import org.openqa.selenium.Platform; |
| 17 | +import org.openqa.selenium.TakesScreenshot; |
| 18 | +import org.openqa.selenium.WebDriver; |
| 19 | +import org.openqa.selenium.WebElement; |
| 20 | +import org.openqa.selenium.remote.Augmenter; |
| 21 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 22 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 23 | + |
| 24 | +@RunWith(Parallelized.class) |
| 25 | +public class JUnitParallelSample { |
| 26 | + private String platform; |
| 27 | + private String browserName; |
| 28 | + private String browserVersion; |
| 29 | + |
| 30 | + @Parameterized.Parameters |
| 31 | + public static LinkedList getEnvironments() throws Exception { |
| 32 | + LinkedList<String[]> env = new LinkedList(); |
| 33 | + |
| 34 | + env.add(new String[]{Platform.XP.toString(), "chrome", "27"}); |
| 35 | + env.add(new String[]{Platform.WINDOWS.toString(),"firefox","20"}); |
| 36 | + env.add(new String[]{Platform.WINDOWS.toString(),"ie","7"}); |
| 37 | + env.add(new String[]{Platform.WINDOWS.toString(),"opera","12.14"}); |
| 38 | +//add more browsers here |
| 39 | + |
| 40 | + return env; |
| 41 | + } |
| 42 | + |
| 43 | + public JUnitParallelSample(String platform, String browserName, String browserVersion) { |
| 44 | + this.platform = platform; |
| 45 | + this.browserName = browserName; |
| 46 | + this.browserVersion = browserVersion; |
| 47 | + } |
| 48 | + |
| 49 | + private WebDriver driver; |
| 50 | + |
| 51 | + @Before |
| 52 | + public void setUp() throws Exception { |
| 53 | + DesiredCapabilities capability = new DesiredCapabilities(); |
| 54 | + capability.setCapability("platform", platform); |
| 55 | + capability.setCapability("browser", browserName); |
| 56 | + capability.setCapability("browserVersion", browserVersion); |
| 57 | + capability.setCapability("build", "JUnit-Parallel"); |
| 58 | + driver = new RemoteWebDriver(new URL("http://USERNAME:ACCESS_KEY@hub.browserstack.com/wd/hub"), capability); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testSimple() throws Exception { |
| 63 | + driver.get("http://www.google.com"); |
| 64 | + String title = driver.getTitle(); |
| 65 | + System.out.println("Page title is: " + title); |
| 66 | + assertEquals("Google", title); |
| 67 | + WebElement element = driver.findElement(By.name("q")); |
| 68 | + element.sendKeys("Browser Stack"); |
| 69 | + element.submit(); |
| 70 | + driver = new Augmenter().augment(driver); |
| 71 | + File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); |
| 72 | + try { |
| 73 | + FileUtils.copyFile(srcFile, new File("Screenshot.png")); |
| 74 | + } catch (IOException e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @After |
| 80 | + public void tearDown() throws Exception { |
| 81 | + driver.quit(); |
| 82 | + } |
| 83 | +} |
0 commit comments