diff --git a/README.md b/README.md index 53854b05..bf577889 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,8 @@ Master branch contains **Selenium 4** samples, for **Selenium 3 - JSON Wire Prot ### Running your tests -- To run a single test, run `mvn test -P single` +- To run tests, run `mvn test -P parallel` - To run local tests, run `mvn test -P local` -- To run parallel tests, run `mvn test -P parallel` - To run the test suite, run `mvn test -P suite` Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github) @@ -34,9 +33,8 @@ Master branch contains **Selenium 4** samples, for **Selenium 3 - JSON Wire Prot ### Running your tests -- To run a single test, run `gradle singleTest` +- To run tests, run `gradle parallelTest` - To run local tests, run `gradle localTest` -- To run parallel tests, run `gradle parallelTest` - To run the test suite, run `gradle suiteTest` Understand how many parallel sessions you need by using our [Parallel Test Calculator](https://www.browserstack.com/automate/parallel-calculator?ref=github) diff --git a/config/parallel.testng.xml b/config/parallel.testng.xml index 24c4721f..c887ae7b 100644 --- a/config/parallel.testng.xml +++ b/config/parallel.testng.xml @@ -1,8 +1,8 @@ + - @@ -10,7 +10,6 @@ - @@ -18,7 +17,6 @@ - diff --git a/pom.xml b/pom.xml index ac200e08..d20dbef8 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,11 @@ org.apache.maven.plugins maven-surefire-plugin 2.18.1 + + + config/parallel.testng.xml + + diff --git a/src/test/java/com/browserstack/BrowserStackTestNGTest.java b/src/test/java/com/browserstack/BrowserStackTestNGTest.java index cd77b364..235597e0 100644 --- a/src/test/java/com/browserstack/BrowserStackTestNGTest.java +++ b/src/test/java/com/browserstack/BrowserStackTestNGTest.java @@ -13,23 +13,55 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; +import org.testng.annotations.*; public class BrowserStackTestNGTest { public WebDriver driver; private Local l; + private static JSONObject config; + private static Map commonCapabilities; + private static String username; + private static String accessKey; + + @BeforeSuite(alwaysRun=true) + @Parameters(value = { "config" }) + public void beforeSuite(String config_file) throws Exception { + JSONParser parser = new JSONParser(); + config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file)); + commonCapabilities = (Map) config.get("capabilities"); + + username = System.getenv("BROWSERSTACK_USERNAME"); + if (username == null) { + username = (String) config.get("user"); + } + + accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); + if (accessKey == null) { + accessKey = (String) config.get("key"); + } + try { + if ((commonCapabilities.get("browserstack.local") != null && + commonCapabilities.get("browserstack.local").toString().equalsIgnoreCase("true") && (l == null))) { + l = new Local(); + Map options = new HashMap(); + options.put("key", accessKey); + l.start(options); + } + } catch (Exception e) { + System.out.println("Error while start local - " + e); + } + } @BeforeMethod(alwaysRun = true) @org.testng.annotations.Parameters(value = { "config", "environment" }) @SuppressWarnings("unchecked") public void setUp(String config_file, String environment) throws Exception { JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file)); + config = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + config_file)); JSONObject envs = (JSONObject) config.get("environments"); - DesiredCapabilities capabilities = new DesiredCapabilities(); - capabilities.setCapability("browserstack.source", "testng:sample-selenium-3:v1.0"); + + capabilities.setCapability("browserstack.source", "testng:sample-selenium-3:v1.1"); Map envCapabilities = (Map) envs.get(environment); Iterator it = envCapabilities.entrySet().iterator(); @@ -47,24 +79,6 @@ public void setUp(String config_file, String environment) throws Exception { } } - String username = System.getenv("BROWSERSTACK_USERNAME"); - if (username == null) { - username = (String) config.get("user"); - } - - String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); - if (accessKey == null) { - accessKey = (String) config.get("key"); - } - - if (capabilities.getCapability("browserstack.local") != null - && capabilities.getCapability("browserstack.local") == "true") { - l = new Local(); - Map options = new HashMap(); - options.put("key", accessKey); - l.start(options); - } - driver = new RemoteWebDriver( new URL("https://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities); } @@ -72,8 +86,10 @@ public void setUp(String config_file, String environment) throws Exception { @AfterMethod(alwaysRun = true) public void tearDown() throws Exception { driver.quit(); - if (l != null) { - l.stop(); - } + } + + @AfterSuite(alwaysRun = true) + public void afterSuite() throws Exception { + if (l != null) l.stop(); } }