Best io.appium code snippet using io.appium.java_client.MobileBy.ByAccessibilityId
AppiumHelper.java
Source:AppiumHelper.java
1package com.appium.framework.helper;2import com.appium.framework.utility.PropertiesUtility;3import io.appium.java_client.AppiumDriver;4import io.appium.java_client.MobileBy;5import io.appium.java_client.MobileElement;6import org.openqa.selenium.By;7import org.openqa.selenium.NoSuchElementException;8import org.openqa.selenium.support.ui.WebDriverWait;9import java.util.List;10public class AppiumHelper {11 protected AppiumDriver<MobileElement> driver;12 protected WebDriverWait wait;13 public AppiumHelper(AppiumDriver<MobileElement> _driver) {14 this.driver = _driver;15 wait = new WebDriverWait(_driver, 30);16 }17 public By findBy(PropertiesUtility propUtilObj, String locatorKey) {18 try {19 String locatedBy = propUtilObj.getLocatorType(locatorKey);20 String locatedValue = propUtilObj.getLocatorValue(locatorKey);21 if(locatedBy != null && locatedValue != null) {22 switch (locatedBy.toUpperCase()) {23 case "ACCESSIBILITYID":24 return MobileBy.ByAccessibilityId.AccessibilityId(locatedValue);25 case "CLASSNAME":26 return MobileBy.ByClassName.className(locatedValue);27 case "ID":28 return MobileBy.ById.id(locatedValue);29 case "NAME":30 return MobileBy.ByName.name(locatedValue);31 case "XPATH":32 return MobileBy.ByXPath.xpath(locatedValue);33 case "LINKTEXT":34 return MobileBy.ByLinkText.linkText(locatedValue);35 case "PARTIALLINKTEXT":36 return MobileBy.ByPartialLinkText.partialLinkText(locatedValue);37 }38 }39 } catch (NoSuchElementException ex) {40 System.out.println("Exception at <AppiumHelper>.<findBy> for Locator Key : " + locatorKey);41 }42 return null;43 }44 public MobileElement findElementBy(PropertiesUtility propUtilObj, String locatorKey) {45 try {46 String locatedBy = propUtilObj.getLocatorType(locatorKey);47 String locatedValue = propUtilObj.getLocatorValue(locatorKey);48 if(locatedBy != null && locatedValue != null) {49 switch (locatedBy.toUpperCase()) {50 case "ACCESSIBILITYID":51 return driver.findElementByAccessibilityId(locatedValue);52 case "CLASSNAME":53 return driver.findElementByClassName(locatedValue);54 case "ID":55 return driver.findElementById(locatedValue);56 case "NAME":57 return driver.findElementByName(locatedValue);58 case "XPATH":59 return driver.findElementByXPath(locatedValue);60 case "IMAGE":61 return driver.findElementByImage(locatedValue);62 case "LINKTEXT":63 return driver.findElementByLinkText(locatedValue);64 case "PARTIALLINKTEXT":65 return driver.findElementByPartialLinkText(locatedValue);66 }67 }68 } catch (NoSuchElementException ex) {69 System.out.println("Exception at <AppiumHelper>.<findElementBy> for Locator Key : " + locatorKey);70 }71 return null;72 }73 public List<MobileElement> findElementsBy(PropertiesUtility propUtilObj, String locatorKey) {74 try {75 String locatedBy = propUtilObj.getLocatorType(locatorKey);76 String locatedValue = propUtilObj.getLocatorValue(locatorKey);77 if(locatedBy != null && locatedValue != null) {78 switch (locatedBy.toUpperCase()) {79 case "ACCESSIBILITYID":80 return driver.findElementsByAccessibilityId(locatedValue);81 case "CLASSNAME":82 return driver.findElementsByClassName(locatedValue);83 case "ID":84 return driver.findElementsById(locatedValue);85 case "NAME":86 return driver.findElementsByName(locatedValue);87 case "XPATH":88 return driver.findElementsByXPath(locatedValue);89 case "IMAGE":90 return driver.findElementsByImage(locatedValue);91 case "LINKTEXT":92 return driver.findElementsByLinkText(locatedValue);93 case "PARTIALLINKTEXT":94 return driver.findElementsByPartialLinkText(locatedValue);95 }96 }97 } catch (NoSuchElementException ex) {98 System.out.println("Exception at <AppiumHelper>.<findElementsBy> for Locator Key : " + locatorKey);99 }100 return null;101 }102 public boolean isElementPresent(MobileBy by) {103 try {104 driver.findElement(by);105 return true;106 } catch (Exception ex) {107 return false;108 }109 }110 public boolean isElementPresent(By by) {111 try {112 driver.findElement(by);113 return true;114 } catch (Exception ex) {115 return false;116 }117 }118 public void sleep (long time) {119 try {120 Thread.sleep(time);121 } catch (InterruptedException e) {122 e.printStackTrace();123 }124 }125 public void hideKeyboard() {126 driver.hideKeyboard();127 }128 public void navigateBack() {129 driver.navigate().back();130 }131 public void navigateForward() {132 driver.navigate().forward();133 }134 public String getText(MobileElement element) {135 return element.getText();136 }137 public String getAtrribute(MobileElement element, String attributeName) {138 return element.getAttribute(attributeName);139 }140 public boolean isDisplayed(MobileElement element) {141 return element.isDisplayed();142 }143 public boolean isEnabled(MobileElement element) {144 return element.isEnabled();145 }146}...
AndroidLocator.java
Source:AndroidLocator.java
1package config.locators;2import config.DriverCreator;3import cucumber.api.Scenario;4import io.appium.java_client.MobileBy;5import io.appium.java_client.MobileElement;6import io.appium.java_client.android.AndroidDriver;7import io.appium.java_client.android.AndroidElement;8import io.appium.java_client.android.AndroidKeyCode;9import org.apache.commons.io.FileUtils;10import org.openqa.selenium.*;11import org.openqa.selenium.support.ui.ExpectedConditions;12import org.openqa.selenium.support.ui.FluentWait;13import org.openqa.selenium.support.ui.Wait;14/*import io.appium.java_client.android.nativekey.AndroidKey;15import io.appium.java_client.android.nativekey.KeyEvent;*/16import java.io.File;17import java.io.IOException;18import java.sql.Driver;19import java.text.DateFormat;20import java.text.SimpleDateFormat;21import java.util.Date;22import java.util.List;23import java.util.concurrent.TimeUnit;24import java.util.function.Function;25public class AndroidLocator implements LocatorInterface{26 public static AndroidDriver<AndroidElement> driver = null;27 public AndroidLocator(){28 driver=DriverCreator.androidDriver;29 }30 @Override31 public MobileElement getLocator(final String strategy, final String element){32 AndroidElement e=null;33 MobileBy mobileBy=null;34 Wait wait = new FluentWait(driver)35 .withTimeout(30, TimeUnit.SECONDS)36 .pollingEvery(2, TimeUnit.SECONDS)37 .ignoring(NoSuchElementException.class);38 switch (strategy) {39 case "id":40 e = (AndroidElement) wait.until(new Function<AndroidDriver, AndroidElement>() {41 public AndroidElement apply(AndroidDriver driver) {42 driver = DriverCreator.androidDriver;43 return (AndroidElement) driver.findElement(new MobileBy.ByAndroidUIAutomator("new UiSelector().resourceId(\""+element+"\")"));44 }45 });46 wait.until(ExpectedConditions.elementToBeClickable(new MobileBy.ByAndroidUIAutomator("new UiSelector().resourceId(\""+element+"\")")));47 break;48 case "accessibility":49 e = (AndroidElement) wait.until(new Function<AndroidDriver, AndroidElement>() {50 public AndroidElement apply(AndroidDriver driver) {51 driver = DriverCreator.androidDriver;52 return (AndroidElement) driver.findElement(new MobileBy.ByAccessibilityId(element));53 }54 });55 wait.until(ExpectedConditions.elementToBeClickable(new MobileBy.ByAccessibilityId(element)));56 break;57 case ("descriptionStartsWith"):58 e= initDriver(this.driver, new MobileBy.ByAndroidUIAutomator59 ("descriptionStartsWith(\""+element+"\")"));60 break;61 case ("textStartsWith"):62 e= initDriver(this.driver, new MobileBy.ByAndroidUIAutomator63 ("descriptionStartsWith(\""+element+"\")"));64 break;65 default:66 //we will pass the element name as a filename for sreenshot67 //TODO: set the scenario name and element name as the SS name68 takeScreenshot(element.split("/")[1]);69 throw new IllegalArgumentException("check type of the identifier");70 }71 return e;72 }73 @Override74 public List getLocators(final String strategy, final String element){75 List<AndroidElement> e=null;76 switch (strategy) {77 case "id":78 //e= (List<AndroidElement>) driver.findElements(MobileBy.AndroidUIAutomator(element));79 e= (List<AndroidElement>) driver.findElements(new MobileBy.ByAndroidUIAutomator("new UiSelector().resourceId(\""+element+"\")"));80 break;81 default:82 throw new IllegalArgumentException("check type of the identifier");83 }84 return e;85 }86 public void sendKeysToKeyBoard(int keyCode) {87 driver.pressKeyCode(keyCode);88 //driver.pressKey(new KeyEvent(keyCode));89 }90 public AndroidElement initDriver(AndroidDriver driver, final MobileBy byType){91 Wait wait = new FluentWait(driver)92 .withTimeout(30, TimeUnit.SECONDS)93 .pollingEvery(2, TimeUnit.SECONDS)94 .ignoring(NoSuchElementException.class);95 AndroidElement element = (AndroidElement) wait.until(new Function<AndroidDriver, AndroidElement>() {96 public AndroidElement apply(AndroidDriver driver) {97 driver = DriverCreator.androidDriver;98 return (AndroidElement) driver.findElement(byType);99 }100 });101 wait.until(ExpectedConditions.elementToBeClickable(byType));102 return element;103 }104 public AndroidElement initDriverBy(AndroidDriver driver, By byType){105 Wait wait = new FluentWait(driver)106 .withTimeout(10, TimeUnit.SECONDS)107 .pollingEvery(1, TimeUnit.SECONDS)108 .ignoring(NoSuchElementException.class);109 AndroidElement element = (AndroidElement) wait.until(new Function<AndroidDriver, AndroidElement>() {110 public AndroidElement apply(AndroidDriver driver) {111 driver = DriverCreator.androidDriver;112 return (AndroidElement) driver.findElement(byType);113 }114 });115 wait.until(ExpectedConditions.elementToBeClickable(byType));116 return element;117 }118 public void takeScreenshot(String tcName){119 //Create Folder for SS120 new File("TestArtifacts").mkdir();121 //Output type122 File f=((TakesScreenshot)this.driver).getScreenshotAs(OutputType.FILE);123 //Set filename124 String filename=tcName+".png";125 try {126 FileUtils.copyFile(f, new File("TestArtifacts" + "/" + filename));127 } catch (IOException e) {128 e.printStackTrace();129 }130 }131 //this is primarly implement for IOS and not for Android.132 public void switchToStub(){}133}...
IOSLocator.java
Source:IOSLocator.java
1package config.locators;2import config.DriverCreator;3import io.appium.java_client.MobileBy;4import io.appium.java_client.MobileElement;5import io.appium.java_client.TouchAction;6import io.appium.java_client.ios.IOSDriver;7import io.appium.java_client.ios.IOSElement;8import org.apache.commons.io.FileUtils;9import org.openqa.selenium.By;10import org.openqa.selenium.NoSuchElementException;11import org.openqa.selenium.OutputType;12import org.openqa.selenium.TakesScreenshot;13import org.openqa.selenium.interactions.Keyboard;14import org.openqa.selenium.support.ui.ExpectedConditions;15import org.openqa.selenium.support.ui.FluentWait;16import org.openqa.selenium.support.ui.Wait;17import java.io.File;18import java.io.IOException;19import java.util.List;20import java.util.concurrent.TimeUnit;21import java.util.function.Function;22public class IOSLocator implements LocatorInterface {23 public static IOSDriver<IOSElement> driver = null;24 public IOSLocator(){25 driver=DriverCreator.iosDriver;26 }27 @Override28 public MobileElement getLocator(final String strategy, final String element){29 IOSElement e=null;30 MobileBy mobileBy=null;31 switch (strategy) {32 case "accessibility":33 e= initDriver(driver, new MobileBy.ByAccessibilityId(element));34 break;35 case "nspredicate":36 e= initDriver(driver, new MobileBy.ByAccessibilityId(element));37 break;38 case "xpath":39 e=driver.findElementByXPath("//XCUIElementTypeKey[@name=\'"+element+"\']");40 break;41 default:42 //we will pass the element name as a filename for sreenshot43 //TODO: set the scenario name and element name as the SS name44 takeScreenshot(element.split("/")[1]);45 throw new IllegalArgumentException("check type of the identifier");46 }47 return e;48 }49 @Override50 public List getLocators(final String strategy, final String element){51 IOSElement e=null;52 switch (strategy) {53 // hard coding for54 case "nestedpredicate":55 e=(IOSElement)driver.findElement(MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeTabBar'")).56 findElements(MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton'"));57 break;58 default:59 throw new IllegalArgumentException("check type of the identifier");60 }61 List<IOSElement> list=null;62 list.add(e);63 return list;64 }65 public void sendKeysToKeyBoard(String keyCode) {66 Keyboard k=driver.getKeyboard();67 k.pressKey(keyCode);68 k.releaseKey(keyCode);69 }70 public void sendKeysToKeyBoard(MobileElement mobileElement) {71 TouchAction touchAction = new TouchAction(driver);72 touchAction.tap(mobileElement).perform();73 }74 public IOSElement initDriver(IOSDriver driver, final MobileBy byType){75 Wait wait = new FluentWait(driver)76 .withTimeout(30, TimeUnit.SECONDS)77 .pollingEvery(2, TimeUnit.SECONDS)78 .ignoring(NoSuchElementException.class);79 IOSElement element = (IOSElement) wait.until(new Function<IOSDriver, IOSElement>() {80 public IOSElement apply(IOSDriver driver) {81 driver = DriverCreator.iosDriver;82 return (IOSElement) driver.findElement(byType);83 }84 });85 wait.until(ExpectedConditions.elementToBeClickable(byType));86 return element;87 }88 public IOSElement initDriverBy(IOSDriver driver, By byType){89 Wait wait = new FluentWait(driver)90 .withTimeout(10, TimeUnit.SECONDS)91 .pollingEvery(1, TimeUnit.SECONDS)92 .ignoring(NoSuchElementException.class);93 IOSElement element = (IOSElement) wait.until(new Function<IOSDriver, IOSElement>() {94 public IOSElement apply(IOSDriver driver) {95 driver = DriverCreator.iosDriver;96 return (IOSElement) driver.findElement(byType);97 }98 });99 wait.until(ExpectedConditions.elementToBeClickable(byType));100 return element;101 }102 public void takeScreenshot(String tcName){103 //Create Folder for SS104 new File("TestArtifacts").mkdir();105 //Output type106 File f=((TakesScreenshot)this.driver).getScreenshotAs(OutputType.FILE);107 //Set filename108 String filename=tcName+".png";109 try {110 FileUtils.copyFile(f, new File("TestArtifacts" + "/" + filename));111 } catch (IOException e) {112 e.printStackTrace();113 }114 }115 public void switchToFrame(String type) {116 switch (type){117 case ("alert.Accept"):118 driver.switchTo().alert().accept();119 break;120 case ("alert.Dismiss"):121 driver.switchTo().alert().dismiss();122 break;123 default:124 throw new IllegalArgumentException("Not supported");125 }126 }127 //as opposed to Android, IOS starts in UAT env and so we need to switch to STUB128 public void switchToStub(){129 driver.shake();130 initDriver(driver, new MobileBy.ByAccessibilityId("App settings")).click();131 initDriver(driver, new MobileBy.ByAccessibilityId("UAT")).click();132 initDriver(driver, new MobileBy.ByAccessibilityId("Stubs")).click();133 initDriver(driver, new MobileBy.ByAccessibilityId("Done")).click();;134 }135 public void getDateandTimeWheel(){136 driver.findElementByXPath(("//XCUIElementTypePickerWheel[@type='XCUIElementTypePickerWheel']")).click();137 }138}...
FormHelper.java
Source:FormHelper.java
1package appmanager;23import io.appium.java_client.*;4import io.appium.java_client.android.AndroidDriver;5import org.hamcrest.CoreMatchers;6import org.hamcrest.MatcherAssert;7import org.junit.Assert;8import org.openqa.selenium.NoSuchElementException;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.support.ui.ExpectedConditions;12import org.openqa.selenium.support.ui.FluentWait;1314import java.util.concurrent.TimeUnit;1516import static org.hamcrest.CoreMatchers.containsString;17import static org.hamcrest.CoreMatchers.notNullValue;18import static org.hamcrest.MatcherAssert.assertThat;1920public class FormHelper extends BaseHelpers21{2223 public FormHelper(AppiumDriver driver) {24 super(driver);25 }26/** public void selectByValue (String value){27 new Select(driver.findElement(By.id("de.aureum.card.dev:id/design_bottom_sheet"))).selectByVisibleText(value);28 }*/2930 public void enterName(String name){31 //fillInTheField(By.id("de.aureum.card.dev:id/name"), name);32 //nameField.sendKeys(name);33 }34 public void enterLastName(String name){35 //fillInTheField(By.id("de.aureum.card.dev:id/lastName"), name);36 //lastNameField.sendKeys(name);37 }3839 public void isNumKeyboardOpen() throws InterruptedException {40 Thread.sleep(2000);41 if (driver.getCapabilities().getPlatform().toString().equals("LINUX")){42 AndroidDriver android = (AndroidDriver) driver;43 Assert.assertTrue(android.isKeyboardShown());44 } else {45 assertThat("1",driver.findElementByAccessibilityId("1").isEnabled());46 assertThat("0",driver.findElementByAccessibilityId("0").isEnabled());47 //Assert.assertFalse(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("Shift")).isEmpty());48 //Assert.assertFalse(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("Delete")).isEmpty());49 }50 }51 public void isNumKeyboardClosed() {52 if (driver.getCapabilities().getPlatform().toString().equals("LINUX")){53 AndroidDriver android = (AndroidDriver) driver;54 Assert.assertFalse(android.isKeyboardShown());55 } else {56 try {57 driver.findElementByAccessibilityId("0").isEnabled();58 driver.findElementByAccessibilityId("1").isEnabled();59 Assert.assertTrue(false);60 } catch (NoSuchElementException e) {61 Assert.assertTrue(true);62 }63 Assert.assertTrue(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("Shift")).isEmpty());64 Assert.assertTrue(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("Delete")).isEmpty());65 }66 }67 public void isKeyboardOpen() {68 //String str = driver.findElement(MobileBy.ByAccessibilityId.AccessibilityId("Return")).toString();69 //boolean btn = driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("Return")).isEmpty();70 if (driver.getCapabilities().getPlatform().toString().equals("LINUX")){71 AndroidDriver android = (AndroidDriver) driver;72 Assert.assertTrue(android.isKeyboardShown());73 } else {74 Assert.assertFalse(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("Return")).isEmpty());75 Assert.assertFalse(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("space")).isEmpty());76 }77 }78 public void isKeyboardClosed() {79 if (driver.getCapabilities().getPlatform().toString().equals("LINUX")){80 AndroidDriver android = (AndroidDriver) driver;81 Assert.assertFalse(android.isKeyboardShown());82 } else {83 Assert.assertTrue(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("Return")).isEmpty());84 Assert.assertTrue(driver.findElements(MobileBy.ByAccessibilityId.AccessibilityId("space")).isEmpty());85 }86 }8788 public void isNextDisabled() {89 Assert.assertTrue(!NextBtn.isEnabled());90 }91 public void isNextEnabled() {92 Assert.assertTrue(NextBtn.isEnabled());93 }94 public void goBack() {95 formButtons.get(4).click();96 }9798 public void verifyCodeTitle(String phone, String country_code) {99 if (driver.getCapabilities().getPlatform().toString().equals("LINUX")){100 Assert.assertThat(formStaticTexts.get(0).getAttribute("text"), containsString(country_code +" "+phone));101 } else {102 //String full_title = "Weâve sent the code to the "+ country_code +" "+phone;103 //Assert.assertEquals(formStaticTexts.get(0).getAttribute("value"),full_title);104 Assert.assertThat(formStaticTexts.get(3).getAttribute("value"), containsString(country_code +" "+phone));105 }106 }107 public void resendCode() throws InterruptedException {108 codeHaventReceived.click();109 Thread.sleep(33000);110 codeResend.click();111 Thread.sleep(2000);112 }113 public void verifyCodeError() {114 if(driver.getCapabilities().getPlatform().toString().equals("LINUX")){115 Assert.assertEquals(wrongCodeError.getAttribute("text"),"Wrong code");116 } else {117 Assert.assertEquals(wrongCodeError.getAttribute("value"),"Wrong code");118 }119 }120121 public void waitForCode() {122 FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)123 .withTimeout(2, TimeUnit.MINUTES)124 .pollingEvery(10,TimeUnit.SECONDS)125 .ignoring(NoSuchElementException.class)126 .withMessage("Timeout waiting the code");127 wait.until(ExpectedConditions.elementToBeClickable(formButtons.get(2)));128 }129130 public void chooseAvatar(int number) {131 avatars.get(number).click();132 }133134}
...
CalculatorApp.java
Source:CalculatorApp.java
1package com.itest4u.testing.implementations;2import io.appium.java_client.MobileBy;3import org.openqa.selenium.By;4import java.net.MalformedURLException;5import java.util.Arrays;6import java.util.HashMap;7public class CalculatorApp extends MobileBase implements com.itest4u.testing.ResourceInterfaces.ICalculatorApp {8 HashMap<String, By> IDs = new HashMap<>();9 public CalculatorApp() throws MalformedURLException {10 IDs.put("+", new MobileBy.ByAccessibilityId("Plus"));11 IDs.put("=", new MobileBy.ByAccessibilityId("Equal"));12 IDs.put("-", new MobileBy.ByAccessibilityId("Minus"));13 IDs.put("/", new MobileBy.ByAccessibilityId("Divide"));14 IDs.put("%", new MobileBy.ByAccessibilityId("Percentage"));15 IDs.put("(", new MobileBy.ByAccessibilityId("Brackets"));16 IDs.put(")", new MobileBy.ByAccessibilityId("Brackets"));17 IDs.put(".", new MobileBy.ByAccessibilityId("Dot"));18 IDs.put("~", new MobileBy.ByAccessibilityId("Plus/minus"));19 IDs.put("*", new MobileBy.ByAccessibilityId("Multiply"));20 IDs.put("x", new MobileBy.ByAccessibilityId("Multiply"));21 IDs.put("X", new MobileBy.ByAccessibilityId("Multiply"));22 IDs.put("toast", new By.ByXPath("//android.widget.Toast") {23 });24 IDs.put("result", new By.ById("com.sec.android.app.popupcalculator:id/txtCalc"));25 for (int i=0;i<9;i++) {26 IDs.put(String.valueOf(i), new MobileBy.ByAccessibilityId(String.valueOf(i)));27 }28 }29 @Override30 public void click(String c) {31 click(IDs.get(c));32 }33 @Override34 public void input(String inputStr) {35 Arrays.stream(inputStr.split("")).forEach(this::click);36 }37 @Override38 public String getResult() {39 click(IDs.get("="));40 return getText(IDs.get("result")).replace("â","-");41 }42 @Override43 public String getToast() {44 getSource("check");45 return getText(IDs.get("toast"));46 }47}...
LocatorMethods.java
Source:LocatorMethods.java
1package stepdefinitions;2import static stepdefinitions.InitializeTest.driver;3import static stepdefinitions.InitializeTest.elementIds;4import static stepdefinitions.InitializeTest.locatorTypes;5import io.appium.java_client.MobileBy;6import io.appium.java_client.MobileBy.ByAccessibilityId;7import io.appium.java_client.MobileElement;8import java.util.concurrent.TimeUnit;9import org.openqa.selenium.By;10import org.openqa.selenium.support.ui.ExpectedConditions;11import org.openqa.selenium.support.ui.WebDriverWait;12public class LocatorMethods {13 public static MobileElement elementLocator(String element) {14 String id = elementIds.getProperty(element);15 String type = locatorTypes.getProperty(element);16 MobileElement element1 = null;17 switch (type) {18 case "id":19 WebDriverWait wait = new WebDriverWait(driver,30);20 wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id)));21 element1 = driver.findElement(By.id(id));22 break;23 case "contentdesc":24 //driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);25 WebDriverWait wait2 = new WebDriverWait(driver,30);26 wait2.until(ExpectedConditions.visibilityOfElementLocated(new ByAccessibilityId(id)));27 //new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOf(driver.findElementByAccessibilityId(id)));28 element1 = driver.findElementByAccessibilityId(id);29 break;30 case "name":31 element1 = driver.findElement(By.name(id));32 break;33 case "linktext":34 element1 = driver.findElement(By.linkText(id));35 break;36 case "xpath":37 // element = driver.findElement(By.xpath(id));38 element1 = driver.findElement(MobileBy.xpath(id));39 break;40 case "class":41 element1 = driver.findElementByClassName(id);42 break;43 default:44 throw new IllegalStateException("Unexpected value: " + type);45 }46 return element1;47 }48}...
EnderecoPage.java
Source:EnderecoPage.java
1package Ze_Delivery.pages;2import core.BasePage;3import io.appium.java_client.MobileBy;4import io.appium.java_client.MobileElement;5import io.appium.java_client.android.AndroidDriver;6import org.openqa.selenium.By;7import static core.Conexao.getDriver;8 public class EnderecoPage extends BasePage {9 private AndroidDriver<MobileElement> driver;10 private By complement = new MobileBy.ByAccessibilityId("complement-input");11 private By notComplement = new MobileBy.ByAccessibilityId("not-own-complemnt");12 private By btnCasa = new MobileBy.ByAccessibilityId("home-button");13 private By btnTrabalho = new MobileBy.ByAccessibilityId("work-button");14 private By btnOutros = new MobileBy.ByAccessibilityId("others-button");15 private By setOutros = new MobileBy.ByAccessibilityId("other-place-name-input");16 private By btnContinuar = new MobileBy.ByAccessibilityId("continue");17 private By btnConfirmar = By.xpath("//*[@text='CONFIRMAR']");18 public EnderecoPage(AndroidDriver<MobileElement> driver) {19 super(driver);20 }21 public void setComplemento(String Complemento){22 getDriver().findElement(complement).sendKeys(Complemento);23 }24 public void notComplemento(){25 getDriver().findElement(notComplement).click();26 }27 public void casa(){28 getDriver().findElement(btnCasa).click();29 }30 public void trabalho(){31 getDriver().findElement(btnTrabalho).click();32 }33 public void outros(String detalhes){34 getDriver().findElement(btnOutros).click();35 getDriver().findElement(setOutros).sendKeys(detalhes);36 }37 public void confirmar(){38 getDriver().findElement(btnConfirmar).click();39 }40 public void continuar(){41 getDriver().findElement(btnContinuar).click();42 }43}...
IosHelperTest.java
Source:IosHelperTest.java
1package nl.praegus.fitnesse.slim.util;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.MobileBy;4import org.junit.Before;5import org.junit.Test;6import org.junit.runner.RunWith;7import org.mockito.Mock;8import org.mockito.Mockito;9import org.mockito.junit.MockitoJUnitRunner;10import static org.mockito.ArgumentMatchers.eq;11import static org.mockito.Mockito.any;12import static org.mockito.Mockito.times;13import static org.mockito.Mockito.verify;14@RunWith(MockitoJUnitRunner.class)15public class IosHelperTest {16 @Mock17 private AppiumDriver webDriver;18 private IosHelper iosHelper = new IosHelper();19 @Before20 public void setMocks() {21 iosHelper.setWebDriver(webDriver, 10);22 }23 @Test24 public void when_element_is_checked_for_visibility_several_attempts_are_made_to_find_the_element() {25 iosHelper.getElementToCheckVisibility("banaan");26 verify(webDriver, times(1)).findElement(any(MobileBy.ByAccessibilityId.ByAccessibilityId.class));27 verify(webDriver, times(1)).findElements(eq(MobileBy.iOSNsPredicateString("name CONTAINS 'banaan' OR label CONTAINS 'banaan' OR value CONTAINS 'banaan'")));28 }29}...
MobileBy.ByAccessibilityId
Using AI Code Generation
1import io.appium.java_client.MobileBy;2import io.appium.java_client.MobileElement;3import io.appium.java_client.android.AndroidDriver;4import java.net.MalformedURLException;5import java.net.URL;6import org.openqa.selenium.remote.DesiredCapabilities;7public class AccessibilityId {8 public static void main(String[] args) throws MalformedURLException, InterruptedException {9 DesiredCapabilities dc = new DesiredCapabilities();10 dc.setCapability("deviceName", "emulator-5554");11 dc.setCapability("platformName", "Android");12 dc.setCapability("appPackage", "com.android.calculator2");13 dc.setCapability("appActivity", "com.android.calculator2.Calculator");14 dc.setCapability("noReset", true);
MobileBy.ByAccessibilityId
Using AI Code Generation
1MobileElement element = driver.findElement(MobileBy.ByAccessibilityId("Accessibility"));2MobileElement element = driver.findElement(MobileBy.ByAndroidUIAutomator("UiSelector().description(\"Accessibility\")"));3MobileElement element = driver.findElement(MobileBy.ByIosUIAutomation(".elements()[0]"));4MobileElement element = driver.findElement(MobileBy.ById("com.android.packageinstaller:id/permission_allow_button"));5MobileElement element = driver.findElement(MobileBy.ByClassName("android.widget.Button"));6MobileElement element = driver.findElement(MobileBy.ByLinkText("Accessibility"));7MobileElement element = driver.findElement(MobileBy.ByName("Accessibility"));8MobileElement element = driver.findElement(MobileBy.ByPartialLinkText("Access"));9List<MobileElement> element = driver.findElements(MobileBy.ByAccessibilityId("Accessibility"));10List<MobileElement> element = driver.findElements(MobileBy.ByAndroidUIAutomator("UiSelector().description(\"Accessibility\")"));11List<MobileElement> element = driver.findElements(MobileBy.ByIosUIAutomation(".elements()[0]"));12List<MobileElement> element = driver.findElements(MobileBy.ById("com.android.packageinstaller:id/permission_allow_button"));
MobileBy.ByAccessibilityId
Using AI Code Generation
1MobileElement element = (MobileElement) driver.findElementByAccessibilityId("AccessibilityId");2MobileElement element = (MobileElement) driver.findElementByAndroidUIAutomator("text(\"AccessibilityId\")");3IWebElement element = driver.FindElement(MobileBy.AccessibilityId("AccessibilityId"));4IWebElement element = driver.FindElement(MobileBy.AndroidUIAutomator("text(\"AccessibilityId\")"));5element = driver.find_element_by_accessibility_id("AccessibilityId")6element = driver.find_element_by_android_uiautomator("text(\"AccessibilityId\")")7element = driver.find_element :android_uiautomator, 'text("AccessibilityId")'8element = driver.find_element(:accessibility_id, "AccessibilityId")9element = driver.find_element(:android_uiautomator, "text(\"AccessibilityId\")")10element := driver.FindElement(appium.ByAccessibilityId("AccessibilityId"))11element := driver.FindElement(appium.ByAndroidUIAutomator("text(\"AccessibilityId\")"))12element = driver.findElement(appium.ByAccessibilityId("AccessibilityId"));13element = driver.findElement(appium.ByAndroidUI
MobileBy.ByAccessibilityId
Using AI Code Generation
1MobileElement element = (MobileElement) driver.findElement(MobileBy.ByAccessibilityId("Accessibility"));2MobileElement element = (MobileElement) driver.findElement(MobileBy.ByAndroidUIAutomator("UiSelector().text(\"Accessibility\")"));3MobileElement element = (MobileElement) driver.findElement(MobileBy.ByIosUIAutomation(".elements()[\"Accessibility\"]"));4MobileElement element = (MobileElement) driver.findElement(AndroidBy.AndroidUIAutomator("UiSelector().text(\"Accessibility\")"));5MobileElement element = (MobileElement) driver.findElement(IOSBy.IosUIAutomation(".elements()[\"Accessibility\"]"));6MobileElement element = (MobileElement) driver.findElement(WindowsBy.AccessibilityId("Accessibility"));7MobileElement element = (MobileElement) driver.findElement(WindowsBy.Name("Accessibility"));8MobileElement element = (MobileElement) driver.findElement(WindowsBy.WindowsAutomation("Accessibility"));9MobileElement element = (MobileElement) driver.findElement(WindowsBy.XPath("Accessibility"));10MobileElement element = (MobileElement) driver.findElement(WindowsBy.ClassName("Accessibility"));11MobileElement element = (MobileElement) driver.findElement(WindowsBy.CssSelector("Accessibility"));
MobileBy.ByAccessibilityId
Using AI Code Generation
1import io.appium.java_client.MobileBy;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5import org.openqa.selenium.remote.DesiredCapabilities;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8import java.net.URL;9public class Appium {10 public static void main(String[] args) throws Exception {11 DesiredCapabilities capabilities = new DesiredCapabilities();12 capabilities.setCapability("deviceName", "My Phone");13 capabilities.setCapability("BROWSER_NAME", "Android");14 capabilities.setCapability("VERSION", "4.4.2");15 capabilities.setCapability("platformName", "Android");16 capabilities.setCapability("appPackage", "com.android.calculator2");17 capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
MobileBy.ByAccessibilityId
Using AI Code Generation
1WebElement element = driver.findElement(MobileBy.ByAccessibilityId("content-desc-value"));2element = self.driver.find_element(MobileBy.ACCESSIBILITY_ID, 'content-desc-value')3var element = driver.findElement(MobileBy.AccessibilityId('content-desc-value'));4$element = $driver.FindElement([MobileBy]::AccessibilityId('content-desc-value'))5$element = $driver.FindElement([MobileBy]::AccessibilityId('content-desc-value'))6$element = $driver->findElement(MobileBy::accessibilityId('content-desc-value'))7element := driver.FindElement(MobileBy.AccessibilityId("content-desc-value"))8var element = driver.FindElement(MobileBy.AccessibilityId("content-desc-value
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!