Skip to content

use options class instead deprecated DesiredCapabilities #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 33 additions & 51 deletions java_8/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
| [Using Java 7 client with MJSONWP protocol](../java_7) | Using Java 8 client with w3c protocol |
|------------------------------------------------------- | ------------------------------------- |
| ------------------------------------------------------ | ------------------------------------- |

# java-appium-app-browserstack

Expand All @@ -12,13 +12,13 @@ This repository demonstrates how to run Appium Java tests on BrowserStack App Au
1. Java 8+ (JDK)

- If Java is not installed, follow these instructions:
- For Windows, download latest java version from [here](https://java.com/en/download/) and run the installer executable
- For Mac and Linux, run `java -version` to see what java version is pre-installed. If you want a different version download from [here](https://java.com/en/download/)
- For Windows, download latest java version from [here](https://java.com/en/download/) and run the installer executable
- For Mac and Linux, run `java -version` to see what java version is pre-installed. If you want a different version download from [here](https://java.com/en/download/)

2. Maven

- If Maven is not downloaded, download it from [here](https://maven.apache.org/download.cgi)
- For installation, follow the instructions [here](https://maven.apache.org/install.html)
- If Maven is not downloaded, download it from [here](https://maven.apache.org/download.cgi)
- For installation, follow the instructions [here](https://maven.apache.org/install.html)

### Install the dependencies

Expand All @@ -35,24 +35,22 @@ 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)
[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))
```

**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 :
Expand Down Expand Up @@ -85,17 +83,17 @@ Open `BrowserStackSample.java` file in the `android` directory or `ios` director

- To run the test, use the following command in the base directory :

- For Android test, run
- For Android test, run

```cmd
mvn test -P android-first-test
```
```cmd
mvn test -P android-first-test
```

- For iOS test, run
- For iOS test, run

```cmd
mvn test -P ios-first-test
```
```cmd
mvn test -P ios-first-test
```

- You can access the test execution results, and debugging information such as video recording, network logs on [App Automate dashboard](https://app-automate.browserstack.com/dashboard)

Expand All @@ -121,50 +119,34 @@ 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

**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 :**
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)

- Replace `bs://<app-id>` with the URL obtained from app upload step

- Set the device and OS version

- Ensure that `browserstack.local` capability is set to `true`. Within the test script, there is code snippet that automatically establishes Local Testing connection to BrowserStack servers using Java binding for BrowserStack Local.
- Ensure that `local` capability is set to `true`. Within the test script, there is code snippet that automatically establishes Local Testing connection to BrowserStack servers using Java binding for BrowserStack Local.

- If you have uploaded your own app update the test case

- To run the test, use the following command in the base directory :

- For Android test, run
- For Android test, run

```cmd
mvn test -P android-local-test
```
```cmd
mvn test -P android-local-test
```

- For iOS test, run
- For iOS test, run

```cmd
mvn test -P ios-local-test
```
```cmd
mvn test -P ios-local-test
```

- You can access the test execution results, and debugging information such as video recording, network logs on [App Automate dashboard](https://app-automate.browserstack.com/dashboard)


## Integration with other Java frameworks

- [JUnit](https://github.com/browserstack/junit-appium-app-browserstack)
Expand Down
2 changes: 1 addition & 1 deletion java_8/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.0.0</version>
<version>8.1.1</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
Expand Down
55 changes: 24 additions & 31 deletions java_8/src/test/java/android/BrowserStackSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
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.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();
throws MalformedURLException, InterruptedException {
UiAutomator2Options options = new UiAutomator2Options();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use Mutuable Caps?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been discussed with product team and @francisf and we decided to use Options.

Mutuable caps are not needed here, for android and ios separate folders are present because a single script cannot be used for both ios and android automation.

HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();

// Set your access credentials
Expand All @@ -27,57 +27,50 @@ public static void main(String[] args)
browserstackOptions.put("appiumVersion", "1.22.0");
browserstackOptions.put("projectName", "First Java Project");
browserstackOptions.put("buildName", "browserstack-build-1");
browserstackOptions.put("sessionName", "first_test");
browserstackOptions.put("sessionName", "BStack first_test");

// Passing browserstack capabilities inside bstack:options
caps.setCapability("bstack:options", browserstackOptions);
options.setCapability("bstack:options", browserstackOptions);

// Set URL of the application under test
caps.setCapability("app", "bs://<app-id>");
options.setCapability("app", "bs://<app-id>");

// Specify deviceName and platformName for testing
caps.setCapability("deviceName", "Google Pixel 3");
caps.setCapability("platformName", "android");
caps.setCapability("platformVersion", "9.0");
options.setCapability("deviceName", "Google Pixel 3");
options.setCapability("platformName", "android");
options.setCapability("platformVersion", "9.0");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is non-w3c compliant and it must go inside bStackOptions ?
https://www.w3.org/TR/webdriver1/#capabilities

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also app cap is not complaint.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Appium these caps are general capabilities and not specific to Browserstack, please ref here


// 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
);
new URL("http://hub.browserstack.com/wd/hub"),
options);

// 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")
)
);
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")
)
);
driver,
Duration.ofSeconds(30))
.until(
ExpectedConditions.elementToBeClickable(
AppiumBy.id("org.wikipedia.alpha:id/search_src_text")));
insertTextElement.sendKeys("BrowserStack");
Thread.sleep(5000);

List<WebElement> allProductsName = driver.findElements(
AppiumBy.className("android.widget.TextView")
);
AppiumBy.className("android.widget.TextView"));
assert (allProductsName.size() > 0);

// Invoke driver.quit() after the test is done to indicate that the test is completed.
// Invoke driver.quit() after the test is done to indicate that the test is
// completed.
driver.quit();
}
}
50 changes: 22 additions & 28 deletions java_8/src/test/java/android/BrowserStackSampleLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.browserstack.local.Local;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
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 {
Expand All @@ -18,7 +18,7 @@ public class BrowserStackSampleLocal {

public static void setupLocal() throws Exception {
localInstance = new Local();
Map<String, String> options = new HashMap<String, String>();
HashMap<String, String> options = new HashMap<String, String>();
options.put("key", accessKey);
options.put("local", "true");
localInstance.start(options);
Expand All @@ -32,7 +32,7 @@ public static void main(String[] args) throws Exception {
// Start the BrowserStack Local binary
setupLocal();

DesiredCapabilities capabilities = new DesiredCapabilities();
UiAutomator2Options options = new UiAutomator2Options();
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();

// Set your access credentials
Expand All @@ -43,53 +43,46 @@ public static void main(String[] args) throws Exception {
browserstackOptions.put("appiumVersion", "1.22.0");
browserstackOptions.put("projectName", "First Java Project");
browserstackOptions.put("buildName", "browserstack-build-1");
browserstackOptions.put("sessionName", "local_test");
browserstackOptions.put("sessionName", "BStack local_test");

// Set the browserstack.local capability to true
browserstackOptions.put("local", "true");

// Passing browserstack capabilities inside bstack:options
capabilities.setCapability("bstack:options", browserstackOptions);
options.setCapability("bstack:options", browserstackOptions);

// Set URL of the application under test
capabilities.setCapability("app", "bs://<app-id>");
options.setCapability("app", "bs://<app-id>");

// Specify device and os_version for testing
capabilities.setCapability("deviceName", "Google Pixel 3");
capabilities.setCapability("platformName", "android");
capabilities.setCapability("platformVersion", "9.0");
options.setCapability("deviceName", "Google Pixel 3");
options.setCapability("platformName", "android");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Above comment applies here too.

options.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
);
new URL("http://hub.browserstack.com/wd/hub"),
options);

// 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")
)
);
.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")
)
);
driver,
Duration.ofSeconds(30))
.until(
ExpectedConditions.elementToBeClickable(
AppiumBy.className("android.widget.TextView")));

WebElement testElement = null;
List<WebElement> allTextViewElements = driver.findElements(
AppiumBy.className("android.widget.TextView")
);
AppiumBy.className("android.widget.TextView"));
Thread.sleep(10);
for (WebElement textElement : allTextViewElements) {
if (textElement.getText().contains("The active connection is")) {
Expand All @@ -105,7 +98,8 @@ public static void main(String[] args) throws Exception {
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.
// Invoke driver.quit() after the test is done to indicate that the test is
// completed.
driver.quit();

// Stop the BrowserStack Local binary
Expand Down
Loading