From e728bcbec14e96891b6ae68deffd392a5ab81d6f Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Thu, 25 Aug 2022 21:34:18 +0530 Subject: [PATCH 01/10] [android]Use w3c with latest appium client version | replace deprecated methods with new ones --- android/testng-examples/pom.xml | 8 +-- .../BrowserStackTestNGTest.java | 24 ++++--- .../run_first_test/FirstTest.java | 16 ++--- .../BrowserStackTestNGTest.java | 62 ++++++++++--------- .../run_local_test/LocalTest.java | 19 +++--- .../BrowserStackTestNGTest.java | 23 ++++--- .../run_parallel_test/ParallelTest.java | 15 ++--- .../run_first_test/first.conf.json | 24 ++++--- .../run_local_test/local.conf.json | 26 ++++---- .../run_parallel_test/parallel.conf.json | 28 +++++---- 10 files changed, 139 insertions(+), 106 deletions(-) diff --git a/android/testng-examples/pom.xml b/android/testng-examples/pom.xml index 0d96421..c5a23e0 100644 --- a/android/testng-examples/pom.xml +++ b/android/testng-examples/pom.xml @@ -22,22 +22,22 @@ org.testng testng - 6.9.10 + 7.6.1 io.appium java-client - 7.0.0 + 8.1.1 commons-io commons-io - 1.3.2 + 2.11.0 org.seleniumhq.selenium selenium-java - 3.141.59 + 4.4.0 com.browserstack diff --git a/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java index d763a6d..af3bc19 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java @@ -2,6 +2,8 @@ import java.net.URL; import java.util.Map; +import java.util.HashMap; + import java.util.Iterator; import java.io.FileReader; import org.json.simple.JSONObject; @@ -9,41 +11,43 @@ import org.json.simple.JSONArray; import io.appium.java_client.android.AndroidDriver; -import io.appium.java_client.android.AndroidElement; - -import org.openqa.selenium.remote.DesiredCapabilities; +import io.appium.java_client.android.options.UiAutomator2Options; +import org.openqa.selenium.WebElement; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; public class BrowserStackTestNGTest { - public AndroidDriver driver; + public AndroidDriver driver; @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { JSONParser parser = new JSONParser(); JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_first_test/first.conf.json")); - DesiredCapabilities capabilities = new DesiredCapabilities(); + UiAutomator2Options options = new UiAutomator2Options(); JSONArray envs = (JSONArray) config.get("environments"); Map envCapabilities = (Map) envs.get(0); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString()); + options.setCapability(pair.getKey().toString(), pair.getValue().toString()); } Map commonCapabilities = (Map) config.get("capabilities"); it = commonCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - if(capabilities.getCapability(pair.getKey().toString()) == null){ - capabilities.setCapability(pair.getKey().toString(), pair.getValue()); + if(options.getCapability(pair.getKey().toString()) == null){ + options.setCapability(pair.getKey().toString(), pair.getValue()); } } + HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); + options.setCapability("bstack:options", browserstackOptions); + String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { username = (String) config.get("username"); @@ -56,10 +60,10 @@ public void setUp() throws Exception { String app = System.getenv("BROWSERSTACK_APP_ID"); if(app != null && !app.isEmpty()) { - capabilities.setCapability("app", app); + options.setCapability("app", app); } - driver = new AndroidDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities); + driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); } @AfterMethod(alwaysRun=true) diff --git a/android/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java b/android/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java index b77b748..ce8bd07 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java @@ -1,28 +1,30 @@ package com.browserstack.run_first_test; -import io.appium.java_client.MobileBy; -import io.appium.java_client.android.AndroidElement; +import io.appium.java_client.AppiumBy; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.WebElement; import org.testng.Assert; import org.testng.annotations.Test; import java.util.List; +import java.time.Duration; public class FirstTest extends BrowserStackTestNGTest { @Test public void test() throws Exception { - AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia"))); + WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Search Wikipedia"))); + searchElement.click(); - AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.id("org.wikipedia.alpha:id/search_src_text"))); + WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.id("org.wikipedia.alpha:id/search_src_text"))); insertTextElement.sendKeys("BrowserStack"); Thread.sleep(5000); - List allProductsName = driver.findElementsByClassName("android.widget.TextView"); + List allProductsName = driver.findElements(AppiumBy.className("android.widget.TextView")); Assert.assertTrue(allProductsName.size() > 0); } } diff --git a/android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java index 1bbfe29..e983442 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java @@ -1,4 +1,5 @@ package com.browserstack.run_local_test; + import com.browserstack.local.Local; import java.net.URL; @@ -11,72 +12,77 @@ import org.json.simple.JSONArray; import io.appium.java_client.android.AndroidDriver; -import io.appium.java_client.android.AndroidElement; +import io.appium.java_client.android.options.UiAutomator2Options; -import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.WebElement; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; - public class BrowserStackTestNGTest { - public AndroidDriver driver; + public AndroidDriver driver; private Local local; - @BeforeMethod(alwaysRun=true) + @BeforeMethod(alwaysRun = true) public void setUp() throws Exception { JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_local_test/local.conf.json")); + JSONObject config = (JSONObject) parser + .parse(new FileReader("src/test/resources/com/browserstack/run_local_test/local.conf.json")); JSONArray envs = (JSONArray) config.get("environments"); - DesiredCapabilities capabilities = new DesiredCapabilities(); + UiAutomator2Options options = new UiAutomator2Options(); Map envCapabilities = (Map) envs.get(0); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString()); + Map.Entry pair = (Map.Entry) it.next(); + options.setCapability(pair.getKey().toString(), pair.getValue().toString()); } - + Map commonCapabilities = (Map) config.get("capabilities"); it = commonCapabilities.entrySet().iterator(); while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - if(capabilities.getCapability(pair.getKey().toString()) == null){ - capabilities.setCapability(pair.getKey().toString(), pair.getValue()); + Map.Entry pair = (Map.Entry) it.next(); + if (options.getCapability(pair.getKey().toString()) == null) { + options.setCapability(pair.getKey().toString(), pair.getValue()); } } + HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); + options.setCapability("bstack:options", browserstackOptions); + String username = System.getenv("BROWSERSTACK_USERNAME"); - if(username == null) { - username = (String) config.get("username"); + if (username == null) { + username = (String) browserstackOptions.get("userName"); } String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); - if(accessKey == null) { - accessKey = (String) config.get("access_key"); + if (accessKey == null) { + accessKey = (String) browserstackOptions.get("accessKey"); } - + String app = System.getenv("BROWSERSTACK_APP_ID"); - if(app != null && !app.isEmpty()) { - capabilities.setCapability("app", app); + if (app != null && !app.isEmpty()) { + options.setCapability("app", app); } - if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){ + if (browserstackOptions.get("local") != null + && browserstackOptions.get("local").toString() == "true") { local = new Local(); - Map options = new HashMap(); - options.put("key", accessKey); - local.start(options); + Map LocalOptions = new HashMap(); + LocalOptions.put("key", accessKey); + local.start(LocalOptions); } - driver = new AndroidDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities); + driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); } - @AfterMethod(alwaysRun=true) + @AfterMethod(alwaysRun = true) public void tearDown() throws Exception { - // Invoke driver.quit() to indicate that the test is completed. + // Invoke driver.quit() to indicate that the test is completed. // Otherwise, it will appear as timed out on BrowserStack. driver.quit(); - if(local != null) local.stop(); + if (local != null) + local.stop(); } } diff --git a/android/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java b/android/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java index c682666..20709c7 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java @@ -5,11 +5,12 @@ import java.io.File; import java.util.List; +import java.time.Duration; import org.apache.commons.io.FileUtils; -import io.appium.java_client.MobileBy; -import io.appium.java_client.android.AndroidElement; +import io.appium.java_client.AppiumBy; +import org.openqa.selenium.WebElement; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.support.ui.WebDriverWait; @@ -19,16 +20,16 @@ public class LocalTest extends BrowserStackTestNGTest { @Test public void test() throws Exception { - AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.id("com.example.android.basicnetworking:id/test_action"))); + WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.id("com.example.android.basicnetworking:id/test_action"))); searchElement.click(); - AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.className("android.widget.TextView"))); + WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.className("android.widget.TextView"))); - AndroidElement testElement = null; - List allTextViewElements = driver.findElementsByClassName("android.widget.TextView"); + WebElement testElement = null; + List allTextViewElements = driver.findElements(AppiumBy.className("android.widget.TextView")); Thread.sleep(10); - for(AndroidElement textElement : allTextViewElements) { + for(WebElement textElement : allTextViewElements) { if(textElement.getText().contains("The active connection is")) { testElement = textElement; } diff --git a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java index 4172790..4afa7ad 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java @@ -2,16 +2,18 @@ import java.net.URL; import java.util.Map; +import java.util.HashMap; import java.util.Iterator; import java.io.FileReader; +import java.time.Duration; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.JSONArray; import io.appium.java_client.android.AndroidDriver; -import io.appium.java_client.android.AndroidElement; -import org.openqa.selenium.remote.DesiredCapabilities; +import org.openqa.selenium.WebElement; +import io.appium.java_client.android.options.UiAutomator2Options; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; @@ -19,7 +21,7 @@ public class BrowserStackTestNGTest { - public AndroidDriver driver; + public AndroidDriver driver; @BeforeMethod(alwaysRun=true) @org.testng.annotations.Parameters(value={"deviceIndex"}) @@ -28,24 +30,27 @@ public void setUp(String deviceIndex) throws Exception { JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json")); JSONArray envs = (JSONArray) config.get("environments"); - DesiredCapabilities capabilities = new DesiredCapabilities(); + UiAutomator2Options options = new UiAutomator2Options(); Map envCapabilities = (Map) envs.get(Integer.parseInt(deviceIndex)); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString()); + options.setCapability(pair.getKey().toString(), pair.getValue().toString()); } Map commonCapabilities = (Map) config.get("capabilities"); it = commonCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - if(capabilities.getCapability(pair.getKey().toString()) == null){ - capabilities.setCapability(pair.getKey().toString(), pair.getValue()); + if(options.getCapability(pair.getKey().toString()) == null){ + options.setCapability(pair.getKey().toString(), pair.getValue()); } } + HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); + options.setCapability("bstack:options", browserstackOptions); + String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { username = (String) config.get("username"); @@ -58,10 +63,10 @@ public void setUp(String deviceIndex) throws Exception { String app = System.getenv("BROWSERSTACK_APP_ID"); if(app != null && !app.isEmpty()) { - capabilities.setCapability("app", app); + options.setCapability("app", app); } - driver = new AndroidDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities); + driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); } @AfterMethod(alwaysRun=true) diff --git a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java b/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java index a6d5e13..24494b0 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java @@ -1,28 +1,29 @@ package com.browserstack.run_parallel_test; -import io.appium.java_client.MobileBy; -import io.appium.java_client.android.AndroidElement; +import io.appium.java_client.AppiumBy; import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.WebElement; import org.testng.Assert; import org.testng.annotations.Test; import java.util.List; +import java.time.Duration; public class ParallelTest extends BrowserStackTestNGTest { @Test public void test() throws Exception { - AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Search Wikipedia"))); + WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Search Wikipedia"))); searchElement.click(); - AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.id("org.wikipedia.alpha:id/search_src_text"))); + WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.id("org.wikipedia.alpha:id/search_src_text"))); insertTextElement.sendKeys("BrowserStack"); Thread.sleep(5000); - List allProductsName = driver.findElementsByClassName("android.widget.TextView"); + List allProductsName = driver.findElements(AppiumBy.className("android.widget.TextView")); Assert.assertTrue(allProductsName.size() > 0); } } diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json b/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json index 48f3e7b..7b6553a 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json +++ b/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json @@ -1,20 +1,24 @@ { - "server": "hub-cloud.browserstack.com", - "username": "BROWSERSTACK_USERNAME", - "access_key": "BROWSERSTACK_ACCESS_KEY", + "server": "hub.browserstack.com", "capabilities": { - "project": "First TestNg Android Project", - "build": "browserstack-build-1", - "name": "first_test", - "browserstack.debug": true, "app": "bs://" }, + "browserstackOptions": { + "projectName": "First TestNg Android Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack first_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + }, + "environments": [ - { - "device": "Google Pixel 3", - "os_version": "9.0" + { + "deviceName": "Google Pixel 3", + "platformVersion": "9.0", + "platformName": "android" } ] } diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json b/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json index af26c45..03ff594 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json +++ b/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json @@ -1,21 +1,25 @@ { - "server": "hub-cloud.browserstack.com", - "username": "BROWSERSTACK_USERNAME", - "access_key": "BROWSERSTACK_ACCESS_KEY", + "server": "hub.browserstack.com", "capabilities": { - "project": "First TestNg Android Project", - "build": "browserstack-build-1", - "name": "local_test", - "browserstack.debug": true, - "browserstack.local": true, "app": "bs://" }, + "browserstackOptions": { + "projectName": "First TestNg Android Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack local_test", + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY", + "debug": true, + "local": true + }, + "environments": [ - { - "device": "Google Pixel 3", - "os_version": "9.0" + { + "deviceName": "Google Pixel 3", + "platformVersion": "9.0", + "platformName": "android" } ] } diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json b/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json index 5571d9a..fe2b014 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json +++ b/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json @@ -1,24 +1,30 @@ { - "server": "hub-cloud.browserstack.com", - "username": "BROWSERSTACK_USERNAME", - "access_key": "BROWSERSTACK_ACCESS_KEY", + "server": "hub.browserstack.com", "capabilities": { - "project": "First TestNg Android Project", - "build": "browserstack-build-1", - "name": "parallel_test", - "browserstack.debug": true, "app": "bs://" }, + "browserstackOptions": { + "projectName": "First TestNg Android Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack parallel_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + }, + "environments": [ { - "device": "Google Pixel 3", - "os_version": "9.0" + "deviceName": "Google Pixel 3", + "platformVersion": "9.0", + "platformName": "android" }, + { - "device": "Samsung Galaxy S10e", - "os_version": "9.0" + "deviceName": "Samsung Galaxy S22", + "platformVersion": "12.0", + "platformName": "android" } ] } From 2cd5c283157d2352404dcee28f837d05e3bdfeaa Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Fri, 26 Aug 2022 02:06:11 +0530 Subject: [PATCH 02/10] read creds properly --- .../browserstack/run_first_test/BrowserStackTestNGTest.java | 4 ++-- .../run_parallel_test/BrowserStackTestNGTest.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java index af3bc19..151be43 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java @@ -50,12 +50,12 @@ public void setUp() throws Exception { String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { - username = (String) config.get("username"); + username = (String) browserstackOptions.get("userName"); } String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); if(accessKey == null) { - accessKey = (String) config.get("access_key"); + accessKey = (String) browserstackOptions.get("accessKey"); } String app = System.getenv("BROWSERSTACK_APP_ID"); diff --git a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java index 4afa7ad..5af7d8b 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java @@ -53,12 +53,12 @@ public void setUp(String deviceIndex) throws Exception { String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { - username = (String) config.get("username"); + username = (String) browserstackOptions.get("userName"); } String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); if(accessKey == null) { - accessKey = (String) config.get("access_key"); + accessKey = (String) browserstackOptions.get("accessKey"); } String app = System.getenv("BROWSERSTACK_APP_ID"); From c323059a71ff7ec4ec9183cab793cfbf465337bb Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Fri, 26 Aug 2022 02:07:21 +0530 Subject: [PATCH 03/10] Add w3c session support | bump to latest appium client version | use options class | replace deprecated methods --- ios/testng-examples/pom.xml | 8 ++--- .../BrowserStackTestNGTest.java | 27 ++++++++------- .../run_first_test/FirstTest.java | 18 +++++----- .../BrowserStackTestNGTest.java | 33 ++++++++++--------- .../run_local_test/LocalTest.java | 16 +++++---- .../BrowserStackTestNGTest.java | 27 ++++++++------- .../run_parallel_test/ParallelTest.java | 18 +++++----- .../run_first_test/first.conf.json | 22 ++++++++----- .../run_local_test/local.conf.json | 24 ++++++++------ .../run_parallel_test/parallel.conf.json | 31 +++++++++-------- 10 files changed, 127 insertions(+), 97 deletions(-) diff --git a/ios/testng-examples/pom.xml b/ios/testng-examples/pom.xml index 0d96421..c5a23e0 100644 --- a/ios/testng-examples/pom.xml +++ b/ios/testng-examples/pom.xml @@ -22,22 +22,22 @@ org.testng testng - 6.9.10 + 7.6.1 io.appium java-client - 7.0.0 + 8.1.1 commons-io commons-io - 1.3.2 + 2.11.0 org.seleniumhq.selenium selenium-java - 3.141.59 + 4.4.0 com.browserstack diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java index a615c89..e01b61f 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java @@ -3,23 +3,25 @@ import java.net.URL; import java.util.Map; +import java.util.HashMap; import java.util.Iterator; import java.io.FileReader; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.JSONArray; -import org.openqa.selenium.remote.DesiredCapabilities; +import io.appium.java_client.ios.options.XCUITestOptions; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import io.appium.java_client.ios.IOSDriver; -import io.appium.java_client.ios.IOSElement; + +import org.openqa.selenium.WebElement; public class BrowserStackTestNGTest { - public IOSDriver driver; + public IOSDriver driver; @BeforeMethod(alwaysRun=true) public void setUp() throws Exception { @@ -27,40 +29,43 @@ public void setUp() throws Exception { JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_first_test/first.conf.json")); JSONArray envs = (JSONArray) config.get("environments"); - DesiredCapabilities capabilities = new DesiredCapabilities(); + XCUITestOptions options = new XCUITestOptions(); Map envCapabilities = (Map) envs.get(0); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString()); + options.setCapability(pair.getKey().toString(), pair.getValue().toString()); } Map commonCapabilities = (Map) config.get("capabilities"); it = commonCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - if(capabilities.getCapability(pair.getKey().toString()) == null){ - capabilities.setCapability(pair.getKey().toString(), pair.getValue()); + if(options.getCapability(pair.getKey().toString()) == null){ + options.setCapability(pair.getKey().toString(), pair.getValue()); } } + HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); + options.setCapability("bstack:options", browserstackOptions); + String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { - username = (String) config.get("username"); + username = (String) browserstackOptions.get("userName"); } String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); if(accessKey == null) { - accessKey = (String) config.get("access_key"); + accessKey = (String) browserstackOptions.get("accessKey"); } String app = System.getenv("BROWSERSTACK_APP_ID"); if(app != null && !app.isEmpty()) { - capabilities.setCapability("app", app); + options.setCapability("app", app); } - driver = new IOSDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities); + driver = new IOSDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); } @AfterMethod(alwaysRun=true) diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java index 738163e..cc198ae 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java @@ -6,25 +6,27 @@ import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; -import io.appium.java_client.MobileBy; -import io.appium.java_client.ios.IOSElement; +import io.appium.java_client.AppiumBy; +import org.openqa.selenium.WebElement; + +import java.time.Duration; public class FirstTest extends BrowserStackTestNGTest { @Test public void test() throws Exception { - IOSElement textButton = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Button"))); + WebElement textButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Button"))); textButton.click(); - IOSElement textInput = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Input"))); + WebElement textInput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input"))); textInput.sendKeys("hello@browserstack.com"+"\n"); Thread.sleep(5000); - IOSElement textOutput = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Output"))); + WebElement textOutput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Output"))); Assert.assertEquals(textOutput.getText(),"hello@browserstack.com"); } diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java index 47764ab..81bca2d 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java @@ -10,17 +10,17 @@ import org.json.simple.parser.JSONParser; import org.json.simple.JSONArray; -import org.openqa.selenium.remote.DesiredCapabilities; +import io.appium.java_client.ios.options.XCUITestOptions; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; import io.appium.java_client.ios.IOSDriver; -import io.appium.java_client.ios.IOSElement; +import org.openqa.selenium.WebElement; public class BrowserStackTestNGTest { - public IOSDriver driver; + public IOSDriver driver; private Local local; @BeforeMethod(alwaysRun=true) @@ -29,47 +29,50 @@ public void setUp() throws Exception { JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_local_test/local.conf.json")); JSONArray envs = (JSONArray) config.get("environments"); - DesiredCapabilities capabilities = new DesiredCapabilities(); + XCUITestOptions options = new XCUITestOptions(); Map envCapabilities = (Map) envs.get(0); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString()); + options.setCapability(pair.getKey().toString(), pair.getValue().toString()); } Map commonCapabilities = (Map) config.get("capabilities"); it = commonCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - if(capabilities.getCapability(pair.getKey().toString()) == null){ - capabilities.setCapability(pair.getKey().toString(), pair.getValue()); + if(options.getCapability(pair.getKey().toString()) == null){ + options.setCapability(pair.getKey().toString(), pair.getValue()); } } + HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); + options.setCapability("bstack:options", browserstackOptions); + String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { - username = (String) config.get("username"); + username = (String) browserstackOptions.get("userName"); } String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); if(accessKey == null) { - accessKey = (String) config.get("access_key"); + accessKey = (String) browserstackOptions.get("accessKey"); } String app = System.getenv("BROWSERSTACK_APP_ID"); if(app != null && !app.isEmpty()) { - capabilities.setCapability("app", app); + options.setCapability("app", app); } - if(capabilities.getCapability("browserstack.local") != null && capabilities.getCapability("browserstack.local") == "true"){ + if(browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true"){ local = new Local(); - Map options = new HashMap(); - options.put("key", accessKey); - local.start(options); + Map LocalOptions = new HashMap(); + LocalOptions.put("key", accessKey); + local.start(LocalOptions); } - driver = new IOSDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities); + driver = new IOSDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); } @AfterMethod(alwaysRun=true) diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java index 0bf3c26..858dea9 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java @@ -13,27 +13,29 @@ import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.ExpectedConditions; -import io.appium.java_client.MobileBy; -import io.appium.java_client.ios.IOSElement; +import io.appium.java_client.AppiumBy; +import org.openqa.selenium.WebElement; + +import java.time.Duration; public class LocalTest extends BrowserStackTestNGTest { @Test public void test() throws Exception { - IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal"))); + WebElement testButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("TestBrowserStackLocal"))); testButton.click(); - WebDriverWait wait = new WebDriverWait(driver, 30); + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); wait.until(new ExpectedCondition() { @Override public Boolean apply(WebDriver d) { - String result = d.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")).getAttribute("value"); + String result = d.findElement(AppiumBy.accessibilityId("ResultBrowserStackLocal")).getAttribute("value"); return result != null && result.length() > 0; } }); - IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")); + WebElement resultElement = (WebElement) driver.findElement(AppiumBy.accessibilityId("ResultBrowserStackLocal")); String resultString = resultElement.getText().toLowerCase(); System.out.println(resultString); diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java index e6e08bc..25c6540 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java @@ -2,24 +2,24 @@ import java.net.URL; import java.util.Map; +import java.util.HashMap; import java.util.Iterator; import java.io.FileReader; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.JSONArray; -import org.openqa.selenium.remote.DesiredCapabilities; +import io.appium.java_client.ios.options.XCUITestOptions; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; -import org.testng.annotations.Parameters; import io.appium.java_client.ios.IOSDriver; -import io.appium.java_client.ios.IOSElement; +import org.openqa.selenium.WebElement; public class BrowserStackTestNGTest { - public IOSDriver driver; + public IOSDriver driver; @BeforeMethod(alwaysRun=true) @org.testng.annotations.Parameters(value={"deviceIndex"}) @@ -28,40 +28,43 @@ public void setUp(String deviceIndex) throws Exception { JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json")); JSONArray envs = (JSONArray) config.get("environments"); - DesiredCapabilities capabilities = new DesiredCapabilities(); + XCUITestOptions options = new XCUITestOptions(); Map envCapabilities = (Map) envs.get(Integer.parseInt(deviceIndex)); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - capabilities.setCapability(pair.getKey().toString(), pair.getValue().toString()); + options.setCapability(pair.getKey().toString(), pair.getValue().toString()); } Map commonCapabilities = (Map) config.get("capabilities"); it = commonCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); - if(capabilities.getCapability(pair.getKey().toString()) == null){ - capabilities.setCapability(pair.getKey().toString(), pair.getValue()); + if(options.getCapability(pair.getKey().toString()) == null){ + options.setCapability(pair.getKey().toString(), pair.getValue()); } } + HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); + options.setCapability("bstack:options", browserstackOptions); + String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { - username = (String) config.get("username"); + username = (String) browserstackOptions.get("userName"); } String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); if(accessKey == null) { - accessKey = (String) config.get("access_key"); + accessKey = (String) browserstackOptions.get("accessKey"); } String app = System.getenv("BROWSERSTACK_APP_ID"); if(app != null && !app.isEmpty()) { - capabilities.setCapability("app", app); + options.setCapability("app", app); } - driver = new IOSDriver(new URL("http://"+username+":"+accessKey+"@"+config.get("server")+"/wd/hub"), capabilities); + driver = new IOSDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); } @AfterMethod(alwaysRun=true) diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java index bbd3b80..6509021 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java @@ -6,25 +6,27 @@ import org.openqa.selenium.support.ui.WebDriverWait; import org.openqa.selenium.support.ui.ExpectedConditions; -import io.appium.java_client.MobileBy; -import io.appium.java_client.ios.IOSElement; +import io.appium.java_client.AppiumBy; +import org.openqa.selenium.WebElement; + +import java.time.Duration; public class ParallelTest extends BrowserStackTestNGTest { @Test public void test() throws Exception { - IOSElement textButton = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Button"))); + WebElement textButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Button"))); textButton.click(); - IOSElement textInput = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Input"))); + WebElement textInput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input"))); textInput.sendKeys("hello@browserstack.com"+"\n"); Thread.sleep(5000); - IOSElement textOutput = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Output"))); + WebElement textOutput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Output"))); Assert.assertEquals(textOutput.getText(),"hello@browserstack.com"); } diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json b/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json index 9cba216..8a106e7 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json @@ -1,20 +1,24 @@ { - "server": "hub-cloud.browserstack.com", - "username": "BROWSERSTACK_USERNAME", - "access_key": "BROWSERSTACK_ACCESS_KEY", + "server": "hub.browserstack.com", "capabilities": { - "project": "First TestNg iOS Project", - "build": "browserstack-build-1", - "name": "first_test", - "browserstack.debug": true, "app": "bs://" }, + "browserstackOptions": { + "projectName": "First TestNg iOS Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack first_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + }, + "environments": [ { - "device": "iPhone 11 Pro", - "os_version": "13" + "deviceName": "iPhone 11 Pro Max", + "platformVersion": "13", + "platformName": "ios" } ] } diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json b/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json index 9acbe44..366cee5 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json @@ -1,21 +1,25 @@ { - "server": "hub-cloud.browserstack.com", - "username": "BROWSERSTACK_USERNAME", - "access_key": "BROWSERSTACK_ACCESS_KEY", + "server": "hub.browserstack.com", "capabilities": { - "project": "First TestNg iOS Project", - "build": "browserstack-build-1", - "name": "local_test", - "browserstack.debug": true, - "browserstack.local": true, "app": "bs://" }, + "browserstackOptions": { + "projectName": "First TestNg iOS Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack local_test", + "debug": true, + "local": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + }, + "environments": [ { - "device": "iPhone 11 Pro", - "os_version": "13" + "deviceName": "iPhone XS", + "platformVersion": "12", + "platformName": "ios" } ] } diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json b/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json index 6b7afe9..c27bae4 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json @@ -1,24 +1,29 @@ { - "server": "hub-cloud.browserstack.com", - "username": "BROWSERSTACK_USERNAME", - "access_key": "BROWSERSTACK_ACCESS_KEY", + "server": "hub.browserstack.com", "capabilities": { - "project": "First TestNg iOS Project", - "build": "browserstack-build-1", - "name": "parallel_test", - "browserstack.debug": true, "app": "bs://" }, + "browserstackOptions": { + "projectName": "First TestNg iOS Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack parallel_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + }, + "environments": [ - { - "device": "iPhone 11 Pro", - "os_version": "13" + { + "deviceName": "iPhone 11 Pro Max", + "platformVersion": "13", + "platformName": "ios" }, - { - "device": "iPhone 11 Pro Max", - "os_version": "13" + { + "deviceName": "iPhone 11 Pro Max", + "platformVersion": "13", + "platformName": "ios" } ] } From 432d53bc12e3072d7508b645f197720e28ae29c3 Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Fri, 26 Aug 2022 02:13:05 +0530 Subject: [PATCH 04/10] Add w3c note --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 3b16806..67c7a5f 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ This repository demonstrates how to run Appium tests in [TestNG](http://testng.o ![BrowserStack Logo](https://d98b8t1nnulk5.cloudfront.net/production/images/layout/logo-header.png?1469004780) +## Based on + +These code samples are currently based on: + +- **Appium-Java-Client:** `8.1.1` +- **Protocol:** `W3C` ## Setup ### Requirements From 2e724414a303c9ff6606d2ceec5ce880dde003d5 Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Sat, 27 Aug 2022 22:09:36 +0530 Subject: [PATCH 05/10] Use parametrized config for common base class --- .../BrowserStackTestNGTest.java | 25 ++++-- .../{run_first_test => }/FirstTest.java | 2 +- .../{run_local_test => }/LocalTest.java | 2 +- .../{run_parallel_test => }/ParallelTest.java | 2 +- .../BrowserStackTestNGTest.java | 75 ---------------- .../BrowserStackTestNGTest.java | 88 ------------------- .../run_first_test/first.testng.xml | 4 +- .../run_local_test/local.testng.xml | 4 +- .../run_parallel_test/parallel.testng.xml | 6 +- 9 files changed, 29 insertions(+), 179 deletions(-) rename android/testng-examples/src/test/java/com/browserstack/{run_parallel_test => }/BrowserStackTestNGTest.java (81%) rename android/testng-examples/src/test/java/com/browserstack/{run_first_test => }/FirstTest.java (96%) rename android/testng-examples/src/test/java/com/browserstack/{run_local_test => }/LocalTest.java (97%) rename android/testng-examples/src/test/java/com/browserstack/{run_parallel_test => }/ParallelTest.java (96%) delete mode 100644 android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java delete mode 100644 android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java diff --git a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java similarity index 81% rename from android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java rename to android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java index 5af7d8b..dccd9de 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java @@ -1,37 +1,37 @@ -package com.browserstack.run_parallel_test; +package com.browserstack; +import com.browserstack.local.Local; import java.net.URL; import java.util.Map; import java.util.HashMap; + import java.util.Iterator; import java.io.FileReader; -import java.time.Duration; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.JSONArray; import io.appium.java_client.android.AndroidDriver; - -import org.openqa.selenium.WebElement; import io.appium.java_client.android.options.UiAutomator2Options; +import org.openqa.selenium.WebElement; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; -import org.testng.annotations.Parameters; public class BrowserStackTestNGTest { public AndroidDriver driver; + private Local local; @BeforeMethod(alwaysRun=true) - @org.testng.annotations.Parameters(value={"deviceIndex"}) - public void setUp(String deviceIndex) throws Exception { + @org.testng.annotations.Parameters(value = { "config", "deviceIndex" }) + public void setUp(String config_file, String deviceIndex) throws Exception { JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json")); - JSONArray envs = (JSONArray) config.get("environments"); + JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/" + config_file)); UiAutomator2Options options = new UiAutomator2Options(); + JSONArray envs = (JSONArray) config.get("environments"); Map envCapabilities = (Map) envs.get(Integer.parseInt(deviceIndex)); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { @@ -66,6 +66,13 @@ public void setUp(String deviceIndex) throws Exception { options.setCapability("app", app); } + if(browserstackOptions.get("local") != null && browserstackOptions.get("local").toString() == "true"){ + local = new Local(); + Map LocalOptions = new HashMap(); + LocalOptions.put("key", accessKey); + local.start(LocalOptions); + } + driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); } diff --git a/android/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java b/android/testng-examples/src/test/java/com/browserstack/FirstTest.java similarity index 96% rename from android/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java rename to android/testng-examples/src/test/java/com/browserstack/FirstTest.java index ce8bd07..f276f19 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/FirstTest.java @@ -1,4 +1,4 @@ -package com.browserstack.run_first_test; +package com.browserstack; import io.appium.java_client.AppiumBy; diff --git a/android/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java b/android/testng-examples/src/test/java/com/browserstack/LocalTest.java similarity index 97% rename from android/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java rename to android/testng-examples/src/test/java/com/browserstack/LocalTest.java index 20709c7..6dd40ce 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/LocalTest.java @@ -1,4 +1,4 @@ -package com.browserstack.run_local_test; +package com.browserstack; import org.testng.Assert; import org.testng.annotations.Test; diff --git a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java b/android/testng-examples/src/test/java/com/browserstack/ParallelTest.java similarity index 96% rename from android/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java rename to android/testng-examples/src/test/java/com/browserstack/ParallelTest.java index 24494b0..07cd0a0 100644 --- a/android/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/ParallelTest.java @@ -1,4 +1,4 @@ -package com.browserstack.run_parallel_test; +package com.browserstack; import io.appium.java_client.AppiumBy; diff --git a/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java deleted file mode 100644 index 151be43..0000000 --- a/android/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.browserstack.run_first_test; - -import java.net.URL; -import java.util.Map; -import java.util.HashMap; - -import java.util.Iterator; -import java.io.FileReader; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.JSONArray; - -import io.appium.java_client.android.AndroidDriver; -import io.appium.java_client.android.options.UiAutomator2Options; -import org.openqa.selenium.WebElement; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.AfterMethod; - - -public class BrowserStackTestNGTest { - public AndroidDriver driver; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_first_test/first.conf.json")); - - UiAutomator2Options options = new UiAutomator2Options(); - - JSONArray envs = (JSONArray) config.get("environments"); - Map envCapabilities = (Map) envs.get(0); - Iterator it = envCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - options.setCapability(pair.getKey().toString(), pair.getValue().toString()); - } - - Map commonCapabilities = (Map) config.get("capabilities"); - it = commonCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - if(options.getCapability(pair.getKey().toString()) == null){ - options.setCapability(pair.getKey().toString(), pair.getValue()); - } - } - - HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); - options.setCapability("bstack:options", browserstackOptions); - - String username = System.getenv("BROWSERSTACK_USERNAME"); - if(username == null) { - username = (String) browserstackOptions.get("userName"); - } - - String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); - if(accessKey == null) { - accessKey = (String) browserstackOptions.get("accessKey"); - } - - String app = System.getenv("BROWSERSTACK_APP_ID"); - if(app != null && !app.isEmpty()) { - options.setCapability("app", app); - } - - driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - // Invoke driver.quit() to indicate that the test is completed. - // Otherwise, it will appear as timed out on BrowserStack. - driver.quit(); - } -} diff --git a/android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java deleted file mode 100644 index e983442..0000000 --- a/android/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.browserstack.run_local_test; - -import com.browserstack.local.Local; - -import java.net.URL; -import java.util.Map; -import java.util.HashMap; -import java.util.Iterator; -import java.io.FileReader; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.JSONArray; - -import io.appium.java_client.android.AndroidDriver; -import io.appium.java_client.android.options.UiAutomator2Options; - -import org.openqa.selenium.WebElement; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.AfterMethod; - -public class BrowserStackTestNGTest { - public AndroidDriver driver; - private Local local; - - @BeforeMethod(alwaysRun = true) - public void setUp() throws Exception { - JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser - .parse(new FileReader("src/test/resources/com/browserstack/run_local_test/local.conf.json")); - JSONArray envs = (JSONArray) config.get("environments"); - - UiAutomator2Options options = new UiAutomator2Options(); - - Map envCapabilities = (Map) envs.get(0); - Iterator it = envCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry) it.next(); - options.setCapability(pair.getKey().toString(), pair.getValue().toString()); - } - - Map commonCapabilities = (Map) config.get("capabilities"); - it = commonCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry) it.next(); - if (options.getCapability(pair.getKey().toString()) == null) { - options.setCapability(pair.getKey().toString(), pair.getValue()); - } - } - - HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); - options.setCapability("bstack:options", browserstackOptions); - - String username = System.getenv("BROWSERSTACK_USERNAME"); - if (username == null) { - username = (String) browserstackOptions.get("userName"); - } - - String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); - if (accessKey == null) { - accessKey = (String) browserstackOptions.get("accessKey"); - } - - String app = System.getenv("BROWSERSTACK_APP_ID"); - if (app != null && !app.isEmpty()) { - options.setCapability("app", app); - } - - if (browserstackOptions.get("local") != null - && browserstackOptions.get("local").toString() == "true") { - local = new Local(); - Map LocalOptions = new HashMap(); - LocalOptions.put("key", accessKey); - local.start(LocalOptions); - } - - driver = new AndroidDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); - } - - @AfterMethod(alwaysRun = true) - public void tearDown() throws Exception { - // Invoke driver.quit() to indicate that the test is completed. - // Otherwise, it will appear as timed out on BrowserStack. - driver.quit(); - if (local != null) - local.stop(); - } -} diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml b/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml index 8624aeb..51aee70 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml +++ b/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml @@ -2,8 +2,10 @@ + + - + diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml b/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml index 26bcaff..73e32d7 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml +++ b/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml @@ -2,8 +2,10 @@ + + - + diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml b/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml index b009413..4c26875 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml +++ b/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml @@ -3,15 +3,17 @@ + - + + - + From 5c49eda8eeada902a98bfc75f6217e4e80859a87 Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Sun, 28 Aug 2022 13:23:23 +0530 Subject: [PATCH 06/10] IOS | Use parametrized config for common base class --- .../BrowserStackTestNGTest.java | 10 +-- .../{run_first_test => }/FirstTest.java | 2 +- .../{run_local_test => }/LocalTest.java | 2 +- .../{run_parallel_test => }/ParallelTest.java | 2 +- .../BrowserStackTestNGTest.java | 77 ------------------- .../BrowserStackTestNGTest.java | 76 ------------------ .../run_first_test/first.testng.xml | 4 +- .../run_local_test/local.testng.xml | 4 +- .../run_parallel_test/parallel.testng.xml | 6 +- 9 files changed, 18 insertions(+), 165 deletions(-) rename ios/testng-examples/src/test/java/com/browserstack/{run_local_test => }/BrowserStackTestNGTest.java (90%) rename ios/testng-examples/src/test/java/com/browserstack/{run_first_test => }/FirstTest.java (96%) rename ios/testng-examples/src/test/java/com/browserstack/{run_local_test => }/LocalTest.java (97%) rename ios/testng-examples/src/test/java/com/browserstack/{run_parallel_test => }/ParallelTest.java (96%) delete mode 100644 ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java delete mode 100644 ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java b/ios/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java similarity index 90% rename from ios/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java rename to ios/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java index 81bca2d..6770940 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/BrowserStackTestNGTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java @@ -1,4 +1,4 @@ -package com.browserstack.run_local_test; +package com.browserstack; import com.browserstack.local.Local; import java.net.URL; @@ -16,7 +16,6 @@ import org.testng.annotations.AfterMethod; import io.appium.java_client.ios.IOSDriver; -import org.openqa.selenium.WebElement; public class BrowserStackTestNGTest { @@ -24,14 +23,15 @@ public class BrowserStackTestNGTest { private Local local; @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { + @org.testng.annotations.Parameters(value = { "config", "deviceIndex" }) + public void setUp(String config_file, String deviceIndex) throws Exception { JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_local_test/local.conf.json")); + JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/"+config_file)); JSONArray envs = (JSONArray) config.get("environments"); XCUITestOptions options = new XCUITestOptions(); - Map envCapabilities = (Map) envs.get(0); + Map envCapabilities = (Map) envs.get(Integer.parseInt(deviceIndex)); Iterator it = envCapabilities.entrySet().iterator(); while (it.hasNext()) { Map.Entry pair = (Map.Entry)it.next(); diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java b/ios/testng-examples/src/test/java/com/browserstack/FirstTest.java similarity index 96% rename from ios/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java rename to ios/testng-examples/src/test/java/com/browserstack/FirstTest.java index cc198ae..73736fd 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/FirstTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/FirstTest.java @@ -1,4 +1,4 @@ -package com.browserstack.run_first_test; +package com.browserstack; import org.testng.Assert; import org.testng.annotations.Test; diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java b/ios/testng-examples/src/test/java/com/browserstack/LocalTest.java similarity index 97% rename from ios/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java rename to ios/testng-examples/src/test/java/com/browserstack/LocalTest.java index 858dea9..295100c 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_local_test/LocalTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/LocalTest.java @@ -1,4 +1,4 @@ -package com.browserstack.run_local_test; +package com.browserstack; import org.testng.Assert; import org.testng.annotations.Test; diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java b/ios/testng-examples/src/test/java/com/browserstack/ParallelTest.java similarity index 96% rename from ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java rename to ios/testng-examples/src/test/java/com/browserstack/ParallelTest.java index 6509021..226b704 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/ParallelTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/ParallelTest.java @@ -1,4 +1,4 @@ -package com.browserstack.run_parallel_test; +package com.browserstack; import org.testng.Assert; import org.testng.annotations.Test; diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java deleted file mode 100644 index e01b61f..0000000 --- a/ios/testng-examples/src/test/java/com/browserstack/run_first_test/BrowserStackTestNGTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.browserstack.run_first_test; -import com.browserstack.local.Local; - -import java.net.URL; -import java.util.Map; -import java.util.HashMap; -import java.util.Iterator; -import java.io.FileReader; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.JSONArray; - -import io.appium.java_client.ios.options.XCUITestOptions; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.AfterMethod; - -import io.appium.java_client.ios.IOSDriver; - -import org.openqa.selenium.WebElement; - - -public class BrowserStackTestNGTest { - public IOSDriver driver; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_first_test/first.conf.json")); - JSONArray envs = (JSONArray) config.get("environments"); - - XCUITestOptions options = new XCUITestOptions(); - - Map envCapabilities = (Map) envs.get(0); - Iterator it = envCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - options.setCapability(pair.getKey().toString(), pair.getValue().toString()); - } - - Map commonCapabilities = (Map) config.get("capabilities"); - it = commonCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - if(options.getCapability(pair.getKey().toString()) == null){ - options.setCapability(pair.getKey().toString(), pair.getValue()); - } - } - - HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); - options.setCapability("bstack:options", browserstackOptions); - - String username = System.getenv("BROWSERSTACK_USERNAME"); - if(username == null) { - username = (String) browserstackOptions.get("userName"); - } - - String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); - if(accessKey == null) { - accessKey = (String) browserstackOptions.get("accessKey"); - } - - String app = System.getenv("BROWSERSTACK_APP_ID"); - if(app != null && !app.isEmpty()) { - options.setCapability("app", app); - } - - driver = new IOSDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - // Invoke driver.quit() to indicate that the test is completed. - // Otherwise, it will appear as timed out on BrowserStack. - driver.quit(); - } -} diff --git a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java b/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java deleted file mode 100644 index 25c6540..0000000 --- a/ios/testng-examples/src/test/java/com/browserstack/run_parallel_test/BrowserStackTestNGTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.browserstack.run_parallel_test; - -import java.net.URL; -import java.util.Map; -import java.util.HashMap; -import java.util.Iterator; -import java.io.FileReader; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.JSONArray; - -import io.appium.java_client.ios.options.XCUITestOptions; - -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.AfterMethod; - -import io.appium.java_client.ios.IOSDriver; -import org.openqa.selenium.WebElement; - - -public class BrowserStackTestNGTest { - public IOSDriver driver; - - @BeforeMethod(alwaysRun=true) - @org.testng.annotations.Parameters(value={"deviceIndex"}) - public void setUp(String deviceIndex) throws Exception { - JSONParser parser = new JSONParser(); - JSONObject config = (JSONObject) parser.parse(new FileReader("src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json")); - JSONArray envs = (JSONArray) config.get("environments"); - - XCUITestOptions options = new XCUITestOptions(); - - Map envCapabilities = (Map) envs.get(Integer.parseInt(deviceIndex)); - Iterator it = envCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - options.setCapability(pair.getKey().toString(), pair.getValue().toString()); - } - - Map commonCapabilities = (Map) config.get("capabilities"); - it = commonCapabilities.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry pair = (Map.Entry)it.next(); - if(options.getCapability(pair.getKey().toString()) == null){ - options.setCapability(pair.getKey().toString(), pair.getValue()); - } - } - - HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); - options.setCapability("bstack:options", browserstackOptions); - - String username = System.getenv("BROWSERSTACK_USERNAME"); - if(username == null) { - username = (String) browserstackOptions.get("userName"); - } - - String accessKey = System.getenv("BROWSERSTACK_ACCESS_KEY"); - if(accessKey == null) { - accessKey = (String) browserstackOptions.get("accessKey"); - } - - String app = System.getenv("BROWSERSTACK_APP_ID"); - if(app != null && !app.isEmpty()) { - options.setCapability("app", app); - } - - driver = new IOSDriver(new URL("http://"+config.get("server")+"/wd/hub"), options); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - // Invoke driver.quit() to indicate that the test is completed. - // Otherwise, it will appear as timed out on BrowserStack. - driver.quit(); - } -} diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml b/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml index 8624aeb..51aee70 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.testng.xml @@ -2,8 +2,10 @@ + + - + diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml b/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml index 26bcaff..73e32d7 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.testng.xml @@ -2,8 +2,10 @@ + + - + diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml b/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml index b009413..4c26875 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.testng.xml @@ -3,15 +3,17 @@ + - + + - + From 5b4e7d22c6bea897be16dd41aa8da267b3b12359 Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Sun, 28 Aug 2022 16:50:54 +0530 Subject: [PATCH 07/10] better config format --- .../browserstack/BrowserStackTestNGTest.java | 7 +++++-- .../run_first_test/first.conf.json | 19 ++++++++--------- .../run_local_test/local.conf.json | 21 +++++++++---------- .../run_parallel_test/parallel.conf.json | 19 ++++++++--------- .../browserstack/BrowserStackTestNGTest.java | 7 +++++-- .../run_first_test/first.conf.json | 19 ++++++++--------- .../run_local_test/local.conf.json | 21 +++++++++---------- .../run_parallel_test/parallel.conf.json | 19 ++++++++--------- 8 files changed, 66 insertions(+), 66 deletions(-) diff --git a/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java index dccd9de..6d888ed 100644 --- a/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java @@ -45,11 +45,14 @@ public void setUp(String config_file, String deviceIndex) throws Exception { Map.Entry pair = (Map.Entry)it.next(); if(options.getCapability(pair.getKey().toString()) == null){ options.setCapability(pair.getKey().toString(), pair.getValue()); + } else if (pair.getKey().toString().equalsIgnoreCase("bstack:options")){ + HashMap bstackOptionsMap = (HashMap) pair.getValue(); + bstackOptionsMap.putAll((HashMap) options.getCapability("bstack:options")); + options.setCapability(pair.getKey().toString(), bstackOptionsMap); } } - HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); - options.setCapability("bstack:options", browserstackOptions); + JSONObject browserstackOptions = (JSONObject) options.getCapability("bstack:options"); String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json b/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json index 7b6553a..f2093c4 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json +++ b/android/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json @@ -2,16 +2,15 @@ "server": "hub.browserstack.com", "capabilities": { - "app": "bs://" - }, - - "browserstackOptions": { - "projectName": "First TestNg Android Project", - "buildName": "browserstack-build-1", - "sessionName": "BStack first_test", - "debug": true, - "userName": "BROWSERSTACK_USERNAME", - "accessKey": "BROWSERSTACK_ACCESS_KEY" + "app": "bs://", + "bstack:options": { + "projectName": "First TestNg Android Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack first_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + } }, "environments": [ diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json b/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json index 03ff594..007467a 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json +++ b/android/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json @@ -2,17 +2,16 @@ "server": "hub.browserstack.com", "capabilities": { - "app": "bs://" - }, - - "browserstackOptions": { - "projectName": "First TestNg Android Project", - "buildName": "browserstack-build-1", - "sessionName": "BStack local_test", - "userName": "BROWSERSTACK_USERNAME", - "accessKey": "BROWSERSTACK_ACCESS_KEY", - "debug": true, - "local": true + "app": "bs://", + "bstack:options": { + "projectName": "First TestNg Android Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack local_test", + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY", + "debug": true, + "local": true + } }, "environments": [ diff --git a/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json b/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json index fe2b014..1715e23 100644 --- a/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json +++ b/android/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json @@ -2,16 +2,15 @@ "server": "hub.browserstack.com", "capabilities": { - "app": "bs://" - }, - - "browserstackOptions": { - "projectName": "First TestNg Android Project", - "buildName": "browserstack-build-1", - "sessionName": "BStack parallel_test", - "debug": true, - "userName": "BROWSERSTACK_USERNAME", - "accessKey": "BROWSERSTACK_ACCESS_KEY" + "app": "bs://", + "bstack:options": { + "projectName": "First TestNg Android Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack parallel_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + } }, "environments": [ diff --git a/ios/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java b/ios/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java index 6770940..3434967 100644 --- a/ios/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java +++ b/ios/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java @@ -44,11 +44,14 @@ public void setUp(String config_file, String deviceIndex) throws Exception { Map.Entry pair = (Map.Entry)it.next(); if(options.getCapability(pair.getKey().toString()) == null){ options.setCapability(pair.getKey().toString(), pair.getValue()); + } else if (pair.getKey().toString().equalsIgnoreCase("bstack:options")){ + HashMap bstackOptionsMap = (HashMap) pair.getValue(); + bstackOptionsMap.putAll((HashMap) options.getCapability("bstack:options")); + options.setCapability(pair.getKey().toString(), bstackOptionsMap); } } - HashMap browserstackOptions = (HashMap) config.get("browserstackOptions"); - options.setCapability("bstack:options", browserstackOptions); + JSONObject browserstackOptions = (JSONObject) options.getCapability("bstack:options"); String username = System.getenv("BROWSERSTACK_USERNAME"); if(username == null) { diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json b/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json index 8a106e7..8ae888e 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_first_test/first.conf.json @@ -2,16 +2,15 @@ "server": "hub.browserstack.com", "capabilities": { - "app": "bs://" - }, - - "browserstackOptions": { - "projectName": "First TestNg iOS Project", - "buildName": "browserstack-build-1", - "sessionName": "BStack first_test", - "debug": true, - "userName": "BROWSERSTACK_USERNAME", - "accessKey": "BROWSERSTACK_ACCESS_KEY" + "app": "bs://", + "bstack:options": { + "projectName": "First TestNg iOS Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack first_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + } }, "environments": [ diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json b/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json index 366cee5..2c214f3 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_local_test/local.conf.json @@ -2,17 +2,16 @@ "server": "hub.browserstack.com", "capabilities": { - "app": "bs://" - }, - - "browserstackOptions": { - "projectName": "First TestNg iOS Project", - "buildName": "browserstack-build-1", - "sessionName": "BStack local_test", - "debug": true, - "local": true, - "userName": "BROWSERSTACK_USERNAME", - "accessKey": "BROWSERSTACK_ACCESS_KEY" + "app": "bs://", + "bstack:options": { + "projectName": "First TestNg iOS Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack local_test", + "debug": true, + "local": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + } }, "environments": [ diff --git a/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json b/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json index c27bae4..aab4b7f 100644 --- a/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json +++ b/ios/testng-examples/src/test/resources/com/browserstack/run_parallel_test/parallel.conf.json @@ -2,16 +2,15 @@ "server": "hub.browserstack.com", "capabilities": { - "app": "bs://" - }, - - "browserstackOptions": { - "projectName": "First TestNg iOS Project", - "buildName": "browserstack-build-1", - "sessionName": "BStack parallel_test", - "debug": true, - "userName": "BROWSERSTACK_USERNAME", - "accessKey": "BROWSERSTACK_ACCESS_KEY" + "app": "bs://", + "bstack:options": { + "projectName": "First TestNg iOS Project", + "buildName": "browserstack-build-1", + "sessionName": "BStack parallel_test", + "debug": true, + "userName": "BROWSERSTACK_USERNAME", + "accessKey": "BROWSERSTACK_ACCESS_KEY" + } }, "environments": [ From 4cd8247c51685f8751cc4648b59abe275a161347 Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Sun, 28 Aug 2022 17:39:58 +0530 Subject: [PATCH 08/10] Stop local in teardown process --- .../src/test/java/com/browserstack/BrowserStackTestNGTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java b/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java index 6d888ed..ef88a76 100644 --- a/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java +++ b/android/testng-examples/src/test/java/com/browserstack/BrowserStackTestNGTest.java @@ -84,5 +84,6 @@ public void tearDown() throws Exception { // Invoke driver.quit() to indicate that the test is completed. // Otherwise, it will appear as timed out on BrowserStack. driver.quit(); + if(local != null) local.stop(); } } From 3d8b59068ab13a62299739c659701fbfd6588a8b Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Mon, 29 Aug 2022 19:46:47 +0530 Subject: [PATCH 09/10] Fix steps to run tests in readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 67c7a5f..a9388b6 100644 --- a/README.md +++ b/README.md @@ -47,17 +47,17 @@ Getting Started with Appium tests in TestNg on BrowserStack couldn't be easier! ### **Run first test :** -- Switch to `run_first_test` directory under [Android examples](android/testng-examples) or [iOS examples](ios/testng-examples) +- Switch to directory [Android examples](android/testng-examples) or [iOS examples](ios/testng-examples) - Follow the steps outlined in the documentation - [Get Started with your first test on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/testng) ### **Speed up test execution with parallel testing :** -- Switch to `run_parallel_test` directory under [Android examples](android/testng-examples/) or [iOS examples](ios/testng-examples/) +- Switch to directory [Android examples](android/testng-examples/) or [iOS examples](ios/testng-examples/) - Follow the steps outlined in the documentation - [Get Started with parallel testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/testng/parallelize-tests) ### **Use Local testing for apps that access resources hosted in development or testing environments :** -- Switch to `run_local_test` directory under [Android examples](android/testng-examples/) or [iOS examples](ios/testng-examples/) +- Switch to directory [Android examples](android/testng-examples/) or [iOS examples](ios/testng-examples/) - Follow the steps outlined in the documentation - [Get Started with Local testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/testng/local-testing) **Note**: If you are facing any issues, refer [Getting Help section](#Getting-Help) From 361d3788d03d8f47754339df1b7f50ed9ea34d1c Mon Sep 17 00:00:00 2001 From: avinash-bharti Date: Mon, 29 Aug 2022 20:02:48 +0530 Subject: [PATCH 10/10] Add exact command to execute tests --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a9388b6..5409240 100644 --- a/README.md +++ b/README.md @@ -47,18 +47,18 @@ Getting Started with Appium tests in TestNg on BrowserStack couldn't be easier! ### **Run first test :** -- Switch to directory [Android examples](android/testng-examples) or [iOS examples](ios/testng-examples) -- Follow the steps outlined in the documentation - [Get Started with your first test on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/testng) +- Update `first.conf.json` file inside the `src/test/resources/com/browserstack/run_first_test` with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings) +- Run `mvn test -P first` ### **Speed up test execution with parallel testing :** -- Switch to directory [Android examples](android/testng-examples/) or [iOS examples](ios/testng-examples/) -- Follow the steps outlined in the documentation - [Get Started with parallel testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/testng/parallelize-tests) +- Update `parallel.conf.json` file inside the `src/test/resources/com/browserstack/run_parallel_test` with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings) +- Run `mvn test -P parallel` ### **Use Local testing for apps that access resources hosted in development or testing environments :** -- Switch to directory [Android examples](android/testng-examples/) or [iOS examples](ios/testng-examples/) -- Follow the steps outlined in the documentation - [Get Started with Local testing on App Automate](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/testng/local-testing) +- Update `local.conf.json` file inside the `src/test/resources/com/browserstack/run_local_test` with your [BrowserStack Username and Access Key](https://www.browserstack.com/accounts/settings) +- Run `mvn test -P local` **Note**: If you are facing any issues, refer [Getting Help section](#Getting-Help)