|
| 1 | +package com.browserstack; |
| 2 | + |
| 3 | +import com.browserstack.local.Local; |
| 4 | + |
| 5 | +import org.testng.annotations.DataProvider; |
| 6 | +import org.testng.annotations.BeforeMethod; |
| 7 | +import org.testng.annotations.AfterMethod; |
| 8 | +import org.testng.annotations.BeforeSuite; |
| 9 | +import org.testng.annotations.AfterSuite; |
| 10 | +import org.testng.annotations.Factory; |
| 11 | +import org.testng.annotations.Test; |
| 12 | +import org.testng.Assert; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | +import java.net.URL; |
| 17 | +import java.util.HashMap; |
| 18 | + |
| 19 | +import org.apache.commons.io.FileUtils; |
| 20 | +import org.openqa.selenium.By; |
| 21 | +import org.openqa.selenium.Platform; |
| 22 | +import org.openqa.selenium.WebDriver; |
| 23 | +import org.openqa.selenium.WebElement; |
| 24 | +import org.openqa.selenium.OutputType; |
| 25 | +import org.openqa.selenium.TakesScreenshot; |
| 26 | +import org.openqa.selenium.remote.Augmenter; |
| 27 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 28 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 29 | + |
| 30 | +public class TestNGLocal { |
| 31 | + private String platform; |
| 32 | + private String browserName; |
| 33 | + private String browserVersion; |
| 34 | + private Local bsLocal; |
| 35 | + |
| 36 | + @Factory(dataProvider = "getBrowsers") |
| 37 | + public TestNGLocal(String platform,String browserName,String browserVersion) { |
| 38 | + this.bsLocal = new Local(); |
| 39 | + this.platform = platform; |
| 40 | + this.browserName = browserName; |
| 41 | + this.browserVersion = browserVersion; |
| 42 | + } |
| 43 | + |
| 44 | + private WebDriver driver; |
| 45 | + |
| 46 | + @BeforeSuite(alwaysRun=true) |
| 47 | + public void suiteSetup() throws Exception { |
| 48 | + String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); |
| 49 | + |
| 50 | + HashMap<String, String> bsLocalArgs = new HashMap<String, String>(); |
| 51 | + bsLocalArgs.put("key", accessKey); |
| 52 | + bsLocalArgs.put("forcelocal", ""); |
| 53 | + bsLocal.start(bsLocalArgs); |
| 54 | + } |
| 55 | + |
| 56 | + @BeforeMethod(alwaysRun=true) |
| 57 | + public void setUp() throws Exception { |
| 58 | + String username = System.getenv("BROWSERSTACK_USERNAME"); |
| 59 | + String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); |
| 60 | + |
| 61 | + DesiredCapabilities capability = new DesiredCapabilities(); |
| 62 | + capability.setCapability("platform",platform); |
| 63 | + capability.setCapability("browser", browserName); |
| 64 | + capability.setCapability("browserVersion", browserVersion); |
| 65 | + capability.setCapability("name", "Sample TestNG Local Test"); |
| 66 | + capability.setCapability("build", "Sample TestNG Tests"); |
| 67 | + capability.setCapability("browserstack.local", "true"); |
| 68 | + |
| 69 | + driver = new RemoteWebDriver(new URL("http://"+username+":"+accessKey+"@hub.browserstack.com/wd/hub"), capability); |
| 70 | + } |
| 71 | + |
| 72 | + @Test(groups = { "local_test" }) |
| 73 | + public void testSimple() throws Exception { |
| 74 | + this.driver.get("http://www.google.com"); |
| 75 | + System.out.println("Page title is: " + driver.getTitle()); |
| 76 | + Assert.assertEquals("Google", driver.getTitle()); |
| 77 | + WebElement element = driver.findElement(By.name("q")); |
| 78 | + element.sendKeys("BrowserStack"); |
| 79 | + element.submit(); |
| 80 | + driver = new Augmenter().augment(driver); |
| 81 | + File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); |
| 82 | + try { |
| 83 | + FileUtils.copyFile(srcFile, new File("Screenshot.png")); |
| 84 | + } catch (IOException e) { |
| 85 | + e.printStackTrace(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @AfterMethod(alwaysRun=true) |
| 90 | + public void tearDown() throws Exception { |
| 91 | + if(driver != null) { |
| 92 | + driver.quit(); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @AfterSuite(alwaysRun=true) |
| 97 | + public void afterSuite() throws Exception { |
| 98 | + if(bsLocal != null) { |
| 99 | + bsLocal.stop(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @DataProvider(name = "getBrowsers") |
| 104 | + public static Object[][] createData1() { |
| 105 | + return new Object[][] { |
| 106 | + { Platform.WINDOWS.toString(), "chrome", "48" }, |
| 107 | + { Platform.XP.toString(), "firefox", "44"}, |
| 108 | + }; |
| 109 | + } |
| 110 | +} |
0 commit comments