Skip to content

Commit 2f1f069

Browse files
authored
Merge pull request #18 from browserstack/AASI-812-support-java-client-8.0.0
Added support for java-client 8.0.0
2 parents 186d775 + ecb8978 commit 2f1f069

File tree

6 files changed

+401
-261
lines changed

6 files changed

+401
-261
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ mvn clean install
2929

3030
Getting Started with Appium tests in Java on BrowserStack couldn't be easier!
3131

32+
### For java-client 8.0.0 and above
33+
34+
- Any BrowserStack capability passed outside bstack:options will not be honoured \
35+
[Browserstack Capability Builder](https://www.browserstack.com/app-automate/capabilities?tag=w3c)
36+
37+
- AppiumBy is available with java-client 8.0.0 as MobileBy is depreceated . For java-client < 8.0.0, MobileBy can be used.
38+
39+
- DefaultGenericMobileElement class has been removed completely together with its descendants (MobileElement, IOSElement, AndroidElement etc.). Use WebElement instead.
40+
41+
- 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
42+
**java-client v-7.0.0**
43+
```
44+
WebElement searchElement = (WebElement) new WebDriverWait(driver, 30)
45+
```
46+
47+
**java-client v-8.0.0**
48+
```
49+
import java.time.Duration;
50+
WebElement searchElement = (WebElement) new WebDriverWait(driver, Duration.ofSeconds(30))
51+
```
52+
53+
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)
54+
3255
### Run your first test :
3356
3457
**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
93116
94117
**2. Configure and run your local test**
95118
96-
Open `BrowserStackSampleLocal.java` file in the `android` or `ios` directory :
119+
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
120+
121+
**i. Setup Browserstack Local Testing connection :**
122+
123+
Check the releases page to download the binary / native application [Browserstack Local Releases](https://www.browserstack.com/docs/local-testing/releases-and-downloads)
124+
125+
- Option 1
126+
- Use Browserstack Local Binary - [Local Binary](https://www.browserstack.com/docs/app-automate/appium/getting-started/java/local-testing)
127+
- Option 2
128+
- Use Browserstack native application - [Local Native App](https://www.browserstack.com/docs/local-testing/local-app-upgrade-guide)
129+
130+
131+
NOTE : If you're unable to run the LocalTesting Binary / Native application due to Apple permission issues, go to \
132+
```
133+
System preferences -> Security and privacy -> General -> Allow app
134+
```
135+
136+
**ii. Open `BrowserStackSampleLocal.java` file in the `android` or `ios` directory :**
97137
98138
- Replace `YOUR_USERNAME` & `YOUR_ACCESS_KEY` with your BrowserStack access credentials. Get your BrowserStack access credentials from [here](https://www.browserstack.com/accounts/settings)
99139

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<dependency>
2626
<groupId>io.appium</groupId>
2727
<artifactId>java-client</artifactId>
28-
<version>7.0.0</version>
28+
<version>8.0.0</version>
2929
</dependency>
3030
<dependency>
3131
<groupId>com.browserstack</groupId>
Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,83 @@
11
package android;
22

3+
import io.appium.java_client.AppiumBy;
4+
import io.appium.java_client.android.AndroidDriver;
5+
import java.net.MalformedURLException;
36
import java.net.URL;
7+
import java.time.Duration;
8+
import java.util.HashMap;
49
import java.util.List;
5-
import java.util.function.Function;
6-
import java.net.MalformedURLException;
7-
8-
import io.appium.java_client.MobileBy;
9-
import io.appium.java_client.android.AndroidDriver;
10-
import io.appium.java_client.android.AndroidElement;
11-
10+
import org.openqa.selenium.WebElement;
11+
import org.openqa.selenium.remote.DesiredCapabilities;
1212
import org.openqa.selenium.support.ui.ExpectedConditions;
1313
import org.openqa.selenium.support.ui.WebDriverWait;
14-
import org.openqa.selenium.WebDriver;
15-
import org.openqa.selenium.remote.DesiredCapabilities;
16-
1714

1815
public class BrowserStackSample {
1916

20-
public static void main(String[] args) throws MalformedURLException, InterruptedException {
21-
22-
DesiredCapabilities caps = new DesiredCapabilities();
23-
24-
// Set your access credentials
25-
caps.setCapability("browserstack.user", "YOUR_USERNAME");
26-
caps.setCapability("browserstack.key", "YOUR_ACCESS_KEY");
27-
28-
// Set URL of the application under test
29-
caps.setCapability("app", "bs://<app-id>");
30-
31-
// Specify device and os_version for testing
32-
caps.setCapability("device", "Google Pixel 3");
33-
caps.setCapability("os_version", "9.0");
34-
35-
// Set other BrowserStack capabilities
36-
caps.setCapability("project", "First Java Project");
37-
caps.setCapability("build", "browserstack-build-1");
38-
caps.setCapability("name", "first_test");
39-
40-
41-
// Initialise the remote Webdriver using BrowserStack remote URL
42-
// and desired capabilities defined above
43-
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
44-
new URL("http://hub.browserstack.com/wd/hub"), caps);
45-
17+
public static void main(String[] args)
18+
throws MalformedURLException, InterruptedException {
19+
DesiredCapabilities caps = new DesiredCapabilities();
20+
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
21+
22+
// Set your access credentials
23+
browserstackOptions.put("userName", "YOUR_USERNAME");
24+
browserstackOptions.put("accessKey", "YOUR_ACCESS_KEY");
25+
26+
// Set other BrowserStack capabilities
27+
browserstackOptions.put("appiumVersion", "1.22.0");
28+
browserstackOptions.put("projectName", "First Java Project");
29+
browserstackOptions.put("buildName", "browserstack-build-1");
30+
browserstackOptions.put("sessionName", "first_test");
31+
32+
// Passing browserstack capabilities inside bstack:options
33+
caps.setCapability("bstack:options", browserstackOptions);
34+
35+
// Set URL of the application under test
36+
caps.setCapability("app", "bs://<app-id>");
37+
38+
// Specify deviceName and platformName for testing
39+
caps.setCapability("deviceName", "Google Pixel 3");
40+
caps.setCapability("platformName", "android");
41+
caps.setCapability("platformVersion", "9.0");
42+
43+
// Initialise the remote Webdriver using BrowserStack remote URL
44+
// and desired capabilities defined above
45+
AndroidDriver driver = new AndroidDriver(
46+
new URL("http://hub.browserstack.com/wd/hub"),
47+
caps
48+
);
49+
50+
// Test case for the BrowserStack sample Android app.
51+
// If you have uploaded your app, update the test case here.
52+
WebElement searchElement = (WebElement) new WebDriverWait(
53+
driver,
54+
Duration.ofSeconds(30)
55+
)
56+
.until(
57+
ExpectedConditions.elementToBeClickable(
58+
AppiumBy.accessibilityId("Search Wikipedia")
59+
)
60+
);
61+
searchElement.click();
62+
63+
WebElement insertTextElement = (WebElement) new WebDriverWait(
64+
driver,
65+
Duration.ofSeconds(30)
66+
)
67+
.until(
68+
ExpectedConditions.elementToBeClickable(
69+
AppiumBy.id("org.wikipedia.alpha:id/search_src_text")
70+
)
71+
);
72+
insertTextElement.sendKeys("BrowserStack");
73+
Thread.sleep(5000);
4674

47-
// Test case for the BrowserStack sample Android app.
48-
// If you have uploaded your app, update the test case here.
49-
AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
50-
ExpectedConditions.elementToBeClickable(
51-
MobileBy.AccessibilityId("Search Wikipedia")));
52-
searchElement.click();
53-
AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
54-
ExpectedConditions.elementToBeClickable(
55-
MobileBy.id("org.wikipedia.alpha:id/search_src_text")));
56-
insertTextElement.sendKeys("BrowserStack");
57-
Thread.sleep(5000);
58-
List<AndroidElement> allProductsName = driver.findElementsByClassName(
59-
"android.widget.TextView");
60-
assert(allProductsName.size() > 0);
61-
62-
63-
// Invoke driver.quit() after the test is done to indicate that the test is completed.
64-
driver.quit();
65-
66-
}
75+
List<WebElement> allProductsName = driver.findElements(
76+
AppiumBy.className("android.widget.TextView")
77+
);
78+
assert (allProductsName.size() > 0);
6779

80+
// Invoke driver.quit() after the test is done to indicate that the test is completed.
81+
driver.quit();
82+
}
6883
}
Lines changed: 93 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,114 @@
11
package android;
22

33
import com.browserstack.local.Local;
4-
import java.net.URL; import java.util.*;
5-
import io.appium.java_client.MobileBy; import io.appium.java_client.android.*;
6-
import org.openqa.selenium.support.ui.*;import org.openqa.selenium.remote.*;
4+
import io.appium.java_client.AppiumBy;
5+
import io.appium.java_client.android.AndroidDriver;
6+
import java.net.URL;
7+
import java.time.Duration;
8+
import java.util.*;
9+
import org.openqa.selenium.WebElement;
10+
import org.openqa.selenium.remote.*;
11+
import org.openqa.selenium.support.ui.*;
712

813
public class BrowserStackSampleLocal {
9-
14+
1015
private static Local localInstance;
1116
public static String userName = "YOUR_USERNAME";
1217
public static String accessKey = "YOUR_ACCESS_KEY";
1318

14-
1519
public static void setupLocal() throws Exception {
1620
localInstance = new Local();
1721
Map<String, String> options = new HashMap<String, String>();
1822
options.put("key", accessKey);
23+
options.put("local", "true");
1924
localInstance.start(options);
2025
}
2126

2227
public static void tearDownLocal() throws Exception {
2328
localInstance.stop();
2429
}
2530

26-
public static void main(String[] args) throws Exception {
27-
// Start the BrowserStack Local binary
28-
setupLocal();
29-
30-
DesiredCapabilities capabilities = new DesiredCapabilities();
31-
32-
// Set your access credentials
33-
capabilities.setCapability("browserstack.user", userName);
34-
capabilities.setCapability("browserstack.key", accessKey);
35-
36-
// Set URL of the application under test
37-
capabilities.setCapability("app", "bs://<app-id>");
38-
39-
// Specify device and os_version for testing
40-
capabilities.setCapability("device", "Google Pixel 3");
41-
capabilities.setCapability("os_version", "9.0");
42-
43-
// Set the browserstack.local capability to true
44-
capabilities.setCapability("browserstack.local", true);
45-
46-
// Set other BrowserStack capabilities
47-
capabilities.setCapability("project", "First Java Project");
48-
capabilities.setCapability("build", "browserstack-build-1");
49-
capabilities.setCapability("name", "local_test");
50-
51-
52-
// Initialise the remote Webdriver using BrowserStack remote URL
53-
// and desired capabilities defined above
54-
AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(
55-
new URL("http://hub.browserstack.com/wd/hub"), capabilities);
56-
57-
// Test case for the BrowserStack sample Android Local app.
58-
// If you have uploaded your app, update the test case here.
59-
AndroidElement searchElement = (AndroidElement) new WebDriverWait(driver, 30).until(
60-
ExpectedConditions.elementToBeClickable(MobileBy.id("com.example.android.basicnetworking:id/test_action")));
61-
searchElement.click();
62-
AndroidElement insertTextElement = (AndroidElement) new WebDriverWait(driver, 30).until(
63-
ExpectedConditions.elementToBeClickable(MobileBy.className("android.widget.TextView")));
64-
65-
AndroidElement testElement = null;
66-
List<AndroidElement> allTextViewElements = driver.findElementsByClassName("android.widget.TextView");
67-
Thread.sleep(10);
68-
for(AndroidElement textElement : allTextViewElements) {
69-
if(textElement.getText().contains("The active connection is")) {
70-
testElement = textElement;
71-
}
72-
}
73-
74-
if(testElement == null) {
75-
throw new Error("Cannot find the needed TextView element from app");
76-
}
77-
String matchedString = testElement.getText();
78-
System.out.println(matchedString);
79-
assert(matchedString.contains("The active connection is wifi"));
80-
assert(matchedString.contains("Up and running"));
81-
82-
// Invoke driver.quit() after the test is done to indicate that the test is completed.
83-
driver.quit();
84-
85-
// Stop the BrowserStack Local binary
86-
tearDownLocal();
87-
88-
}
89-
31+
public static void main(String[] args) throws Exception {
32+
// Start the BrowserStack Local binary
33+
setupLocal();
34+
35+
DesiredCapabilities capabilities = new DesiredCapabilities();
36+
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
37+
38+
// Set your access credentials
39+
browserstackOptions.put("userName", userName);
40+
browserstackOptions.put("accessKey", accessKey);
41+
42+
// Set other BrowserStack capabilities
43+
browserstackOptions.put("appiumVersion", "1.22.0");
44+
browserstackOptions.put("projectName", "First Java Project");
45+
browserstackOptions.put("buildName", "browserstack-build-1");
46+
browserstackOptions.put("sessionName", "local_test");
47+
48+
// Set the browserstack.local capability to true
49+
browserstackOptions.put("local", "true");
50+
51+
// Passing browserstack capabilities inside bstack:options
52+
capabilities.setCapability("bstack:options", browserstackOptions);
53+
54+
// Set URL of the application under test
55+
capabilities.setCapability("app", "bs://<app-id>");
56+
57+
// Specify device and os_version for testing
58+
capabilities.setCapability("deviceName", "Google Pixel 3");
59+
capabilities.setCapability("platformName", "android");
60+
capabilities.setCapability("platformVersion", "9.0");
61+
62+
// Initialise the remote Webdriver using BrowserStack remote URL
63+
// and desired capabilities defined above
64+
AndroidDriver driver = new AndroidDriver(
65+
new URL("http://hub.browserstack.com/wd/hub"),
66+
capabilities
67+
);
68+
69+
// Test case for the BrowserStack sample Android Local app.
70+
// If you have uploaded your app, update the test case here.
71+
WebElement searchElement = new WebDriverWait(driver, Duration.ofSeconds(30))
72+
.until(
73+
ExpectedConditions.elementToBeClickable(
74+
AppiumBy.id("com.example.android.basicnetworking:id/test_action")
75+
)
76+
);
77+
searchElement.click();
78+
79+
WebElement insertTextElement = (WebElement) new WebDriverWait(
80+
driver,
81+
Duration.ofSeconds(30)
82+
)
83+
.until(
84+
ExpectedConditions.elementToBeClickable(
85+
AppiumBy.className("android.widget.TextView")
86+
)
87+
);
88+
89+
WebElement testElement = null;
90+
List<WebElement> allTextViewElements = driver.findElements(
91+
AppiumBy.className("android.widget.TextView")
92+
);
93+
Thread.sleep(10);
94+
for (WebElement textElement : allTextViewElements) {
95+
if (textElement.getText().contains("The active connection is")) {
96+
testElement = textElement;
97+
}
98+
}
99+
100+
if (testElement == null) {
101+
throw new Error("Cannot find the needed TextView element from app");
102+
}
103+
String matchedString = testElement.getText();
104+
System.out.println(matchedString);
105+
assert (matchedString.contains("The active connection is wifi"));
106+
assert (matchedString.contains("Up and running"));
107+
108+
// Invoke driver.quit() after the test is done to indicate that the test is completed.
109+
driver.quit();
110+
111+
// Stop the BrowserStack Local binary
112+
tearDownLocal();
113+
}
90114
}

0 commit comments

Comments
 (0)