From 3e7d33bee365587cda70bf66b39c7d26cbdbd0e2 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Thu, 10 Mar 2022 20:34:28 +0530 Subject: [PATCH 01/17] Added support for java-client 8.0.0 --- src/test/java/android/BrowserStackSample.java | 26 +++++++++---------- .../java/android/BrowserStackSampleLocal.java | 26 +++++++++++-------- src/test/java/ios/BrowserStackSample.java | 21 ++++++++------- .../java/ios/BrowserStackSampleLocal.java | 18 ++++++++----- 4 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/test/java/android/BrowserStackSample.java b/src/test/java/android/BrowserStackSample.java index 136ee81..fba3e2e 100644 --- a/src/test/java/android/BrowserStackSample.java +++ b/src/test/java/android/BrowserStackSample.java @@ -5,15 +5,15 @@ import java.util.function.Function; import java.net.MalformedURLException; -import io.appium.java_client.MobileBy; -import io.appium.java_client.android.AndroidDriver; -import io.appium.java_client.android.AndroidElement; - import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; -import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.DesiredCapabilities; +import io.appium.java_client.AppiumBy; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.RemoteWebDriver; + public class BrowserStackSample { @@ -40,23 +40,23 @@ public static void main(String[] args) throws MalformedURLException, Interrupted // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - AndroidDriver driver = new AndroidDriver( + RemoteWebDriver driver = new RemoteWebDriver( new URL("http://hub.browserstack.com/wd/hub"), caps); - + // Test case for the BrowserStack sample Android app. // If you have uploaded your app, update the test case here. - AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until( + WebElement searchElement = (WebElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable( - MobileBy.AccessibilityId("Search Wikipedia"))); + AppiumBy.accessibilityId("Search Wikipedia"))); searchElement.click(); - AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until( + WebElement insertTextElement = (WebElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable( - MobileBy.id("org.wikipedia.alpha:id/search_src_text"))); + 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(allProductsName.size() > 0); diff --git a/src/test/java/android/BrowserStackSampleLocal.java b/src/test/java/android/BrowserStackSampleLocal.java index 0854db1..c977af5 100644 --- a/src/test/java/android/BrowserStackSampleLocal.java +++ b/src/test/java/android/BrowserStackSampleLocal.java @@ -2,8 +2,13 @@ import com.browserstack.local.Local; import java.net.URL; import java.util.*; -import io.appium.java_client.MobileBy; import io.appium.java_client.android.*; -import org.openqa.selenium.support.ui.*;import org.openqa.selenium.remote.*; +import io.appium.java_client.android.*; +import org.openqa.selenium.support.ui.*; +import org.openqa.selenium.remote.*; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.RemoteWebDriver; public class BrowserStackSampleLocal { @@ -51,21 +56,20 @@ public static void main(String[] args) throws Exception { // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - AndroidDriver driver = new AndroidDriver( - new URL("http://hub.browserstack.com/wd/hub"), capabilities); + RemoteWebDriver driver = new RemoteWebDriver(new URL("http://hub.browserstack.com/wd/hub"), capabilities); // Test case for the BrowserStack sample Android Local app. // If you have uploaded your app, update the test case here. - AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.id("com.example.android.basicnetworking:id/test_action"))); + WebElement searchElement = new WebDriverWait(driver, 30).until( + ExpectedConditions.elementToBeClickable(By.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, 30).until( + ExpectedConditions.elementToBeClickable(By.className("android.widget.TextView"))); - AndroidElement testElement = null; - List allTextViewElements = driver.findElementsByClassName("android.widget.TextView"); + WebElement testElement = null; + List allTextViewElements = driver.findElements(By.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/src/test/java/ios/BrowserStackSample.java b/src/test/java/ios/BrowserStackSample.java index 6fd9eea..ca09d8b 100644 --- a/src/test/java/ios/BrowserStackSample.java +++ b/src/test/java/ios/BrowserStackSample.java @@ -8,9 +8,10 @@ import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedConditions; -import io.appium.java_client.MobileBy; -import io.appium.java_client.ios.IOSDriver; -import io.appium.java_client.ios.IOSElement; +import io.appium.java_client.AppiumBy; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.RemoteWebDriver; public class BrowserStackSample { @@ -36,21 +37,21 @@ public static void main(String[] args) throws MalformedURLException, Interrupted // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - IOSDriver driver = new IOSDriver( + RemoteWebDriver driver = new RemoteWebDriver( new URL("http://hub-cloud.browserstack.com/wd/hub"), caps); // Test case for the BrowserStack sample iOS app. // If you have uploaded your app, update the test case here. - IOSElement textButton = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Button"))); + WebElement textButton = (WebElement) new WebDriverWait(driver, 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, 30).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input"))); textInput.sendKeys("hello@browserstack.com"); Thread.sleep(5000); - IOSElement textOutput = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Text Output"))); + WebElement textOutput = (WebElement) new WebDriverWait(driver, 30).until( + ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Output"))); if(textOutput != null && textOutput.getText().equals("hello@browserstack.com")) assert(true); else diff --git a/src/test/java/ios/BrowserStackSampleLocal.java b/src/test/java/ios/BrowserStackSampleLocal.java index f278a07..4663f1b 100644 --- a/src/test/java/ios/BrowserStackSampleLocal.java +++ b/src/test/java/ios/BrowserStackSampleLocal.java @@ -3,9 +3,13 @@ import com.browserstack.local.Local; import java.net.URL; import java.io.File; import java.util.*; import org.apache.commons.io.FileUtils; -import io.appium.java_client.MobileBy; import io.appium.java_client.ios.*; +import io.appium.java_client.ios.*; import org.openqa.selenium.*; import org.openqa.selenium.support.ui.*;import org.openqa.selenium.remote.*; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.RemoteWebDriver; public class BrowserStackSampleLocal { @@ -53,24 +57,24 @@ public static void main(String[] args) throws Exception { // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - IOSDriver driver = new IOSDriver( + RemoteWebDriver driver = new RemoteWebDriver( new URL("http://hub.browserstack.com/wd/hub"), capabilities); // Test case for the BrowserStack sample iOS Local app. // If you have uploaded your app, update the test case here. - IOSElement testButton = (IOSElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("TestBrowserStackLocal"))); + WebElement testButton = (WebElement) new WebDriverWait(driver, 30).until( + ExpectedConditions.elementToBeClickable(By.xpath("//*[@content-desc='TestBrowserStackLocal']"))); testButton.click(); - + WebDriverWait wait = new WebDriverWait(driver, 30); wait.until(new ExpectedCondition() { @Override public Boolean apply(WebDriver d) { - String result = d.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")).getAttribute("value"); + String result = d.findElement(By.xpath("//*[@content-desc='ResultBrowserStackLocal']")).getAttribute("value"); return result != null && result.length() > 0; } }); - IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.AccessibilityId("ResultBrowserStackLocal")); + WebElement resultElement = (WebElement) driver.findElement(By.xpath("//*[@content-desc='ResultBrowserStackLocal']")); String resultString = resultElement.getText().toLowerCase(); System.out.println(resultString); From 10120bb0ec87e4932beb2e1b835ad446c0b0931b Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Fri, 11 Mar 2022 10:57:03 +0530 Subject: [PATCH 02/17] Updated java-client version to 8.0.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 74fc8bd..300166e 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,7 @@ io.appium java-client - 7.0.0 + 8.0.0 com.browserstack From c1b2e32b1ddede09dadf5bb42f857d81ee0181e6 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Fri, 11 Mar 2022 14:58:55 +0530 Subject: [PATCH 03/17] added w3c capabilities --- src/test/java/android/BrowserStackSample.java | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/test/java/android/BrowserStackSample.java b/src/test/java/android/BrowserStackSample.java index fba3e2e..6559904 100644 --- a/src/test/java/android/BrowserStackSample.java +++ b/src/test/java/android/BrowserStackSample.java @@ -5,15 +5,14 @@ import java.util.function.Function; import java.net.MalformedURLException; -import org.openqa.selenium.support.ui.ExpectedConditions; -import org.openqa.selenium.support.ui.WebDriverWait; -import org.openqa.selenium.remote.DesiredCapabilities; - import io.appium.java_client.AppiumBy; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; +import org.openqa.selenium.remote.DesiredCapabilities; public class BrowserStackSample { @@ -23,26 +22,24 @@ public static void main(String[] args) throws MalformedURLException, Interrupted // Set your access credentials caps.setCapability("browserstack.user", "YOUR_USERNAME"); - caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); + caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); // Set URL of the application under test caps.setCapability("app", "bs://"); - // Specify device and os_version for testing - caps.setCapability("device", "Google Pixel 3"); - caps.setCapability("os_version", "9.0"); + // Specify deviceName and platformName for testing + caps.setCapability("deviceName", "Google Pixel 3"); + caps.setCapability("platformName", "android"); + caps.setCapability("platformVersion", "9.0"); // Set other BrowserStack capabilities caps.setCapability("project", "First Java Project"); caps.setCapability("build", "browserstack-build-1"); caps.setCapability("name", "first_test"); - // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver( - new URL("http://hub.browserstack.com/wd/hub"), caps); - + RemoteWebDriver driver = new RemoteWebDriver(new URL("http://hub.browserstack.com/wd/hub"), caps); // Test case for the BrowserStack sample Android app. // If you have uploaded your app, update the test case here. @@ -50,16 +47,16 @@ public static void main(String[] args) throws MalformedURLException, Interrupted ExpectedConditions.elementToBeClickable( AppiumBy.accessibilityId("Search Wikipedia"))); searchElement.click(); + WebElement insertTextElement = (WebElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable( AppiumBy.id("org.wikipedia.alpha:id/search_src_text"))); insertTextElement.sendKeys("BrowserStack"); Thread.sleep(5000); + List allProductsName = driver.findElements(AppiumBy.className("android.widget.TextView")); - assert(allProductsName.size() > 0); - // Invoke driver.quit() after the test is done to indicate that the test is completed. driver.quit(); From 15c4e051db867946882d617de3d9678fd3e430a0 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Fri, 11 Mar 2022 15:00:32 +0530 Subject: [PATCH 04/17] added w3c capabilities --- src/test/java/ios/BrowserStackSample.java | 46 +++++++++++------------ 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/test/java/ios/BrowserStackSample.java b/src/test/java/ios/BrowserStackSample.java index ca09d8b..2efee79 100644 --- a/src/test/java/ios/BrowserStackSample.java +++ b/src/test/java/ios/BrowserStackSample.java @@ -16,50 +16,50 @@ public class BrowserStackSample { public static void main(String[] args) throws MalformedURLException, InterruptedException { - DesiredCapabilities caps = new DesiredCapabilities(); - - // Set your access credentials - caps.setCapability("browserstack.user", "YOUR_USERNAME"); - caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); + + DesiredCapabilities caps = new DesiredCapabilities(); + + // Set your access credentials + caps.setCapability("browserstack.user", "YOUR_USERNAME"); + caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); // Set URL of the application under test caps.setCapability("app", "bs://"); - - // Specify device and os_version for testing - caps.setCapability("device", "iPhone 11 Pro"); - caps.setCapability("os_version", "13"); - // Set other BrowserStack capabilities - caps.setCapability("project", "First Java Project"); - caps.setCapability("build", "browserstack-build-1"); - caps.setCapability("name", "first_test"); - - - // Initialise the remote Webdriver using BrowserStack remote URL - // and desired capabilities defined above + // Specify device and os_version for testing + caps.setCapability("deviceName", "iPhone 11 Pro"); + caps.setCapability("platformName", "ios"); + caps.setCapability("platformVersion", "13"); + + // Set other BrowserStack capabilities + caps.setCapability("project", "First Java Project"); + caps.setCapability("build", "browserstack-build-1"); + caps.setCapability("name", "first_test"); + + // Initialise the remote Webdriver using BrowserStack remote URL + // and desired capabilities defined above RemoteWebDriver driver = new RemoteWebDriver( - new URL("http://hub-cloud.browserstack.com/wd/hub"), caps); + new URL("http://hub-cloud.browserstack.com/wd/hub"), caps); - // Test case for the BrowserStack sample iOS app. // If you have uploaded your app, update the test case here. WebElement textButton = (WebElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Button"))); textButton.click(); + WebElement textInput = (WebElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input"))); textInput.sendKeys("hello@browserstack.com"); Thread.sleep(5000); + WebElement textOutput = (WebElement) new WebDriverWait(driver, 30).until( ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Output"))); if(textOutput != null && textOutput.getText().equals("hello@browserstack.com")) assert(true); else assert(false); - + // Invoke driver.quit() after the test is done to indicate that the test is completed. driver.quit(); - - } - + } } From 8073b7f6726bfb23d5a24453800ec2f91f439149 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Fri, 11 Mar 2022 16:13:39 +0530 Subject: [PATCH 05/17] Added w3c capabilities and support --- .../java/android/BrowserStackSampleLocal.java | 154 ++++++++++-------- 1 file changed, 83 insertions(+), 71 deletions(-) diff --git a/src/test/java/android/BrowserStackSampleLocal.java b/src/test/java/android/BrowserStackSampleLocal.java index c977af5..33a88eb 100644 --- a/src/test/java/android/BrowserStackSampleLocal.java +++ b/src/test/java/android/BrowserStackSampleLocal.java @@ -1,26 +1,26 @@ package android; import com.browserstack.local.Local; -import java.net.URL; import java.util.*; -import io.appium.java_client.android.*; -import org.openqa.selenium.support.ui.*; -import org.openqa.selenium.remote.*; -import org.openqa.selenium.By; -import org.openqa.selenium.WebDriver; +import io.appium.java_client.AppiumBy; +import java.net.URL; +import java.time.Duration; +import java.util.*; import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.*; import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.support.ui.*; public class BrowserStackSampleLocal { - + private static Local localInstance; public static String userName = "YOUR_USERNAME"; public static String accessKey = "YOUR_ACCESS_KEY"; - public static void setupLocal() throws Exception { localInstance = new Local(); Map options = new HashMap(); options.put("key", accessKey); + options.put("local", "true"); localInstance.start(options); } @@ -28,67 +28,79 @@ public static void tearDownLocal() throws Exception { localInstance.stop(); } - public static void main(String[] args) throws Exception { - // Start the BrowserStack Local binary - setupLocal(); - - DesiredCapabilities capabilities = new DesiredCapabilities(); - - // Set your access credentials - capabilities.setCapability("browserstack.user", userName); - capabilities.setCapability("browserstack.key", accessKey); - - // Set URL of the application under test - capabilities.setCapability("app", "bs://"); - - // Specify device and os_version for testing - capabilities.setCapability("device", "Google Pixel 3"); - capabilities.setCapability("os_version", "9.0"); - - // Set the browserstack.local capability to true - capabilities.setCapability("browserstack.local", true); - - // Set other BrowserStack capabilities - capabilities.setCapability("project", "First Java Project"); - capabilities.setCapability("build", "browserstack-build-1"); - capabilities.setCapability("name", "local_test"); - - - // Initialise the remote Webdriver using BrowserStack remote URL - // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver(new URL("http://hub.browserstack.com/wd/hub"), capabilities); - - // Test case for the BrowserStack sample Android Local app. - // If you have uploaded your app, update the test case here. - WebElement searchElement = new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(By.id("com.example.android.basicnetworking:id/test_action"))); - searchElement.click(); - WebElement insertTextElement = (WebElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(By.className("android.widget.TextView"))); - - WebElement testElement = null; - List allTextViewElements = driver.findElements(By.className("android.widget.TextView")); - Thread.sleep(10); - for(WebElement textElement : allTextViewElements) { - if(textElement.getText().contains("The active connection is")) { - testElement = textElement; - } - } - - if(testElement == null) { - throw new Error("Cannot find the needed TextView element from app"); - } - String matchedString = testElement.getText(); - System.out.println(matchedString); - assert(matchedString.contains("The active connection is wifi")); - assert(matchedString.contains("Up and running")); - - // Invoke driver.quit() after the test is done to indicate that the test is completed. - driver.quit(); - - // Stop the BrowserStack Local binary - tearDownLocal(); - - } - + public static void main(String[] args) throws Exception { + // Start the BrowserStack Local binary + setupLocal(); + + DesiredCapabilities capabilities = new DesiredCapabilities(); + + // Set your access credentials + capabilities.setCapability("browserstack.user", userName); + capabilities.setCapability("browserstack.key", accessKey); + + // Set URL of the application under test + capabilities.setCapability("app", "bs://"); + + // Specify device and os_version for testing + capabilities.setCapability("deviceName", "Google Pixel 3"); + capabilities.setCapability("platformName", "android"); + capabilities.setCapability("platformVersion", "9.0"); + + // Set other BrowserStack capabilities + capabilities.setCapability("project", "First Java Project"); + capabilities.setCapability("build", "browserstack-build-1"); + capabilities.setCapability("name", "local_test"); + + // Initialise the remote Webdriver using BrowserStack remote URL + // and desired capabilities defined above + RemoteWebDriver driver = new RemoteWebDriver( + new URL("http://hub.browserstack.com/wd/hub"), + capabilities + ); + + // Test case for the BrowserStack sample Android Local app. + // If you have uploaded your app, update the test case here. + WebElement searchElement = new WebDriverWait(driver, Duration.ofSeconds(30)) + .until( + ExpectedConditions.elementToBeClickable( + AppiumBy.id("com.example.android.basicnetworking:id/test_action") + ) + ); + searchElement.click(); + + WebElement insertTextElement = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) + .until( + ExpectedConditions.elementToBeClickable( + AppiumBy.className("android.widget.TextView") + ) + ); + + WebElement testElement = null; + List allTextViewElements = driver.findElements( + AppiumBy.className("android.widget.TextView") + ); + Thread.sleep(10); + for (WebElement textElement : allTextViewElements) { + if (textElement.getText().contains("The active connection is")) { + testElement = textElement; + } + } + + if (testElement == null) { + throw new Error("Cannot find the needed TextView element from app"); + } + String matchedString = testElement.getText(); + System.out.println(matchedString); + assert (matchedString.contains("The active connection is wifi")); + assert (matchedString.contains("Up and running")); + + // Invoke driver.quit() after the test is done to indicate that the test is completed. + driver.quit(); + + // Stop the BrowserStack Local binary + tearDownLocal(); + } } From 680d0701ae9ce69129fccacb875e358a82d7a544 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Fri, 11 Mar 2022 16:19:59 +0530 Subject: [PATCH 06/17] added support for w3c and upgrade --- .../java/ios/BrowserStackSampleLocal.java | 187 ++++++++++-------- 1 file changed, 106 insertions(+), 81 deletions(-) diff --git a/src/test/java/ios/BrowserStackSampleLocal.java b/src/test/java/ios/BrowserStackSampleLocal.java index 4663f1b..d1b1521 100644 --- a/src/test/java/ios/BrowserStackSampleLocal.java +++ b/src/test/java/ios/BrowserStackSampleLocal.java @@ -1,99 +1,124 @@ package ios; import com.browserstack.local.Local; -import java.net.URL; import java.io.File; import java.util.*; +import io.appium.java_client.AppiumBy; +import java.io.File; +import java.net.URL; +import java.time.Duration; +import java.util.*; import org.apache.commons.io.FileUtils; -import io.appium.java_client.ios.*; import org.openqa.selenium.*; -import org.openqa.selenium.support.ui.*;import org.openqa.selenium.remote.*; -import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.*; import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.support.ui.*; public class BrowserStackSampleLocal { - - private static Local localInstance; - public static String userName = "YOUR_USERNAME"; - public static String accessKey = "YOUR_ACCESS_KEY"; - - - public static void setupLocal() throws Exception { - localInstance = new Local(); - Map options = new HashMap(); - options.put("key", accessKey); - localInstance.start(options); - } - public static void tearDownLocal() throws Exception { - localInstance.stop(); - } + private static Local localInstance; + public static String userName = "YOUR_USERNAME"; + public static String accessKey = "YOUR_ACCESS_KEY"; + + public static void setupLocal() throws Exception { + localInstance = new Local(); + Map options = new HashMap(); + options.put("key", accessKey); + options.put("local", "true"); + localInstance.start(options); + } + + public static void tearDownLocal() throws Exception { + localInstance.stop(); + } + + public static void main(String[] args) throws Exception { + // Start the BrowserStack Local binary + setupLocal(); + + DesiredCapabilities capabilities = new DesiredCapabilities(); + + // Set your access credentials + capabilities.setCapability("browserstack.user", userName); + capabilities.setCapability("browserstack.key", accessKey); + // Set URL of the application under test + capabilities.setCapability("app", ""); - public static void main(String[] args) throws Exception { - // Start the BrowserStack Local binary - setupLocal(); - - DesiredCapabilities capabilities = new DesiredCapabilities(); - - // Set your access credentials - capabilities.setCapability("browserstack.user", userName); - capabilities.setCapability("browserstack.key", accessKey); - - // Set URL of the application under test - capabilities.setCapability("app", "bs://"); - - // Specify device and os_version for testing - capabilities.setCapability("device", "iPhone 11 Pro"); - capabilities.setCapability("os_version", "13"); - - // Set the browserstack.local capability to true - capabilities.setCapability("browserstack.local", true); - - // Set other BrowserStack capabilities - capabilities.setCapability("project", "First Java Project"); - capabilities.setCapability("build", "browserstack-build-1"); - capabilities.setCapability("name", "local_test"); - - // Initialise the remote Webdriver using BrowserStack remote URL - // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver( - new URL("http://hub.browserstack.com/wd/hub"), capabilities); - - // Test case for the BrowserStack sample iOS Local app. - // If you have uploaded your app, update the test case here. - WebElement testButton = (WebElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(By.xpath("//*[@content-desc='TestBrowserStackLocal']"))); - testButton.click(); - - WebDriverWait wait = new WebDriverWait(driver, 30); - wait.until(new ExpectedCondition() { - @Override - public Boolean apply(WebDriver d) { - String result = d.findElement(By.xpath("//*[@content-desc='ResultBrowserStackLocal']")).getAttribute("value"); - return result != null && result.length() > 0; - } - }); - WebElement resultElement = (WebElement) driver.findElement(By.xpath("//*[@content-desc='ResultBrowserStackLocal']")); - - String resultString = resultElement.getText().toLowerCase(); - System.out.println(resultString); - if(resultString.contains("not working")) { - File scrFile = (File) ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); - FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "/screenshot.png")); - System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "/screenshot.png"); - throw new Error("Unexpected BrowserStackLocal test result"); + // Specify device and os_version for testing + capabilities.setCapability("deviceName", "iPhone 11 Pro"); + capabilities.setCapability("platformName", "ios"); + capabilities.setCapability("platformVersion", "13"); + + // Set the browserstack.local capability to true + capabilities.setCapability("browserstack.local", true); + + // Set other BrowserStack capabilities + capabilities.setCapability("project", "First Java Project"); + capabilities.setCapability("build", "browserstack-build-1"); + capabilities.setCapability("name", "local_test"); + + // Initialise the remote Webdriver using BrowserStack remote URL + // and desired capabilities defined above + RemoteWebDriver driver = new RemoteWebDriver( + new URL("http://hub.browserstack.com/wd/hub"), + capabilities + ); + + // Test case for the BrowserStack sample iOS Local app. + // If you have uploaded your app, update the test case here. + WebElement testButton = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) + .until( + ExpectedConditions.elementToBeClickable( + AppiumBy.accessibilityId("TestBrowserStackLocal") + ) + ); + testButton.click(); + + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30)); + wait.until( + new ExpectedCondition() { + @Override + public Boolean apply(WebDriver d) { + String result = d + .findElement(AppiumBy.accessibilityId("ResultBrowserStackLocal")) + .getAttribute("value"); + return result != null && result.length() > 0; } + } + ); + WebElement resultElement = (WebElement) driver.findElement( + AppiumBy.accessibilityId("ResultBrowserStackLocal") + ); + + String resultString = resultElement.getText().toLowerCase(); + System.out.println(resultString); + if (resultString.contains("not working")) { + File scrFile = (File) ((TakesScreenshot) driver).getScreenshotAs( + OutputType.FILE + ); + FileUtils.copyFile( + scrFile, + new File(System.getProperty("user.dir") + "/screenshot.png") + ); + System.out.println( + "Screenshot stored at " + + System.getProperty("user.dir") + + "/screenshot.png" + ); + throw new Error("Unexpected BrowserStackLocal test result"); + } - String expectedString = "Up and running"; - assert(resultString.contains(expectedString.toLowerCase())); + String expectedString = "Up and running"; + assert (resultString.contains(expectedString.toLowerCase())); - // Invoke driver.quit() after the test is done to indicate that the test is completed. - driver.quit(); - - // Stop the BrowserStack Local binary - tearDownLocal(); - - } + // Invoke driver.quit() after the test is done to indicate that the test is completed. + driver.quit(); + // Stop the BrowserStack Local binary + tearDownLocal(); + } } From 500451967e7e1b4cb2dc4d5edc4c20518211e821 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Fri, 11 Mar 2022 16:22:54 +0530 Subject: [PATCH 07/17] removed deprecated syntax --- src/test/java/android/BrowserStackSample.java | 104 ++++++++-------- src/test/java/ios/BrowserStackSample.java | 116 ++++++++++-------- 2 files changed, 119 insertions(+), 101 deletions(-) diff --git a/src/test/java/android/BrowserStackSample.java b/src/test/java/android/BrowserStackSample.java index 6559904..cb5c514 100644 --- a/src/test/java/android/BrowserStackSample.java +++ b/src/test/java/android/BrowserStackSample.java @@ -1,65 +1,71 @@ package android; +import io.appium.java_client.AppiumBy; +import java.net.MalformedURLException; import java.net.URL; +import java.time.Duration; import java.util.List; -import java.util.function.Function; -import java.net.MalformedURLException; - -import io.appium.java_client.AppiumBy; -import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; - import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; -import org.openqa.selenium.remote.DesiredCapabilities; public class BrowserStackSample { - public static void main(String[] args) throws MalformedURLException, InterruptedException { - - DesiredCapabilities caps = new DesiredCapabilities(); - - // Set your access credentials - caps.setCapability("browserstack.user", "YOUR_USERNAME"); - caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); - - // Set URL of the application under test - caps.setCapability("app", "bs://"); - - // Specify deviceName and platformName for testing - caps.setCapability("deviceName", "Google Pixel 3"); - caps.setCapability("platformName", "android"); - caps.setCapability("platformVersion", "9.0"); - - // Set other BrowserStack capabilities - caps.setCapability("project", "First Java Project"); - caps.setCapability("build", "browserstack-build-1"); - caps.setCapability("name", "first_test"); - - // Initialise the remote Webdriver using BrowserStack remote URL - // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver(new URL("http://hub.browserstack.com/wd/hub"), caps); + public static void main(String[] args) + throws MalformedURLException, InterruptedException { + DesiredCapabilities caps = new DesiredCapabilities(); + + // Set your access credentials + caps.setCapability("browserstack.user", "YOUR_USERNAME"); + caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); + + // Set URL of the application under test + caps.setCapability("app", "bs://"); + + // Specify deviceName and platformName for testing + caps.setCapability("deviceName", "Google Pixel 3"); + caps.setCapability("platformName", "android"); + caps.setCapability("platformVersion", "9.0"); + + // Set other BrowserStack capabilities + caps.setCapability("project", "First Java Project"); + caps.setCapability("build", "browserstack-build-1"); + caps.setCapability("name", "first_test"); + + // Initialise the remote Webdriver using BrowserStack remote URL + // and desired capabilities defined above + RemoteWebDriver driver = new RemoteWebDriver( + new URL("http://hub.browserstack.com/wd/hub"), + caps + ); - // Test case for the BrowserStack sample Android app. - // If you have uploaded your app, update the test case here. - WebElement searchElement = (WebElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable( - AppiumBy.accessibilityId("Search Wikipedia"))); - searchElement.click(); + // Test case for the BrowserStack sample Android app. + // If you have uploaded your app, update the test case here. + WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + .until( + ExpectedConditions.elementToBeClickable( + AppiumBy.accessibilityId("Search Wikipedia") + ) + ); + searchElement.click(); - WebElement insertTextElement = (WebElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable( - AppiumBy.id("org.wikipedia.alpha:id/search_src_text"))); - insertTextElement.sendKeys("BrowserStack"); - Thread.sleep(5000); + 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.findElements(AppiumBy.className("android.widget.TextView")); - assert(allProductsName.size() > 0); - - // Invoke driver.quit() after the test is done to indicate that the test is completed. - driver.quit(); - - } + List allProductsName = driver.findElements( + AppiumBy.className("android.widget.TextView") + ); + assert (allProductsName.size() > 0); + // Invoke driver.quit() after the test is done to indicate that the test is completed. + driver.quit(); + } } diff --git a/src/test/java/ios/BrowserStackSample.java b/src/test/java/ios/BrowserStackSample.java index 2efee79..6acc160 100644 --- a/src/test/java/ios/BrowserStackSample.java +++ b/src/test/java/ios/BrowserStackSample.java @@ -1,65 +1,77 @@ package ios; -import java.net.URL; -import java.util.List; +import io.appium.java_client.AppiumBy; import java.net.MalformedURLException; +import java.net.URL; +import java.time.Duration; -import org.openqa.selenium.support.ui.WebDriverWait; -import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.support.ui.ExpectedConditions; - -import io.appium.java_client.AppiumBy; -import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; public class BrowserStackSample { - public static void main(String[] args) throws MalformedURLException, InterruptedException { - - DesiredCapabilities caps = new DesiredCapabilities(); - - // Set your access credentials - caps.setCapability("browserstack.user", "YOUR_USERNAME"); - caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); - - // Set URL of the application under test - caps.setCapability("app", "bs://"); - - // Specify device and os_version for testing - caps.setCapability("deviceName", "iPhone 11 Pro"); - caps.setCapability("platformName", "ios"); - caps.setCapability("platformVersion", "13"); - - // Set other BrowserStack capabilities - caps.setCapability("project", "First Java Project"); - caps.setCapability("build", "browserstack-build-1"); - caps.setCapability("name", "first_test"); - - // Initialise the remote Webdriver using BrowserStack remote URL - // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver( - new URL("http://hub-cloud.browserstack.com/wd/hub"), caps); - - // Test case for the BrowserStack sample iOS app. - // If you have uploaded your app, update the test case here. - WebElement textButton = (WebElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Button"))); - textButton.click(); + public static void main(String[] args) + throws MalformedURLException, InterruptedException { + DesiredCapabilities caps = new DesiredCapabilities(); + + // Set your access credentials + caps.setCapability("browserstack.user", "YOUR_USERNAME"); + caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); + + // Set URL of the application under test + caps.setCapability("app", "bs://"); + + // Specify device and os_version for testing + caps.setCapability("deviceName", "iPhone 11 Pro"); + caps.setCapability("platformName", "ios"); + caps.setCapability("platformVersion", "13"); + + // Set other BrowserStack capabilities + caps.setCapability("project", "First Java Project"); + caps.setCapability("build", "browserstack-build-1"); + caps.setCapability("name", "first_test"); + + // Initialise the remote Webdriver using BrowserStack remote URL + // and desired capabilities defined above + RemoteWebDriver driver = new RemoteWebDriver( + new URL("http://hub-cloud.browserstack.com/wd/hub"), + caps + ); + + // Test case for the BrowserStack sample iOS app. + // If you have uploaded your app, update the test case here. + WebElement textButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + .until( + ExpectedConditions.elementToBeClickable( + AppiumBy.accessibilityId("Text Button") + ) + ); + textButton.click(); - WebElement textInput = (WebElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Input"))); - textInput.sendKeys("hello@browserstack.com"); - Thread.sleep(5000); + WebElement textInput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + .until( + ExpectedConditions.elementToBeClickable( + AppiumBy.accessibilityId("Text Input") + ) + ); + textInput.sendKeys("hello@browserstack.com"); + Thread.sleep(5000); - WebElement textOutput = (WebElement) new WebDriverWait(driver, 30).until( - ExpectedConditions.elementToBeClickable(AppiumBy.accessibilityId("Text Output"))); - if(textOutput != null && textOutput.getText().equals("hello@browserstack.com")) - assert(true); - else - assert(false); + WebElement textOutput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + .until( + ExpectedConditions.elementToBeClickable( + AppiumBy.accessibilityId("Text Output") + ) + ); + if ( + textOutput != null && + textOutput.getText().equals("hello@browserstack.com") + ) assert (true); else assert (false); - // Invoke driver.quit() after the test is done to indicate that the test is completed. - driver.quit(); - } + // Invoke driver.quit() after the test is done to indicate that the test is completed. + driver.quit(); + } } From 2a41e1dade1b80387cdbd4f092f7e47f58b965c4 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 11:06:36 +0530 Subject: [PATCH 08/17] added comment for w3c support --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index c8321a1..e8e80e1 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,10 @@ mvn clean install Getting Started with Appium tests in Java on BrowserStack couldn't be easier! +For java-client 8.0.0 and above +NOTE : Any BrowserStack capability passed outside bstack:options will not be honoured. +[Browserstack Capability Builder](https://www.browserstack.com/app-automate/capabilities?tag=w3c) + ### Run your first test : **1. Upload your Android or iOS App** From 9f20467d688bd9c9cbf28d2da3f02d2be1281301 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 11:06:58 +0530 Subject: [PATCH 09/17] w3c compliant --- src/test/java/android/BrowserStackSample.java | 28 +++++++++------ .../java/android/BrowserStackSampleLocal.java | 29 ++++++++++------ src/test/java/ios/BrowserStackSample.java | 31 ++++++++++------- .../java/ios/BrowserStackSampleLocal.java | 34 +++++++++++-------- 4 files changed, 75 insertions(+), 47 deletions(-) diff --git a/src/test/java/android/BrowserStackSample.java b/src/test/java/android/BrowserStackSample.java index cb5c514..6fdac68 100644 --- a/src/test/java/android/BrowserStackSample.java +++ b/src/test/java/android/BrowserStackSample.java @@ -1,13 +1,14 @@ package android; import io.appium.java_client.AppiumBy; +import io.appium.java_client.android.AndroidDriver; import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; +import java.util.HashMap; import java.util.List; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; @@ -16,27 +17,32 @@ public class BrowserStackSample { public static void main(String[] args) throws MalformedURLException, InterruptedException { DesiredCapabilities caps = new DesiredCapabilities(); - + HashMap browserstackOptions = new HashMap(); + // Set your access credentials - caps.setCapability("browserstack.user", "YOUR_USERNAME"); - caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); + browserstackOptions.put("userName", "YOUR_USERNAME"); + browserstackOptions.put("accessKey", "YOUR_ACCESS_KEY"); + // Set other BrowserStack capabilities + browserstackOptions.put("appiumVersion", "1.22.0"); + browserstackOptions.put("projectName", "First Java Project"); + browserstackOptions.put("buildName", "browserstack-build-1"); + browserstackOptions.put("sessionName", "first_test"); + + // Passing browserstack caspabilities inside bstack:options + caps.setCapability("bstack:options", browserstackOptions); + // Set URL of the application under test caps.setCapability("app", "bs://"); - + // Specify deviceName and platformName for testing caps.setCapability("deviceName", "Google Pixel 3"); caps.setCapability("platformName", "android"); caps.setCapability("platformVersion", "9.0"); - // Set other BrowserStack capabilities - caps.setCapability("project", "First Java Project"); - caps.setCapability("build", "browserstack-build-1"); - caps.setCapability("name", "first_test"); - // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver( + AndroidDriver driver = new AndroidDriver( new URL("http://hub.browserstack.com/wd/hub"), caps ); diff --git a/src/test/java/android/BrowserStackSampleLocal.java b/src/test/java/android/BrowserStackSampleLocal.java index 33a88eb..53acf12 100644 --- a/src/test/java/android/BrowserStackSampleLocal.java +++ b/src/test/java/android/BrowserStackSampleLocal.java @@ -2,12 +2,13 @@ import com.browserstack.local.Local; import io.appium.java_client.AppiumBy; +import io.appium.java_client.android.AndroidDriver; + import java.net.URL; import java.time.Duration; import java.util.*; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.*; -import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.*; public class BrowserStackSampleLocal { @@ -33,10 +34,23 @@ public static void main(String[] args) throws Exception { setupLocal(); DesiredCapabilities capabilities = new DesiredCapabilities(); - + HashMap browserstackOptions = new HashMap(); + // Set your access credentials - capabilities.setCapability("browserstack.user", userName); - capabilities.setCapability("browserstack.key", accessKey); + browserstackOptions.put("userName", userName); + browserstackOptions.put("accessKey", accessKey); + + // Set other BrowserStack capabilities + browserstackOptions.put("appiumVersion", "1.22.0"); + browserstackOptions.put("projectName", "First Java Project"); + browserstackOptions.put("buildName", "browserstack-build-1"); + browserstackOptions.put("sessionName", "local_test"); + + // Set the browserstack.local capability to true + browserstackOptions.put("local", "true"); + + // Passing browserstack capabilities inside bstack:options + capabilities.setCapability("bstack:options", browserstackOptions); // Set URL of the application under test capabilities.setCapability("app", "bs://"); @@ -46,14 +60,9 @@ public static void main(String[] args) throws Exception { capabilities.setCapability("platformName", "android"); capabilities.setCapability("platformVersion", "9.0"); - // Set other BrowserStack capabilities - capabilities.setCapability("project", "First Java Project"); - capabilities.setCapability("build", "browserstack-build-1"); - capabilities.setCapability("name", "local_test"); - // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver( + AndroidDriver driver = new AndroidDriver( new URL("http://hub.browserstack.com/wd/hub"), capabilities ); diff --git a/src/test/java/ios/BrowserStackSample.java b/src/test/java/ios/BrowserStackSample.java index 6acc160..edf3cc8 100644 --- a/src/test/java/ios/BrowserStackSample.java +++ b/src/test/java/ios/BrowserStackSample.java @@ -1,13 +1,15 @@ package ios; import io.appium.java_client.AppiumBy; +import io.appium.java_client.ios.IOSDriver; + import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; +import java.util.HashMap; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; -import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; @@ -16,10 +18,20 @@ public class BrowserStackSample { public static void main(String[] args) throws MalformedURLException, InterruptedException { DesiredCapabilities caps = new DesiredCapabilities(); - + HashMap browserstackOptions = new HashMap(); + // Set your access credentials - caps.setCapability("browserstack.user", "YOUR_USERNAME"); - caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY"); + browserstackOptions.put("userName", "YOUR_USERNAME"); + browserstackOptions.put("accessKey", "YOUR_ACCESS_KEY"); + + // Set other BrowserStack capabilities + browserstackOptions.put("appiumVersion", "1.22.0"); + browserstackOptions.put("projectName", "First Java Project"); + browserstackOptions.put("buildName", "browserstack-build-1"); + browserstackOptions.put("sessionName", "first_test"); + + // Passing browserstack caspabilities inside bstack:options + caps.setCapability("bstack:options", browserstackOptions); // Set URL of the application under test caps.setCapability("app", "bs://"); @@ -28,16 +40,11 @@ public static void main(String[] args) caps.setCapability("deviceName", "iPhone 11 Pro"); caps.setCapability("platformName", "ios"); caps.setCapability("platformVersion", "13"); - - // Set other BrowserStack capabilities - caps.setCapability("project", "First Java Project"); - caps.setCapability("build", "browserstack-build-1"); - caps.setCapability("name", "first_test"); - + // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver( - new URL("http://hub-cloud.browserstack.com/wd/hub"), + IOSDriver driver = new IOSDriver( + new URL("http://hub.browserstack.com/wd/hub"), caps ); diff --git a/src/test/java/ios/BrowserStackSampleLocal.java b/src/test/java/ios/BrowserStackSampleLocal.java index d1b1521..3ad93e3 100644 --- a/src/test/java/ios/BrowserStackSampleLocal.java +++ b/src/test/java/ios/BrowserStackSampleLocal.java @@ -2,6 +2,8 @@ import com.browserstack.local.Local; import io.appium.java_client.AppiumBy; +import io.appium.java_client.ios.IOSDriver; + import java.io.File; import java.net.URL; import java.time.Duration; @@ -11,7 +13,6 @@ import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.*; -import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.*; public class BrowserStackSampleLocal { @@ -37,30 +38,35 @@ public static void main(String[] args) throws Exception { setupLocal(); DesiredCapabilities capabilities = new DesiredCapabilities(); - + HashMap browserstackOptions = new HashMap(); + // Set your access credentials - capabilities.setCapability("browserstack.user", userName); - capabilities.setCapability("browserstack.key", accessKey); + browserstackOptions.put("userName", userName); + browserstackOptions.put("accessKey", accessKey); + + // Set other BrowserStack capabilities + browserstackOptions.put("appiumVersion", "1.22.0"); + browserstackOptions.put("projectName", "First Java Project"); + browserstackOptions.put("buildName", "browserstack-build-1"); + browserstackOptions.put("sessionName", "local_test"); + + // Set the browserstack.local capability to true + browserstackOptions.put("local", "true"); + + // Passing browserstack capabilities inside bstack:options + capabilities.setCapability("bstack:options", browserstackOptions); // Set URL of the application under test - capabilities.setCapability("app", ""); + capabilities.setCapability("app", "bs://"); // Specify device and os_version for testing capabilities.setCapability("deviceName", "iPhone 11 Pro"); capabilities.setCapability("platformName", "ios"); capabilities.setCapability("platformVersion", "13"); - // Set the browserstack.local capability to true - capabilities.setCapability("browserstack.local", true); - - // Set other BrowserStack capabilities - capabilities.setCapability("project", "First Java Project"); - capabilities.setCapability("build", "browserstack-build-1"); - capabilities.setCapability("name", "local_test"); - // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above - RemoteWebDriver driver = new RemoteWebDriver( + IOSDriver driver = new IOSDriver( new URL("http://hub.browserstack.com/wd/hub"), capabilities ); From 249f81877880942a359ccabed7e9c515cd505fb8 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 11:08:22 +0530 Subject: [PATCH 10/17] lint --- src/test/java/android/BrowserStackSample.java | 18 ++++++++++----- .../java/android/BrowserStackSampleLocal.java | 3 +-- src/test/java/ios/BrowserStackSample.java | 23 ++++++++++++------- .../java/ios/BrowserStackSampleLocal.java | 3 +-- 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/test/java/android/BrowserStackSample.java b/src/test/java/android/BrowserStackSample.java index 6fdac68..5fe0588 100644 --- a/src/test/java/android/BrowserStackSample.java +++ b/src/test/java/android/BrowserStackSample.java @@ -18,7 +18,7 @@ public static void main(String[] args) throws MalformedURLException, InterruptedException { DesiredCapabilities caps = new DesiredCapabilities(); HashMap browserstackOptions = new HashMap(); - + // Set your access credentials browserstackOptions.put("userName", "YOUR_USERNAME"); browserstackOptions.put("accessKey", "YOUR_ACCESS_KEY"); @@ -28,13 +28,13 @@ public static void main(String[] args) browserstackOptions.put("projectName", "First Java Project"); browserstackOptions.put("buildName", "browserstack-build-1"); browserstackOptions.put("sessionName", "first_test"); - + // Passing browserstack caspabilities inside bstack:options caps.setCapability("bstack:options", browserstackOptions); - + // Set URL of the application under test caps.setCapability("app", "bs://"); - + // Specify deviceName and platformName for testing caps.setCapability("deviceName", "Google Pixel 3"); caps.setCapability("platformName", "android"); @@ -49,7 +49,10 @@ public static void main(String[] args) // Test case for the BrowserStack sample Android app. // If you have uploaded your app, update the test case here. - WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + WebElement searchElement = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) .until( ExpectedConditions.elementToBeClickable( AppiumBy.accessibilityId("Search Wikipedia") @@ -57,7 +60,10 @@ public static void main(String[] args) ); searchElement.click(); - WebElement insertTextElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + WebElement insertTextElement = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) .until( ExpectedConditions.elementToBeClickable( AppiumBy.id("org.wikipedia.alpha:id/search_src_text") diff --git a/src/test/java/android/BrowserStackSampleLocal.java b/src/test/java/android/BrowserStackSampleLocal.java index 53acf12..e765543 100644 --- a/src/test/java/android/BrowserStackSampleLocal.java +++ b/src/test/java/android/BrowserStackSampleLocal.java @@ -3,7 +3,6 @@ import com.browserstack.local.Local; import io.appium.java_client.AppiumBy; import io.appium.java_client.android.AndroidDriver; - import java.net.URL; import java.time.Duration; import java.util.*; @@ -35,7 +34,7 @@ public static void main(String[] args) throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); HashMap browserstackOptions = new HashMap(); - + // Set your access credentials browserstackOptions.put("userName", userName); browserstackOptions.put("accessKey", accessKey); diff --git a/src/test/java/ios/BrowserStackSample.java b/src/test/java/ios/BrowserStackSample.java index edf3cc8..6449e25 100644 --- a/src/test/java/ios/BrowserStackSample.java +++ b/src/test/java/ios/BrowserStackSample.java @@ -2,12 +2,10 @@ import io.appium.java_client.AppiumBy; import io.appium.java_client.ios.IOSDriver; - import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; import java.util.HashMap; - import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedConditions; @@ -19,7 +17,7 @@ public static void main(String[] args) throws MalformedURLException, InterruptedException { DesiredCapabilities caps = new DesiredCapabilities(); HashMap browserstackOptions = new HashMap(); - + // Set your access credentials browserstackOptions.put("userName", "YOUR_USERNAME"); browserstackOptions.put("accessKey", "YOUR_ACCESS_KEY"); @@ -29,7 +27,7 @@ public static void main(String[] args) browserstackOptions.put("projectName", "First Java Project"); browserstackOptions.put("buildName", "browserstack-build-1"); browserstackOptions.put("sessionName", "first_test"); - + // Passing browserstack caspabilities inside bstack:options caps.setCapability("bstack:options", browserstackOptions); @@ -40,7 +38,7 @@ public static void main(String[] args) caps.setCapability("deviceName", "iPhone 11 Pro"); caps.setCapability("platformName", "ios"); caps.setCapability("platformVersion", "13"); - + // Initialise the remote Webdriver using BrowserStack remote URL // and desired capabilities defined above IOSDriver driver = new IOSDriver( @@ -50,7 +48,10 @@ public static void main(String[] args) // Test case for the BrowserStack sample iOS app. // If you have uploaded your app, update the test case here. - WebElement textButton = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + WebElement textButton = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) .until( ExpectedConditions.elementToBeClickable( AppiumBy.accessibilityId("Text Button") @@ -58,7 +59,10 @@ public static void main(String[] args) ); textButton.click(); - WebElement textInput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + WebElement textInput = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) .until( ExpectedConditions.elementToBeClickable( AppiumBy.accessibilityId("Text Input") @@ -67,7 +71,10 @@ public static void main(String[] args) textInput.sendKeys("hello@browserstack.com"); Thread.sleep(5000); - WebElement textOutput = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) + WebElement textOutput = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) .until( ExpectedConditions.elementToBeClickable( AppiumBy.accessibilityId("Text Output") diff --git a/src/test/java/ios/BrowserStackSampleLocal.java b/src/test/java/ios/BrowserStackSampleLocal.java index 3ad93e3..5ebf5a0 100644 --- a/src/test/java/ios/BrowserStackSampleLocal.java +++ b/src/test/java/ios/BrowserStackSampleLocal.java @@ -3,7 +3,6 @@ import com.browserstack.local.Local; import io.appium.java_client.AppiumBy; import io.appium.java_client.ios.IOSDriver; - import java.io.File; import java.net.URL; import java.time.Duration; @@ -39,7 +38,7 @@ public static void main(String[] args) throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); HashMap browserstackOptions = new HashMap(); - + // Set your access credentials browserstackOptions.put("userName", userName); browserstackOptions.put("accessKey", accessKey); From 1316fea994620e548070c7b15e7ae33fbfcb124e Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 11:11:49 +0530 Subject: [PATCH 11/17] Update README.md --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e8e80e1..3a827be 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,9 @@ mvn clean install Getting Started with Appium tests in Java on BrowserStack couldn't be easier! -For java-client 8.0.0 and above -NOTE : Any BrowserStack capability passed outside bstack:options will not be honoured. +### For java-client 8.0.0 and above + +NOTE : Any BrowserStack capability passed outside bstack:options will not be honoured \ [Browserstack Capability Builder](https://www.browserstack.com/app-automate/capabilities?tag=w3c) ### Run your first test : From 89317080f50ad42298ce2dad2cd24a0dcf8bbcd3 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 11:13:40 +0530 Subject: [PATCH 12/17] typo fix --- src/test/java/android/BrowserStackSample.java | 2 +- src/test/java/ios/BrowserStackSample.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/android/BrowserStackSample.java b/src/test/java/android/BrowserStackSample.java index 5fe0588..c923568 100644 --- a/src/test/java/android/BrowserStackSample.java +++ b/src/test/java/android/BrowserStackSample.java @@ -29,7 +29,7 @@ public static void main(String[] args) browserstackOptions.put("buildName", "browserstack-build-1"); browserstackOptions.put("sessionName", "first_test"); - // Passing browserstack caspabilities inside bstack:options + // Passing browserstack capabilities inside bstack:options caps.setCapability("bstack:options", browserstackOptions); // Set URL of the application under test diff --git a/src/test/java/ios/BrowserStackSample.java b/src/test/java/ios/BrowserStackSample.java index 6449e25..3b5f864 100644 --- a/src/test/java/ios/BrowserStackSample.java +++ b/src/test/java/ios/BrowserStackSample.java @@ -28,7 +28,7 @@ public static void main(String[] args) browserstackOptions.put("buildName", "browserstack-build-1"); browserstackOptions.put("sessionName", "first_test"); - // Passing browserstack caspabilities inside bstack:options + // Passing browserstack capabilities inside bstack:options caps.setCapability("bstack:options", browserstackOptions); // Set URL of the application under test From 63478c05c0223b41da88528cffac5f04cddb6ca9 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 11:52:59 +0530 Subject: [PATCH 13/17] Update README.md --- README.md | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a827be..c84c95b 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,30 @@ Getting Started with Appium tests in Java on BrowserStack couldn't be easier! ### For java-client 8.0.0 and above -NOTE : Any BrowserStack capability passed outside bstack:options will not be honoured \ +- Any BrowserStack capability passed outside bstack:options will not be honoured \ [Browserstack Capability Builder](https://www.browserstack.com/app-automate/capabilities?tag=w3c) +- AppiumBy is available with java-client 8.0.0 . For java-client < 8.0.0, MobileBy can be used. + +- WebDriverWait constructor requires time to be passed as a type Duration. So with java-client 8.0.0, pass wait time as a new Duration +- java-client v-7.0.0 + ``` + WebElement searchElement = (WebElement) new WebDriverWait( + driver, + 30 + ) + ``` + + java-client v-8.0.0 + ``` + WebElement searchElement = (WebElement) new WebDriverWait( + driver, + Duration.ofSeconds(30) + ) + ``` + + Refer this for tracking changes in java-client 8.0.0 documentation - [v7-to-v8-migration-guide](https://github.com/appium/java-client/blob/master/docs/v7-to-v8-migration-guide.md#mobileelement) + ### Run your first test : **1. Upload your Android or iOS App** From e2cf186e323047c4e5461c3d0eb187d03a565258 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 12:00:55 +0530 Subject: [PATCH 14/17] Update README.md --- README.md | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index c84c95b..e4006a8 100644 --- a/README.md +++ b/README.md @@ -34,23 +34,18 @@ Getting Started with Appium tests in Java on BrowserStack couldn't be easier! - Any BrowserStack capability passed outside bstack:options will not be honoured \ [Browserstack Capability Builder](https://www.browserstack.com/app-automate/capabilities?tag=w3c) -- AppiumBy is available with java-client 8.0.0 . For java-client < 8.0.0, MobileBy can be used. +- AppiumBy is available with java-client 8.0.0 as MobileBy is depreceated . For java-client < 8.0.0, MobileBy can be used. - WebDriverWait constructor requires time to be passed as a type Duration. So with java-client 8.0.0, pass wait time as a new Duration -- java-client v-7.0.0 + **java-client v-7.0.0** ``` - WebElement searchElement = (WebElement) new WebDriverWait( - driver, - 30 - ) + WebElement searchElement = (WebElement) new WebDriverWait(driver, 30) ``` - java-client v-8.0.0 + **java-client v-8.0.0** ``` - WebElement searchElement = (WebElement) new WebDriverWait( - driver, - Duration.ofSeconds(30) - ) + import java.time.Duration; + WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30)) ``` Refer this for tracking changes in java-client 8.0.0 documentation - [v7-to-v8-migration-guide](https://github.com/appium/java-client/blob/master/docs/v7-to-v8-migration-guide.md#mobileelement) From f1cb7256aa5b81196f4c6abf021dc6ec59f3b2b7 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Wed, 16 Mar 2022 12:10:12 +0530 Subject: [PATCH 15/17] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e4006a8..034d41d 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ Getting Started with Appium tests in Java on BrowserStack couldn't be easier! - AppiumBy is available with java-client 8.0.0 as MobileBy is depreceated . For java-client < 8.0.0, MobileBy can be used. +- DefaultGenericMobileElement class has been removed completely together with its descendants (MobileElement, IOSElement, AndroidElement etc.). Use WebElement instead. + - WebDriverWait constructor requires time to be passed as a type Duration. So with java-client 8.0.0, pass wait time as a new Duration **java-client v-7.0.0** ``` From 3cf22bee30d9a87fc409d49863933d9b464619b4 Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Thu, 17 Mar 2022 09:58:48 +0530 Subject: [PATCH 16/17] Updated README for initiating a local connection --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 034d41d..5597b8a 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,15 @@ Ensure that @ symbol is prepended to the file path in the above request. Please **2. Configure and run your local test** +Local Testing is a BrowserStack feature that helps you test mobile apps that access resources hosted in development or testing environments during automated test execution + +To setup Local Testing connection - [Local testing reference](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/local-testing) + +NOTE : If you're unable to run the LocalTesting Binary due to Apple permission issues, go to \ + ``` + System preferences -> Security and privacy -> General -> Allow app + ``` + Open `BrowserStackSampleLocal.java` file in the `android` or `ios` directory : - Replace `YOUR_USERNAME` & `YOUR_ACCESS_KEY` with your BrowserStack access credentials. Get your BrowserStack access credentials from [here](https://www.browserstack.com/accounts/settings) From ecb89782d114055d23fabd4ab819489966eaeaef Mon Sep 17 00:00:00 2001 From: Abhishek Jha <21086203+innovater21@users.noreply.github.com> Date: Thu, 17 Mar 2022 13:25:20 +0530 Subject: [PATCH 17/17] Added local testing link for local testing --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 5597b8a..772c5e5 100644 --- a/README.md +++ b/README.md @@ -118,14 +118,22 @@ Ensure that @ symbol is prepended to the file path in the above request. Please Local Testing is a BrowserStack feature that helps you test mobile apps that access resources hosted in development or testing environments during automated test execution -To setup Local Testing connection - [Local testing reference](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/local-testing) +**i. Setup Browserstack Local Testing connection :** -NOTE : If you're unable to run the LocalTesting Binary due to Apple permission issues, go to \ +Check the releases page to download the binary / native application [Browserstack Local Releases](https://www.browserstack.com/docs/local-testing/releases-and-downloads) + +- Option 1 + - Use Browserstack Local Binary - [Local Binary](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/local-testing) +- Option 2 + - Use Browserstack native application - [Local Native App](https://www.browserstack.com/docs/local-testing/local-app-upgrade-guide) + + +NOTE : If you're unable to run the LocalTesting Binary / Native application due to Apple permission issues, go to \ ``` System preferences -> Security and privacy -> General -> Allow app ``` -Open `BrowserStackSampleLocal.java` file in the `android` or `ios` directory : +**ii. Open `BrowserStackSampleLocal.java` file in the `android` or `ios` directory :** - Replace `YOUR_USERNAME` & `YOUR_ACCESS_KEY` with your BrowserStack access credentials. Get your BrowserStack access credentials from [here](https://www.browserstack.com/accounts/settings)