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 7 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
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
115 changes: 59 additions & 56 deletions src/test/java/android/BrowserStackSample.java
Original file line number Diff line number Diff line change
@@ -1,68 +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.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.remote.RemoteWebDriver;
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();

// 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 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, 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();
}
}
154 changes: 85 additions & 69 deletions src/test/java/android/BrowserStackSampleLocal.java
Original file line number Diff line number Diff line change
@@ -1,90 +1,106 @@
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 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<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();

// 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("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<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