@Test public void findByAndroidUIAutomatorFromDriverTest() { assertNotEquals(driver .findElement(MobileBy .AndroidUIAutomator("new UiSelector().clickable(true)")).getText(), null); assertNotEquals(driver .findElements(MobileBy .AndroidUIAutomator("new UiSelector().clickable(true)")).size(), 0); assertNotEquals(driver .findElements(MobileBy .AndroidUIAutomator("new UiSelector().clickable(true)")).size(), 1); }
@Test public void scrollingToSubElement() { driver.findElementByAccessibilityId("Views").click(); AndroidElement list = driver.findElement(By.id("android:id/list")); MobileElement radioGroup = list .findElement(MobileBy .AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(" + "new UiSelector().text(\"Radio Group\"));")); assertNotNull(radioGroup.getLocation()); }
@Test public void replaceValueTest() { String originalValue = "original value"; Activity activity = new Activity("io.appium.android.apis", ".view.Controls1"); startsActivity.startActivity(activity); AndroidElement editElement = driver .findElement(MobileBy .AndroidUIAutomator("resourceId(\"io.appium.android.apis:id/edit\")")); editElement.sendKeys(originalValue); assertEquals(originalValue, editElement.getText()); String replacedValue = "replaced value"; editElement.replaceValue(replacedValue); assertEquals(replacedValue, editElement.getText()); }
public WebElement waitForElement(final By findBy, final int waitTime) { Wait<AppiumDriver> wait = new FluentWait<>((AppiumDriver) driver) .withTimeout(waitTime, TimeUnit.SECONDS) .pollingEvery(POLL_TIME, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class,StaleElementReferenceException.class); WebElement webElement = wait.until(new Function<AppiumDriver, WebElement>() { @Override public WebElement apply(AppiumDriver driver) { System.out.println("Trying to find element " + findBy.toString()); WebElement element = driver.findElement(findBy); return element; } }); return webElement; }
public void gameCenterLogin() throws Exception { if (driver.getPageSource().contains("Sign in")) { log("Google Plus sign in shown..."); log(driver.getPageSource()); takeScreenshot("google_plus_sign_in_shown"); while (driver.getPageSource().contains("Sign in")) if (automationName.equalsIgnoreCase("selendroid")) { driver.findElement(By.xpath("//LinearLayout/Button[@text='Sign in']")).click(); } else if (platform.equals(PlatformType.ANDROID)) { driver.findElement(By.xpath("//android.widget.Button[@text='Sign in']")).click(); } else { //we are on ios driver.findElementByAccessibilityId("Sign In").click(); } takeScreenshot("after_clicking_sign_in"); } }
public class IOSTester { public static void main(String[] args) throws Exception { AppiumDriver driver; DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS"); capabilities.setCapability(CapabilityType.VERSION, "8.1"); capabilities.setCapability("platformName", "iOS"); capabilities.setCapability("deviceName","iPad Air"); capabilities.setCapability("app", "/Users/AZ-Admin/Documents/test.app"); driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities); driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIAButton[1]")).click(); } }
public void cancelGameCenterLogin() throws Exception { log("Check to see if Google Plus Sign in needs to be cancelled.."); if (driver.getPageSource().contains("Choose an account")) { if (automationName.equalsIgnoreCase("selendroid")) { driver.findElement(By.xpath("//LinearLayout/Button[@text='Cancel']")).click(); } else if (platform.equals(PlatformType.ANDROID)) { driver.findElement(By.xpath("//android.widget.Button[@text='Cancel']")).click(); } else { //we are on ios driver.findElementByAccessibilityId("Cancel").click(); } } if (automationName.equalsIgnoreCase("selendroid")) { ImageRecognitionSettings settings = new ImageRecognitionSettings(); settings.setRetries(1); while (findImageOnScreen("native_sign_in_button").isFound()) { tapImageOnScreen("native_sign_in_button", 0.75, 0.5, settings); } } else { if (driver.getPageSource().contains("SIGN IN") || (driver.getPageSource().contains("Sign in")) || (driver.getPageSource().contains("Sign In"))) { log("Google Plus sign in shown..."); takeScreenshot("google_plus_sign_in_shown"); driver.findElement(By.xpath("//android.widget.Button[@text='Cancel']")).click(); takeScreenshot("after_cancel_sign_in"); driver.findElement(By.xpath("//android.widget.Button[@text='Cancel']")).click(); takeScreenshot("after_second_cancel_sign_in"); } } }
public static WebElement findElement( AppiumDriver<?> driver, UiElement uiElement ) { // switch the context if needed String context = getElementContext(uiElement); if (context != null && !context.equals(driver.getContext())) { driver.context(context); } // find the element if (uiElement.getElementProperty("xpath") != null) { return driver.findElement(By.xpath(uiElement.getElementProperty("xpath"))); } else if (uiElement.getElementProperty("id") != null) { return driver.findElement(By.id(uiElement.getElementProperty("id"))); } else if (uiElement.getElementProperty("name") != null) { return driver.findElement(By.name(uiElement.getElementProperty("name"))); } else if (uiElement.getElementProperty("text") != null) { return driver.findElement(By.linkText(uiElement.getElementProperty("text"))); } else if (uiElement.getElementProperty("partialText") != null) { return driver.findElement(By.partialLinkText(uiElement.getElementProperty("partialText"))); } return null; }
@Test void testWithAndroid(AppiumDriver<WebElement> android) { String context = android.getContext(); android.context("NATIVE_APP"); android.findElement(By.id("com.android.chrome:id/terms_accept")) .click(); android.findElement(By.id("com.android.chrome:id/negative_button")) .click(); android.context(context); android.get("https://bonigarcia.github.io/selenium-jupiter/"); assertTrue(android.getTitle().contains("JUnit 5 extension")); }
@Test void testWithAndroid(AppiumDriver<WebElement> android) { String context = android.getContext(); android.context("NATIVE_APP"); android.findElement(By.id("com.android.chrome:id/terms_accept")) .click(); android.findElement(By.id("com.android.chrome:id/negative_button")) .click(); android.context(context); android.get("https://bonigarcia.github.io/selenium-jupiter/"); assertTrue(android.getTitle().contains("JUnit 5 extension")); }
public static MobileElement findElement(By by,int waitSeconds){ //log.info(util.MyLogger.getMethodName() + " " + by.toString()); log.info(MyLogger.getMethodName()); //WebElement elemME = waitMe.until(ExpectedConditions.visibilityOfElementLocated(by)); //.ExpectedConditions.elementToBeClickable(); AppiumDriverWait wait = AppiumDriverWait.getInstance(driver,waitSeconds); AppiumFunction<AppiumDriver, WebElement> waitFunction = var1->{ WebElement elem = null; try{ elem = var1.findElement(by); }catch (Exception e){ log.error("!!!!!!!!!!!!!!!!!!!!!!Element : " + by.toString() + " is not founded! Polling again..."); } if(null != elem){ boolean display = elem.isDisplayed(); if(!display){ log.error("!!!!!!!!!!!!!!!!!!!!!!Element : " + by.toString() + " is found but not displayed!"); //elem = null; if(Util.isAndroid()) { elem = null; } }else { log.info("Element " + by.toString() + " is found."); } } return elem; }; WebElement elem = wait.until(waitFunction); return (MobileElement) elem; }
case EXACT: try { PageFactory.getMobileDriver().findElement(MobileBy .AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" + ".scrollIntoView(new UiSelector().text(\"" + text + "\").instance(0))")); case CONTAINS: try { PageFactory.getMobileDriver().findElement(MobileBy .AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" + ".scrollIntoView(new UiSelector().textContains(\"" + text + "\").instance(0))"));
public void actualTest(AppiumDriver driver) { // Set up default wait WebDriverWait wait = new WebDriverWait(driver, 10); try { driver.get("http://appiumpro.com/contact"); wait.until(ExpectedConditions.visibilityOfElementLocated(EMAIL)) .sendKeys("foo@foo.com"); driver.findElement(MESSAGE).sendKeys("Hello!"); driver.findElement(SEND).click(); String response = wait.until(ExpectedConditions.visibilityOfElementLocated(ERROR)).getText(); // validate that we get an error message involving a captcha, which we didn't fill out Assert.assertThat(response, CoreMatchers.containsString("Captcha")); } finally { driver.quit(); } } }
@Test public void testSendKeysAction() { wait.until(ExpectedConditions.presenceOfElementLocated(loginScreen)).click(); WebElement usernameField = driver.findElement(username); usernameField.click(); Actions a = new Actions(driver); a.sendKeys("foo"); a.perform(); Assert.assertEquals("foo", usernameField.getText()); }
private void automateClipboard(AppiumDriver driver) { WebDriverWait wait = new WebDriverWait(driver, 5); try { wait.until(ExpectedConditions.presenceOfElementLocated(clipboardNav)).click(); String text = "Hello World"; ((HasClipboard) driver).setClipboardText(text); wait.until(ExpectedConditions.presenceOfElementLocated(refreshClipboardBtn)).click(); By clipboardText = MobileBy.AccessibilityId(text); Assert.assertEquals(driver.findElement(clipboardText).getText(), text); text = "Hello World Again"; driver.findElement(clipboardInput).sendKeys(text); try { driver.hideKeyboard(); } catch (Exception ign) {} driver.findElement(setTextBtn).click(); Assert.assertEquals(((HasClipboard) driver).getClipboardText(), text); } finally { driver.quit(); } } }
@Test public void testLowLevelKeys() { wait.until(ExpectedConditions.presenceOfElementLocated(loginScreen)).click(); WebElement usernameField = driver.findElement(username); usernameField.click(); KeyInput keyboard = new KeyInput("keyboard"); Sequence sendKeys = new Sequence(keyboard, 0); sendKeys.addAction(keyboard.createKeyDown(Keys.SHIFT.getCodePoint())); sendKeys.addAction(keyboard.createKeyDown("f".codePointAt(0))); sendKeys.addAction(keyboard.createKeyUp("f".codePointAt(0))); sendKeys.addAction(keyboard.createKeyUp(Keys.SHIFT.getCodePoint())); sendKeys.addAction(keyboard.createKeyDown("o".codePointAt(0))); sendKeys.addAction(keyboard.createKeyUp("o".codePointAt(0))); sendKeys.addAction(keyboard.createKeyDown("o".codePointAt(0))); sendKeys.addAction(keyboard.createKeyUp("o".codePointAt(0))); driver.perform(Arrays.asList(sendKeys)); Assert.assertEquals("Foo", usernameField.getText()); }
WebElement navigate = driver.findElement(navigateBtn); navigate.click(); driver.findElement(clearBtn).click(); input.sendKeys("https://appiumpro.com"); navigate.click();
@Test public void testPushNotifications() throws Exception { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setCapability("platformName", "iOS"); capabilities.setCapability("platformVersion", "11.2"); capabilities.setCapability("deviceName", "iPhone 6s"); capabilities.setCapability("app", APP); capabilities.setCapability("udid", "auto"); capabilities.setCapability("xcodeOrgId", XCODE_ORG_ID); capabilities.setCapability("xcodeSigningId", "iPhone Developer"); capabilities.setCapability("updatedWDABundleId", WDA_BUNDLE_ID); driver = new IOSDriver(new URL("http://localhost:4723/wd/hub"), capabilities); try { // get screen size so we can swipe correctly screenSize = driver.manage().window().getSize(); // close app and wait for push notification to arrive driver.terminateApp(BUNDLE_ID); // here is where your code should trigger the push notification and wait a bit // now pull down the notification shade, check for the message we're expecting, and // then close the shade again ('TWITTER' is just an example of an expected message) showNotifications(); driver.findElement(By.xpath("//XCUIElementTypeCell[contains(@label, 'TWITTER')]")); hideNotifications(); // finally, we can reactivate our app in order to verify that the message is present, // or whatever we need to do driver.activateApp(BUNDLE_ID); } finally { driver.quit(); } }