|
| 1 | +import com.browserstack.local.Local; |
| 2 | + |
| 3 | +import java.net.URL; |
| 4 | +import java.net.MalformedURLException; |
| 5 | + |
| 6 | +import io.appium.java_client.AppiumDriver; |
| 7 | +import io.appium.java_client.android.AndroidDriver; |
| 8 | +import io.appium.java_client.MobileElement; |
| 9 | + |
1 | 10 | import org.openqa.selenium.By; |
2 | 11 | import org.openqa.selenium.WebDriver; |
3 | 12 | import org.openqa.selenium.WebElement; |
4 | | -import org.openqa.selenium.firefox.*; |
| 13 | +import org.openqa.selenium.remote.RemoteWebDriver; |
| 14 | +import org.openqa.selenium.remote.DesiredCapabilities; |
5 | 15 |
|
6 | 16 | public class LocalSample { |
| 17 | + private static Local localInstance; |
| 18 | + |
| 19 | + public static void setupLocal() throws Exception { |
| 20 | + localInstance = new Local(); |
| 21 | + Map<String, String> options = new HashMap<String, String>(); |
| 22 | + options.put("key", accessKey); |
| 23 | + localInstance.start(options); |
| 24 | + } |
| 25 | + |
| 26 | + public static void tearDownLocal() throws Exception { |
| 27 | + localInstance.stop(); |
| 28 | + } |
7 | 29 |
|
8 | 30 | public static void main(String[] args) throws Exception { |
| 31 | + setupLocal(); |
| 32 | + |
| 33 | + DesiredCapabilities capabilities = new DesiredCapabilities(); |
| 34 | + |
| 35 | + capabilities.setCapability("browserstack.local", true); |
| 36 | + capabilities.setCapability("realMobile", true); |
| 37 | + capabilities.setCapability("device", "Samsung Galaxy S6"); |
| 38 | + capabilities.setCapability("app", "bs://3fc0a1f5a158e935ad806b97288f4b24e11ebcc4"); |
| 39 | + |
| 40 | + AndroidDriver driver = new AndroidDriver(new URL("http://BROWSERSTACK_USERNAME:BROWSERSTACK_ACCESS_KEY@hub.browserstack.com/wd/hub"), capabilities); |
| 41 | + |
| 42 | + WebElement searchElement = new WebDriverWait(driver, 30).until( |
| 43 | + ExpectedConditions.elementToBeClickable(By.id("com.example.android.basicnetworking:id/test_action"))); |
| 44 | + searchElement.click(); |
| 45 | + WebElement insertTextElement = new WebDriverWait(driver, 30).until( |
| 46 | + ExpectedConditions.elementToBeClickable(By.className("android.widget.TextView"))); |
| 47 | + |
| 48 | + WebElement testElement = null; |
| 49 | + List<WebElement> allTextViewElements = driver.findElements(By.className("android.widget.TextView")); |
| 50 | + Thread.sleep(10); |
| 51 | + for(WebElement textElement : allTextViewElements) { |
| 52 | + System.out.println(textElement.getText()); |
| 53 | + if(textElement.getText().contains("The active connection is")) { |
| 54 | + testElement = textElement; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if(testElement == null) { |
| 59 | + File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); |
| 60 | + FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir") + "screenshot.png")); |
| 61 | + System.out.println("Screenshot stored at " + System.getProperty("user.dir") + "screenshot.png"); |
| 62 | + throw new Error("Cannot find the needed TextView element from app"); |
| 63 | + } |
| 64 | + String matchedString = testElement.getText(); |
| 65 | + System.out.println(matchedString); |
| 66 | + assertTrue(matchedString.contains("The active connection is wifi")); |
| 67 | + assertTrue(matchedString.contains("Up and running")); |
9 | 68 |
|
10 | | - WebDriver driver = new FirefoxDriver(); |
11 | | - driver.get("http://www.google.com/"); |
12 | | - System.out.println("Page title is: " + driver.getTitle()); |
13 | | - WebElement element = driver.findElement(By.name("q")); |
14 | | - element.sendKeys("BrowserStack"); |
15 | | - element.submit(); |
16 | | - System.out.println("And the Title is: " + driver.getTitle()); |
17 | 69 | driver.quit(); |
18 | 70 |
|
| 71 | + tearDownLocal(); |
19 | 72 | } |
20 | 73 | } |
0 commit comments