Skip to content

Added support for java-client 8.0.0 #18

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

Merged
merged 17 commits into from
Mar 17, 2022
Merged
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
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion 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>7.0.0</version>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>com.browserstack</groupId>
Expand Down
127 changes: 71 additions & 56 deletions src/test/java/android/BrowserStackSample.java
Original file line number Diff line number Diff line change
@@ -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://<app-id>");

// 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<AndroidElement> driver = new AndroidDriver<AndroidElement>(
new URL("http://hub.browserstack.com/wd/hub"), caps);

public static void main(String[] args)
throws MalformedURLException, InterruptedException {
DesiredCapabilities caps = new DesiredCapabilities();
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();

// 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://<app-id>");

// 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<AndroidElement> 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<WebElement> 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();
}
}
162 changes: 93 additions & 69 deletions src/test/java/android/BrowserStackSampleLocal.java
Original file line number Diff line number Diff line change
@@ -1,90 +1,114 @@
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<String, String> options = new HashMap<String, String>();
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", "bs://<app-id>");

// 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<AndroidElement> driver = new AndroidDriver<AndroidElement>(
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<AndroidElement> 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<String, Object> browserstackOptions = new HashMap<String, Object>();

// 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://<app-id>");

// 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<WebElement> 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();
}
}
Loading