diff --git a/README.md b/README.md
index c8321a1..772c5e5 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,29 @@ mvn clean install
Getting Started with Appium tests in Java on BrowserStack couldn't be easier!
+### For java-client 8.0.0 and above
+
+- 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 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**
+ ```
+ WebElement searchElement = (WebElement) new WebDriverWait(driver, 30)
+ ```
+
+ **java-client v-8.0.0**
+ ```
+ 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)
+
### Run your first test :
**1. Upload your Android or iOS App**
@@ -93,7 +116,24 @@ Ensure that @ symbol is prepended to the file path in the above request. Please
**2. Configure and run your local test**
-Open `BrowserStackSampleLocal.java` file in the `android` or `ios` directory :
+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
+
+**i. Setup Browserstack Local Testing connection :**
+
+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
+ ```
+
+**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)
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
diff --git a/src/test/java/android/BrowserStackSample.java b/src/test/java/android/BrowserStackSample.java
index 136ee81..c923568 100644
--- a/src/test/java/android/BrowserStackSample.java
+++ b/src/test/java/android/BrowserStackSample.java
@@ -1,68 +1,83 @@
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 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.WebElement;
+import org.openqa.selenium.remote.DesiredCapabilities;
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;
-
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("device", "Google Pixel 3");
- caps.setCapability("os_version", "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
- AndroidDriver driver = new AndroidDriver(
- new URL("http://hub.browserstack.com/wd/hub"), caps);
-
+ 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");
+
+ // 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 capabilities 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");
+
+ // 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"),
+ 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,
+ Duration.ofSeconds(30)
+ )
+ .until(
+ ExpectedConditions.elementToBeClickable(
+ AppiumBy.accessibilityId("Search Wikipedia")
+ )
+ );
+ searchElement.click();
+
+ 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);
- // 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(
- ExpectedConditions.elementToBeClickable(
- MobileBy.AccessibilityId("Search Wikipedia")));
- searchElement.click();
- AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
- ExpectedConditions.elementToBeClickable(
- MobileBy.id("org.wikipedia.alpha:id/search_src_text")));
- insertTextElement.sendKeys("BrowserStack");
- Thread.sleep(5000);
- List allProductsName = driver.findElementsByClassName(
- "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/android/BrowserStackSampleLocal.java b/src/test/java/android/BrowserStackSampleLocal.java
index 0854db1..e765543 100644
--- a/src/test/java/android/BrowserStackSampleLocal.java
+++ b/src/test/java/android/BrowserStackSampleLocal.java
@@ -1,21 +1,26 @@
package android;
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.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.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);
}
@@ -23,68 +28,87 @@ 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
- AndroidDriver driver = new AndroidDriver(
- 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")));
- searchElement.click();
- AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
- ExpectedConditions.elementToBeClickable(MobileBy.className("android.widget.TextView")));
-
- AndroidElement testElement = null;
- List allTextViewElements = driver.findElementsByClassName("android.widget.TextView");
- Thread.sleep(10);
- for(AndroidElement 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();
+ HashMap browserstackOptions = new HashMap();
+
+ // Set your access credentials
+ 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://");
+
+ // Specify device and os_version for testing
+ capabilities.setCapability("deviceName", "Google Pixel 3");
+ capabilities.setCapability("platformName", "android");
+ capabilities.setCapability("platformVersion", "9.0");
+
+ // 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
+ );
+
+ // 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();
+ }
}
diff --git a/src/test/java/ios/BrowserStackSample.java b/src/test/java/ios/BrowserStackSample.java
index 6fd9eea..3b5f864 100644
--- a/src/test/java/ios/BrowserStackSample.java
+++ b/src/test/java/ios/BrowserStackSample.java
@@ -1,64 +1,91 @@
package ios;
-import java.net.URL;
-import java.util.List;
+import io.appium.java_client.AppiumBy;
+import io.appium.java_client.ios.IOSDriver;
import java.net.MalformedURLException;
-
-import org.openqa.selenium.support.ui.WebDriverWait;
+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;
-
-import io.appium.java_client.MobileBy;
-import io.appium.java_client.ios.IOSDriver;
-import io.appium.java_client.ios.IOSElement;
+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("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
- IOSDriver driver = new IOSDriver(
- new URL("http://hub-cloud.browserstack.com/wd/hub"), caps);
-
+ 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");
+
+ // 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 capabilities inside bstack:options
+ caps.setCapability("bstack:options", browserstackOptions);
+
+ // 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");
+
+ // Initialise the remote Webdriver using BrowserStack remote URL
+ // and desired capabilities defined above
+ IOSDriver driver = new IOSDriver(
+ new URL("http://hub.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();
- // 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")));
- textButton.click();
- IOSElement textInput = (IOSElement) new WebDriverWait(driver, 30).until(
- ExpectedConditions.elementToBeClickable(MobileBy.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")));
- 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();
+ 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,
+ 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();
+ }
}
diff --git a/src/test/java/ios/BrowserStackSampleLocal.java b/src/test/java/ios/BrowserStackSampleLocal.java
index f278a07..5ebf5a0 100644
--- a/src/test/java/ios/BrowserStackSampleLocal.java
+++ b/src/test/java/ios/BrowserStackSampleLocal.java
@@ -1,95 +1,129 @@
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 io.appium.java_client.ios.IOSDriver;
+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.MobileBy; 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.WebDriver;
+import org.openqa.selenium.WebElement;
+import org.openqa.selenium.remote.*;
+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();
- }
-
- 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
- IOSDriver driver = new IOSDriver(
- 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")));
- 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");
- return result != null && result.length() > 0;
- }
- });
- IOSElement resultElement = (IOSElement) driver.findElement(MobileBy.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");
+ 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();
+ HashMap browserstackOptions = new HashMap();
+
+ // Set your access credentials
+ 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://");
+
+ // Specify device and os_version for testing
+ capabilities.setCapability("deviceName", "iPhone 11 Pro");
+ capabilities.setCapability("platformName", "ios");
+ capabilities.setCapability("platformVersion", "13");
+
+ // Initialise the remote Webdriver using BrowserStack remote URL
+ // and desired capabilities defined above
+ IOSDriver driver = new IOSDriver(
+ 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();
+ }
}