diff --git a/junit-4/README.md b/junit-4/README.md index 30e5cc1..d4ad85d 100644 --- a/junit-4/README.md +++ b/junit-4/README.md @@ -15,9 +15,8 @@ Master branch contains **Selenium 3** samples, for **Selenium 4 - W3C protocol** * Update *.conf.json files inside the `src/test/resources/conf` directory with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings). ## 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` 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/junit-4/pom.xml b/junit-4/pom.xml index 8b31b9c..3a05ecd 100644 --- a/junit-4/pom.xml +++ b/junit-4/pom.xml @@ -17,8 +17,8 @@ default - 1.6 - 1.6 + 1.8 + 1.8 @@ -47,11 +47,6 @@ json-simple 1.1.1 - - com.browserstack - automate-testassist - 1.0.0 - @@ -60,24 +55,15 @@ org.apache.maven.plugins maven-surefire-plugin - 2.12.4 - - - com.browserstack - automate-maven-plugin - 1.0.0 + 2.19.1 - 1.8 - 1.8 - 1.8 + + com/browserstack/SingleTest.java + + + parallel.conf.json + - - - - test-compile - - - diff --git a/junit-4/src/test/java/com/browserstack/BrowserStackJUnitTest.java b/junit-4/src/test/java/com/browserstack/BrowserStackJUnitTest.java index 3bc83ea..38f668e 100644 --- a/junit-4/src/test/java/com/browserstack/BrowserStackJUnitTest.java +++ b/junit-4/src/test/java/com/browserstack/BrowserStackJUnitTest.java @@ -21,7 +21,6 @@ import java.io.FileReader; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -32,6 +31,8 @@ public class BrowserStackJUnitTest { public static String username, accessKey; private static JSONObject config; public WebDriver driver; + private static Object lock = new Object(); + private static Integer parallels = 0; @Parameter(value = 0) public int taskID; private Local l; @@ -70,7 +71,7 @@ public void setUp() throws Exception { JSONArray envs = (JSONArray) config.get("environments"); DesiredCapabilities capabilities = new DesiredCapabilities(); - + capabilities.setCapability("browserstack.source", "junit-4:sample-selenium-3:v1.1"); Map envCapabilities = (Map) envs.get(taskID); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { @@ -97,11 +98,18 @@ public void setUp() throws Exception { 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); + synchronized (lock) { + parallels++; + if ((l == null || !l.isRunning()) && capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true") { + l = new Local(); + Map options = new HashMap(); + options.put("key", accessKey); + try { + l.start(options); + } catch (Exception e){ + e.printStackTrace(); + } + } } driver = new RemoteWebDriver(new URL("https://" + username + ":" + accessKey + "@" + config.get("server") + "/wd/hub"), capabilities); @@ -109,7 +117,10 @@ public void setUp() throws Exception { @After public void tearDown() throws Exception { - driver.quit(); - if (l != null) l.stop(); + synchronized (lock){ + parallels--; + driver.quit(); + if (l != null && parallels == 0) l.stop(); + } } } diff --git a/junit-4/src/test/java/com/browserstack/SingleTest.java b/junit-4/src/test/java/com/browserstack/SingleTest.java index 77a854f..e1e09a3 100644 --- a/junit-4/src/test/java/com/browserstack/SingleTest.java +++ b/junit-4/src/test/java/com/browserstack/SingleTest.java @@ -25,7 +25,7 @@ public void test() throws URISyntaxException, IOException { String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='1']/p"))).getText(); WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='1']/div[4]"))); cart_btn.click(); - wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content"))); + wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".float\\-cart__content"))); final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText(); assertTrue(product_name.matches(product_in_cart)); mark(sessionId, "passed", "Product has been successfully added to the cart!"); diff --git a/junit-4/src/test/resources/conf/local.conf.json b/junit-4/src/test/resources/conf/local.conf.json index 4815773..860cea1 100644 --- a/junit-4/src/test/resources/conf/local.conf.json +++ b/junit-4/src/test/resources/conf/local.conf.json @@ -3,8 +3,8 @@ "user": "BROWSERSTACK_USERNAME", "key": "BROWSERSTACK_ACCESS_KEY", "capabilities": { - "build": "junit-browserstack", - "name": "local_test", + "build": "browserstack-build-1", + "name": "BStack local junit-4", "browserstack.debug": true, "browserstack.local": true }, diff --git a/junit-4/src/test/resources/conf/parallel.conf.json b/junit-4/src/test/resources/conf/parallel.conf.json index a465e5c..c05c1aa 100644 --- a/junit-4/src/test/resources/conf/parallel.conf.json +++ b/junit-4/src/test/resources/conf/parallel.conf.json @@ -3,27 +3,18 @@ "user": "BROWSERSTACK_USERNAME", "key": "BROWSERSTACK_ACCESS_KEY", "capabilities": { - "build": "junit-browserstack", - "name": "parallel_test", + "build": "browserstack-build-1", + "name": "BStack parallel junit-4", "browserstack.debug": true }, "environments": [ { "browser": "chrome" }, - { - "browser": "firefox" - }, { "browser": "safari", "os": "OS X" }, - { - "browser": "Internet Explorer" - }, - { - "device": "Samsung Galaxy S21" - }, { "device": "iPhone 12" } diff --git a/junit-4/src/test/resources/conf/single.conf.json b/junit-4/src/test/resources/conf/single.conf.json index 1b7e6f2..84c8db9 100644 --- a/junit-4/src/test/resources/conf/single.conf.json +++ b/junit-4/src/test/resources/conf/single.conf.json @@ -3,8 +3,8 @@ "user": "BROWSERSTACK_USERNAME", "key": "BROWSERSTACK_ACCESS_KEY", "capabilities": { - "build": "junit-browserstack", - "name": "single_test", + "build": "browserstack-build-1", + "name": "BStack single junit-4", "browserstack.debug": true }, "environments": [ diff --git a/junit-5/README.md b/junit-5/README.md index 2616273..1f8a5a5 100644 --- a/junit-5/README.md +++ b/junit-5/README.md @@ -17,9 +17,8 @@ Master branch contains **Selenium 3** samples, for **Selenium 4 - W3C protocol** junit.jupiter.execution.parallel.config.fixed.parallelism=${parallel.count} ``` ## 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` 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/junit-5/pom.xml b/junit-5/pom.xml index 5ff7725..d2e2181 100644 --- a/junit-5/pom.xml +++ b/junit-5/pom.xml @@ -47,6 +47,23 @@ maven-surefire-plugin ${maven-surefire-plugin} maven-plugin + + + ${tests.single} + + + parallel.conf.json + + + + junit.jupiter.execution.parallel.enabled = true + junit.jupiter.execution.parallel.mode.default = concurrent + junit.jupiter.execution.parallel.config.strategy=fixed + junit.jupiter.execution.parallel.config.fixed.parallelism=${parallel.count} + + + false + com.browserstack @@ -73,7 +90,7 @@ ${tests.single} - single + single.conf.json false @@ -94,7 +111,7 @@ ${tests.local} - local + local.conf.json false @@ -115,7 +132,7 @@ ${tests.single} - parallel + parallel.conf.json diff --git a/junit-5/src/test/java/runners/BstackRunner.java b/junit-5/src/test/java/runners/BstackRunner.java index afbd65d..7c62947 100644 --- a/junit-5/src/test/java/runners/BstackRunner.java +++ b/junit-5/src/test/java/runners/BstackRunner.java @@ -1,5 +1,6 @@ package runners; +import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.junit.jupiter.api.extension.*; @@ -16,16 +17,10 @@ public class BstackRunner implements TestTemplateInvocationContextProvider { public WebDriver driver; - public DesiredCapabilities capabilities; public String username, accesskey, server; private JSONObject mainConfig; - private JSONObject browserConfig; - private JSONObject profileConfig; - private JSONObject testConfig; - private JSONObject platformConfig; - private JSONObject commonCapsConfig; - private HashMap allCapsMap; - private HashMap commonCapsMap; + private JSONArray platformConfig; + private HashMap commonCapsConfig; public BstackRunner() { this.username = setupCredsAndServer().get("username"); @@ -35,8 +30,10 @@ public BstackRunner() { public HashMap setupCredsAndServer() { try { - JSONParser parse = new JSONParser(); - mainConfig = (JSONObject) parse.parse(new FileReader("src/test/resources/caps.json")); + if (System.getProperty("config") != null) { + JSONParser parser = new JSONParser(); + mainConfig = (JSONObject) parser.parse(new FileReader("src/test/resources/conf/" + System.getProperty("config"))); + } server = (String) mainConfig.get("server"); username = System.getenv("BROWSERSTACK_USERNAME"); if (username == null) { @@ -47,9 +44,9 @@ public HashMap setupCredsAndServer() { accesskey = (String) mainConfig.get("key"); } } catch (Exception e) { - System.out.println(e.getMessage()); + e.printStackTrace(); } - HashMap creds = new HashMap(); + HashMap creds = new HashMap<>(); creds.put("username", username); creds.put("accesskey", accesskey); creds.put("server", server); @@ -64,30 +61,23 @@ public boolean supportsTestTemplate(ExtensionContext extensionContext) { @Override public Stream provideTestTemplateInvocationContexts(ExtensionContext extensionContext) { List desiredCapsInvocationContexts = new ArrayList<>(); - //picks the test profile based on the maven command executed - single, local, parallel - String profile = System.getProperty("config"); try { - testConfig = (JSONObject) mainConfig.get("tests"); - profileConfig = (JSONObject) testConfig.get(profile); - platformConfig = (JSONObject) profileConfig.get("platform"); - commonCapsConfig = (JSONObject) profileConfig.get("common_caps"); - commonCapsMap = (HashMap) commonCapsConfig; - Iterator platformIterator = platformConfig.keySet().iterator(); + platformConfig = (JSONArray) mainConfig.get("environments"); + commonCapsConfig = (HashMap) mainConfig.get("capabilities"); - while (platformIterator.hasNext()) { - capabilities = new DesiredCapabilities(); - Iterator commonCapsIterator = commonCapsMap.entrySet().iterator(); + for (Object platform : platformConfig) { + DesiredCapabilities capabilities = new DesiredCapabilities(); + Map envCapabilities = (Map) platform; + capabilities.setCapability("browserstack.source", "junit-5:sample-selenium-3:v1.1"); + Iterator commonCapsIterator = commonCapsConfig.entrySet().iterator(); while (commonCapsIterator.hasNext()) { Map.Entry capsName = (Map.Entry) commonCapsIterator.next(); capabilities.setCapability((String) capsName.getKey(), capsName.getValue()); } - final String platformName = (String) platformIterator.next(); - browserConfig = (JSONObject) platformConfig.get(platformName); - allCapsMap = (HashMap) browserConfig; - Iterator finalCapsIterator = allCapsMap.entrySet().iterator(); - while (finalCapsIterator.hasNext()) { - Map.Entry pair = (Map.Entry) finalCapsIterator.next(); + Iterator envCapsIterator = envCapabilities.entrySet().iterator(); + while (envCapsIterator.hasNext()) { + Map.Entry pair = (Map.Entry) envCapsIterator.next(); capabilities.setCapability((String) pair.getKey(), pair.getValue()); } //Initializing local testing connection @@ -101,7 +91,7 @@ public Stream provideTestTemplateInvocationContex } } catch (Exception e) { - System.out.println(e); + e.printStackTrace(); } return desiredCapsInvocationContexts.stream(); } diff --git a/junit-5/src/test/java/tests/SingleTest.java b/junit-5/src/test/java/tests/SingleTest.java index 14de8bf..9b30f74 100644 --- a/junit-5/src/test/java/tests/SingleTest.java +++ b/junit-5/src/test/java/tests/SingleTest.java @@ -24,7 +24,7 @@ void singleTest(WebDriver driver) { String product_name = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='1']/p"))).getText(); WebElement cart_btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='1']/div[4]"))); cart_btn.click(); - wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("float-cart__content"))); + wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".float\\-cart__content"))); final String product_in_cart = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='__next']/div/div/div[2]/div[2]/div[2]/div/div[3]/p[1]"))).getText(); if (product_name.equals(product_in_cart)) { sessionStatus.markTestStatus("passed", "Product has been successfully added to the cart!"); diff --git a/junit-5/src/test/resources/caps.json b/junit-5/src/test/resources/caps.json deleted file mode 100644 index a2a8e79..0000000 --- a/junit-5/src/test/resources/caps.json +++ /dev/null @@ -1,95 +0,0 @@ -{ - "server": "hub.browserstack.com", - "user": "BROWSERSTACK_USERNAME", - "key": "BROWSERSTACK_ACCESS_KEY", - "tests": { - "single": { - "common_caps": { - "name": "single-test", - "build": "junit5-browserstack", - "browserstack.console": "verbose", - "browserstack.networkLogs": "true", - "browserstack.debug": "true" - }, - "platform": { - "chrome": { - "browser": "chrome", - "browser_version": "latest", - "os": "Windows", - "os_version": "10" - } - } - }, - "local": { - "common_caps": { - "name": "local-test", - "build": "junit5-browserstack", - "browserstack.console": "verbose", - "browserstack.local": "true", - "browserstack.debug": "true" - }, - "platform": { - "chrome": { - "browser": "chrome", - "browser_version": "latest", - "os": "Windows", - "os_version": "10" - } - } - }, - "parallel": { - "common_caps": { - "name": "parallel-test", - "build": "junit5-browserstack", - "browserstack.console": "verbose", - "browserstack.debug": "true" - }, - "platform": { - "chrome": { - "browser": "chrome", - "browser_version": "latest-1", - "os": "Windows", - "os_version": "10" - }, - "safari": { - "browser": "safari", - "browser_version": "latest", - "os": "OS X", - "os_version": "Big Sur" - }, - "firefox": { - "browser": "firefox", - "browser_version": "latest-2", - "os": "OS X", - "os_version": "Monterey" - }, - "edge": { - "browser": "edge", - "browser_version": "latest", - "os": "Windows", - "os_version": "11" - }, - "android": { - "device": "Samsung Galaxy S21", - "os_version": "11.0" - }, - "ios": { - "device": "iPhone 12 Mini", - "os_version": "14" - }, - "tab": { - "device": "Samsung Galaxy Tab S7" - }, - "ipad": { - "device": "iPad Mini 2019" - }, - "xiaomi": { - "device": "Xiaomi Redmi Note 9" - }, - "oneplus": { - "device": "OnePlus 9" - } - } - } - } -} diff --git a/junit-5/src/test/resources/conf/local.conf.json b/junit-5/src/test/resources/conf/local.conf.json new file mode 100644 index 0000000..edafdb0 --- /dev/null +++ b/junit-5/src/test/resources/conf/local.conf.json @@ -0,0 +1,16 @@ +{ + "server": "hub.browserstack.com", + "user": "BROWSERSTACK_USERNAME", + "key": "BROWSERSTACK_ACCESS_KEY", + "capabilities": { + "build": "browserstack-build-1", + "name": "BStack local junit-5", + "browserstack.debug": true, + "browserstack.local": true + }, + "environments": [ + { + "browser": "chrome" + } + ] +} diff --git a/junit-5/src/test/resources/conf/parallel.conf.json b/junit-5/src/test/resources/conf/parallel.conf.json new file mode 100644 index 0000000..36ce3ff --- /dev/null +++ b/junit-5/src/test/resources/conf/parallel.conf.json @@ -0,0 +1,22 @@ +{ + "server": "hub.browserstack.com", + "user": "BROWSERSTACK_USERNAME", + "key": "BROWSERSTACK_ACCESS_KEY", + "capabilities": { + "build": "browserstack-build-1", + "name": "BStack parallel junit-5", + "browserstack.debug": true + }, + "environments": [ + { + "browser": "chrome" + }, + { + "browser": "safari", + "os": "OS X" + }, + { + "device": "iPhone 12" + } + ] +} diff --git a/junit-5/src/test/resources/conf/single.conf.json b/junit-5/src/test/resources/conf/single.conf.json new file mode 100644 index 0000000..9b5a3fd --- /dev/null +++ b/junit-5/src/test/resources/conf/single.conf.json @@ -0,0 +1,15 @@ +{ + "server": "hub.browserstack.com", + "user": "BROWSERSTACK_USERNAME", + "key": "BROWSERSTACK_ACCESS_KEY", + "capabilities": { + "build": "browserstack-build-1", + "name": "BStack single junit-5", + "browserstack.debug": true + }, + "environments": [ + { + "browser": "chrome" + } + ] +}