Best io.appium code snippet using io.appium.java_client.HasOnScreenKeyboard
RegistrationTest.java
Source:RegistrationTest.java
...15import org.openqa.selenium.remote.DesiredCapabilities;16import org.openqa.selenium.support.ui.ExpectedConditions;17import org.openqa.selenium.support.ui.WebDriverWait;1819import io.appium.java_client.HasOnScreenKeyboard;20import io.appium.java_client.TouchAction;21import io.appium.java_client.android.AndroidDriver;22import io.appium.java_client.android.AndroidElement;23import io.appium.java_client.android.nativekey.AndroidKey;24import io.appium.java_client.android.nativekey.KeyEvent;25import io.appium.java_client.remote.MobileCapabilityType;26import io.appium.java_client.touch.offset.ElementOption;2728@RunWith(ConfigRunner.class)29public class RegistrationTest {30 31 public static AndroidDriver adDriver;32 33 @Config34 public static MBTestConfiguration config = new MBTestConfiguration(35 "src/test/resources/RegistrationTest.xls", //config test36 "c:/temp/RegistrationTest_result.xls", //test result37 "RegistrationTest"); //sheet name in file in38 39 @Before40 public static void before() throws MalformedURLException {41 DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); 42 desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, config.getCapabilities().get("MobileCapabilityType.AUTOMATION_NAME"));43 desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, config.getCapabilities().get("MobileCapabilityType.PLATFORM_NAME"));44 desiredCapabilities.setCapability("dontStopAppOnReset", config.getCapabilities().get("dontStopAppOnReset"));45 desiredCapabilities.setCapability("appActivity", config.getCapabilities().get("appActivity"));46 desiredCapabilities.setCapability("appPackage", config.getCapabilities().get("appPackage"));47 desiredCapabilities.setCapability("autoGrantPermissions", config.getCapabilities().get("autoGrantPermissions"));48 //Tambahan capability agar junit junit tidak mereset aplikasi setelah test selesai49 //desiredCapabilities.setCapability("noReset", "true");50 51 URL url = new URL("http://localhost:4723/wd/hub");52 adDriver = new AndroidDriver<AndroidElement>(url, desiredCapabilities); 53 54 }55 56 @AfterClass57 public static void close() {58 config.close();59 }60 61 @Test62 public void registrationTest( ) {63 try{64 registration();65 config.writeToTestOuput("Status", "Success");66 67 byte[] srcFile = ((TakesScreenshot)adDriver).getScreenshotAs(OutputType.BYTES);68 config.saveScreenshot("Printscreen", srcFile);69 } catch(Exception e) {70 config.writeToTestOuput("Status", "Fail");71 config.writeToTestOuput("ErrorMessage", e.getMessage());72 73 byte[] srcFile = ((TakesScreenshot)adDriver).getScreenshotAs(OutputType.BYTES);74 config.saveScreenshot("Printscreen", srcFile);75 throw e;76 }77 }78 79 80 private void registration() {81 82 final String clearButtonId = "com.android.systemui:id/clear_all";83 84 final String registrationButtonXPath ="//android.widget.ImageView[@content-desc='"+ config.getResourceValue("registration_button")+"']";85 final String checkBoxApproveXPath ="//android.widget.CheckBox[@content-desc='"+ config.getResourceValue("tc_registration_checkbox")+"']";86 final String approveButtonXPath ="//android.widget.Button[@content-desc='"+ config.getResourceValue("approve_registration_button")+"']";87 final String inputAccountXPath ="//android.view.View[@content-desc='"+config.getResourceValue("account_registration_input")+"']//following-sibling::android.widget.EditText";88 final String nextButtonAccount ="//android.widget.Button[@content-desc='"+config.getResourceValue("next_registration_button")+"']";89 final String inputKTPXPath = "//android.view.View[@content-desc='"+config.getResourceValue("id_customer_registration_button")+"']//following-sibling::android.widget.EditText";90 final String inputEmailXPath ="//android.view.View[@content-desc='"+config.getResourceValue("email_registration_input")+"']//following-sibling::android.widget.EditText";91 final String OTPXPath ="//android.view.View[contains(@content-desc,'"+config.getResourceValue("otp_registration_message")+"')]//following-sibling::android.view.View";92 final String inputUsernameXPath ="//android.view.View[@content-desc='"+config.getResourceValue("username_registration_input")+"']//following-sibling::android.widget.EditText";93 final String inputPasswordXPath ="//android.view.View[@content-desc='"+config.getResourceValue("password_registration_input")+"']//following-sibling::android.widget.EditText";94 final String registrationLastPageXPath ="//android.view.View[@content-desc='"+config.getResourceValue("registration_successfull_message")+"']";95 96 //clearNotif97 adDriver.openNotifications();98 try {99 AndroidElement clearButton = (AndroidElement) new WebDriverWait(adDriver, 5).until(100 ExpectedConditions.elementToBeClickable(By.id(clearButtonId)));101 102 clearButton.click();103 } catch(Exception e) {104 adDriver.pressKey(new KeyEvent(AndroidKey.BACK));105 }106 107 108 //wait until page loaded109 AndroidElement registrationButton = (AndroidElement) new WebDriverWait(adDriver, 30).until(110 ExpectedConditions.elementToBeClickable(By.xpath(registrationButtonXPath)));111 112 registrationButton.click();113 114 //wait until TC page loaded115 AndroidElement tCCheckBox = (AndroidElement) new WebDriverWait(adDriver, 30).until(116 ExpectedConditions.elementToBeClickable(By.xpath(checkBoxApproveXPath)));117 118 if(!config.getParameterValue("Accept").toUpperCase().equals("NO")) {119 tCCheckBox.click();120 }121 122 adDriver.findElementByXPath(approveButtonXPath).click();123 124 //wait username page loaded125 AndroidElement inputAccount = (AndroidElement) new WebDriverWait(adDriver, 30).until(126 ExpectedConditions.elementToBeClickable(By.xpath(inputAccountXPath)));127 128 inputAccount.click();129 inputAccount.sendKeys(config.getParameterValue("Account"));130 //keyNum(testParam.get("Account"));131132 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();133 134 //next135 adDriver.findElementByXPath(nextButtonAccount).click();136137 //wait id page loaded138 AndroidElement inputKTP = (AndroidElement) new WebDriverWait(adDriver, 30).until(139 ExpectedConditions.elementToBeClickable(By.xpath(inputKTPXPath)));140 141 inputKTP.click();142 inputKTP.sendKeys(config.getParameterValue("ID"));143 //keyNum(testParam.get("ID"));144 145 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();146 //next147 adDriver.findElementByXPath(nextButtonAccount).click();148 149 //wait email page loaded150 AndroidElement inputEmail = (AndroidElement) new WebDriverWait(adDriver, 30).until(151 ExpectedConditions.elementToBeClickable(By.xpath(inputEmailXPath)));152 153 inputEmail.click();154 inputEmail.sendKeys(config.getParameterValue("Email"));155 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();156 157 //next158 adDriver.findElementByXPath(nextButtonAccount).click();159 160 //wait for OTP screen161 AndroidElement inputOTP = (AndroidElement) new WebDriverWait(adDriver, 30).until(162 ExpectedConditions.elementToBeClickable(By.xpath(OTPXPath)));163 164 String otp;165 if(config.getParameterValue("OTP").toUpperCase().equals("AUTO") ) {166 otp = CommonTest.getOTPFromNotification(adDriver);167 } else {168 otp = config.getParameterValue("OTP");169 }170 171 if(config.getParameterValue("ExpiredOTP").toUpperCase().equals("YES")) {172 173 CommonTest.sleep(adDriver, 60000);174 }175 176 if(config.getParameterValue("ResendOTP").toUpperCase().equals("YES")) {177 CommonTest.clearNotif(adDriver);178 179 adDriver.findElementByXPath("//android.view.View[@content-desc='Kirim ulang']").click();180 otp = CommonTest.getOTPFromNotification(adDriver);181 }182 183 TouchAction action = new TouchAction(adDriver);184 action.tap(ElementOption.element(inputOTP,10,10));185 action.perform();186187 CommonTest.keyNum(adDriver,otp);188 189 //next190 adDriver.findElementByXPath(nextButtonAccount).click();191 192 //username193 AndroidElement inputUsername = (AndroidElement) new WebDriverWait(adDriver, 30).until(194 ExpectedConditions.elementToBeClickable(By.xpath(inputUsernameXPath)));195 196 inputUsername.click();197 inputUsername.sendKeys(config.getParameterValue("Username"));198 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();199 200 //next201 adDriver.findElementByXPath(nextButtonAccount).click();202 203 //input password204 AndroidElement inputPassword = (AndroidElement) new WebDriverWait(adDriver, 30).until(205 ExpectedConditions.elementToBeClickable(By.xpath(inputPasswordXPath)));206 207 List<AndroidElement> inputPwds = adDriver.findElementsByXPath(inputPasswordXPath);208 209 //pwd element210 AndroidElement inputMainPwd = inputPwds.get(0);211 inputMainPwd.click();212 inputMainPwd.sendKeys(config.getParameterValue("Password"));213 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();214 215 //pwd confirmpwd216 String confirmPwd;217 if(config.getParameterValue("ConfirmPwd").isBlank()) {218 confirmPwd = config.getParameterValue("Password");219 }220 else {221 confirmPwd = config.getParameterValue("ConfirmPwd");222 }223 224 AndroidElement inputConfirmPwd = inputPwds.get(1);225 inputConfirmPwd.click();226 inputConfirmPwd.sendKeys(confirmPwd);227 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();228 229 //next last step230 adDriver.findElementByXPath(nextButtonAccount).click();231 232233 234 //make PIN235 makePIN(config.getResourceValue("mobile_pin_registration_input"),config.getParameterValue("PIN"));236 //confirmation PIN237 makePIN(config.getResourceValue("confirmation_mobile_pin_registration_input"), config.getParameterValue("ConfirmPIN"));238 239 //finally240 AndroidElement title = (AndroidElement) new WebDriverWait(adDriver, 30).until(241 ExpectedConditions.presenceOfElementLocated(By.xpath(registrationLastPageXPath)));
...
LoginTest.java
Source:LoginTest.java
...15import org.openqa.selenium.remote.DesiredCapabilities;16import org.openqa.selenium.support.ui.ExpectedConditions;17import org.openqa.selenium.support.ui.WebDriverWait;1819import io.appium.java_client.HasOnScreenKeyboard;20import io.appium.java_client.TouchAction;21import io.appium.java_client.android.AndroidDriver;22import io.appium.java_client.android.AndroidElement;23import io.appium.java_client.remote.MobileCapabilityType;24import io.appium.java_client.touch.offset.ElementOption;2526@RunWith(ConfigRunner.class)27public class LoginTest {28 29 public static AndroidDriver adDriver;30 31 @Config32 public static MBTestConfiguration config = new MBTestConfiguration(33 "src/test/resources/LoginTest.xls", //config test34 "c:/temp/LoginTest_result.xls", //test result35 "LoginTest"); //sheet name in file in36 37 @Before38 public static void before() throws MalformedURLException {39 DesiredCapabilities desiredCapabilities = new DesiredCapabilities(); 40 desiredCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, config.getCapabilities().get("MobileCapabilityType.AUTOMATION_NAME"));41 desiredCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, config.getCapabilities().get("MobileCapabilityType.PLATFORM_NAME"));42 desiredCapabilities.setCapability("dontStopAppOnReset", config.getCapabilities().get("dontStopAppOnReset"));43 desiredCapabilities.setCapability("appActivity", config.getCapabilities().get("appActivity"));44 desiredCapabilities.setCapability("appPackage", config.getCapabilities().get("appPackage"));45 desiredCapabilities.setCapability("autoGrantPermissions", config.getCapabilities().get("autoGrantPermissions"));46 //Tambahan capability agar junit junit tidak mereset aplikasi setelah test selesai47 //desiredCapabilities.setCapability("noReset", "true");48 49 URL url = new URL("http://localhost:4723/wd/hub");50 adDriver = new AndroidDriver<AndroidElement>(url, desiredCapabilities); 51 52 }53 54 @AfterClass55 public static void close() {56 config.close();57 }58 59 @Test60 public void loginTest() {61 62 try{63 login();64 config.writeToTestOuput("Status", "Success");65 66 byte[] srcFile = ((TakesScreenshot)adDriver).getScreenshotAs(OutputType.BYTES);67 config.saveScreenshot("Printscreen", srcFile);68 } catch(Exception e) {69 config.writeToTestOuput("Status", "Fail");70 config.writeToTestOuput("ErrorMessage", e.getMessage());71 72 byte[] srcFile = ((TakesScreenshot)adDriver).getScreenshotAs(OutputType.BYTES);73 config.saveScreenshot("Printscreen", srcFile);74 throw e;75 }76 77 }78 79 private void login() {80 81 final String loginButtonXPath ="//android.widget.Button[@content-desc='"+config.getResourceValue("login_button")+"']";82 final String confirmLoginButtonXPath ="//android.widget.Button[@content-desc='"+ config.getResourceValue("login_form_button")+ "']";83 final String otpInputXPath = "//android.view.View[contains(@content-desc, '"+ config.getResourceValue("otp_login_message")+"')]//following-sibling::android.view.View";84 85 86 CommonTest.clearNotif(adDriver);87 88 //wait login button loaded89 AndroidElement loginButton = (AndroidElement) new WebDriverWait(adDriver, 30).until(90 ExpectedConditions.elementToBeClickable(By.xpath(loginButtonXPath)));91 92 loginButton.click();93 94 //wait form login loaded95 AndroidElement loginForm = (AndroidElement) new WebDriverWait(adDriver, 30).until(96 ExpectedConditions.elementToBeClickable(By.className("android.widget.EditText")));97 98 List<AndroidElement> editTexts = adDriver.findElementsByClassName("android.widget.EditText");99 100 AndroidElement usernameInput = editTexts.get(0);101 usernameInput.click();102 usernameInput.sendKeys(config.getParameterValue("Username"));103 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();104 105 AndroidElement passwordInput = editTexts.get(1);106 passwordInput.click();107 passwordInput.sendKeys(config.getParameterValue("Password"));108 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();109 110 adDriver.findElementByXPath(confirmLoginButtonXPath).click();111 112 113 List<WebElement> otpElement = new WebDriverWait(adDriver, 30).until(114 ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(otpInputXPath)));115 116 117 AndroidElement otpInput = (AndroidElement) otpElement.get(0);118 119 String otp;120 121 if(config.getParameterValue("OTP").toUpperCase().equals("AUTO")) {122 otp = CommonTest.getOTPFromNotification(adDriver);123 }else {124 otp = config.getParameterValue("OTP");125 }126 127 if(config.getParameterValue("ExpiredOTP").toUpperCase().equals("YES")) {128 129 CommonTest.sleep(adDriver, 60000);130 }131 132 if(config.getParameterValue("ResendOTP").toUpperCase().equals("YES")) {133 CommonTest.clearNotif(adDriver);134 135 adDriver.findElementByXPath("//android.view.View[@content-desc='Kirim ulang']").click();136 otp = CommonTest.getOTPFromNotification(adDriver);137 }138 139 TouchAction action = new TouchAction(adDriver);140 action.tap(ElementOption.element(otpInput,10,10));141 action.perform();142 143 CommonTest.keyNum(adDriver, otp);144 if(((HasOnScreenKeyboard) adDriver).isKeyboardShown()) adDriver.hideKeyboard();145 146 //next147 adDriver.findElementByXPath("//android.widget.Button[@content-desc='"+config.getResourceValue("next_login_button")+"']").click();148 149 //finally navigation bar loaded150 AndroidElement bankHome = (AndroidElement) new WebDriverWait(adDriver, 30).until(151 ExpectedConditions.presenceOfElementLocated(By.xpath("//android.view.View[contains(@content-desc,\"Assalamu'alaikum\")]")));152 }153 154 155156}
...
TelaPrincipal.java
Source:TelaPrincipal.java
...10import org.openqa.selenium.support.ui.ExpectedConditions;11import org.openqa.selenium.support.ui.FluentWait;12import org.openqa.selenium.support.ui.Wait;13import io.appium.java_client.AppiumDriver;14import io.appium.java_client.HasOnScreenKeyboard;15import io.appium.java_client.MobileElement;16import io.appium.java_client.pagefactory.AndroidFindBy;17import io.appium.java_client.pagefactory.AppiumFieldDecorator;18import io.appium.java_client.pagefactory.iOSFindBy;19import io.appium.java_client.pagefactory.iOSXCUITFindBy;20public class TelaPrincipal {21 private WebDriver driver;22 Wait<WebDriver> wait;23 public TelaPrincipal(WebDriver driver) {24 PageFactory.initElements(new AppiumFieldDecorator(driver), this);25 this.driver = driver;26 wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(15))27 .pollingEvery(Duration.ofMillis(1000))28 .ignoring(NoSuchElementException.class, org.openqa.selenium.WebDriverException.class);29 }30 @iOSFindBy(accessibility = "Menu")31 @AndroidFindBy(id = "com.Advantage.aShopping:id/imageViewMenu")32 private MobileElement menu;33 @AndroidFindBy(id = "com.Advantage.aShopping:id/textViewMenuUser")34 @iOSFindBy(xpath = "//XCUIElementTypeCell[XCUIElementTypeButton[@name='User settings']]/XCUIElementTypeStaticText[1]")35 private MobileElement menuLoginItem;36 //@iOSFindBy(xpath = "//XCUIElementTypeSearchField[@name='Search']")37 @iOSXCUITFindBy(iOSNsPredicate="type=='XCUIElementTypeSearchField'")38 @AndroidFindBy(id = "com.Advantage.aShopping:id/editTextSearch")39 private MobileElement txtPesquisaProduto;40 //@iOSFindBy(xpath = "//XCUIElementTypeButton[@name='Search']")41 @iOSXCUITFindBy(iOSNsPredicate="name=='Search' AND type=='XCUIElementTypeButton'")42 @AndroidFindBy(id = "com.Advantage.aShopping:id/imageViewSearch")43 private MobileElement btnPesquisaProduto;44 @AndroidFindBy(xpath = "//*[@resource-id='com.Advantage.aShopping:id/recyclerViewCategories']/android.widget.RelativeLayout/android.widget.TextView")45 @iOSFindBy(xpath = "//XCUIElementTypeCell/XCUIElementTypeStaticText")46 private List<MobileElement> categorias;47 48 @AndroidFindBy(uiAutomator = "new UiSelector().textContains(\"SIGN OUT\")")49 @iOSXCUITFindBy(iOSNsPredicate="name=='SIGN OUT'")50 private MobileElement lnkLogout;51 @AndroidFindBy(uiAutomator = "new UiSelector().textContains(\"YES\")")52 @iOSXCUITFindBy(iOSNsPredicate="name=='Yes'")53 private MobileElement logoutOK;54 55 public MobileElement getMenu() {56 return menu;57 }58 public MobileElement getMenuLoginItem() {59 return menuLoginItem;60 }61 public void clicarNoMenu() {62 wait.until(ExpectedConditions.elementToBeClickable(getMenu()));63 this.menu.click();64 }65 public void clicarNoItemLogin() {66 getMenuLoginItem().click();67 }68 public void validarTelaInicial() {69 wait.until(ExpectedConditions.elementToBeClickable(getMenu()));70 Assert.assertTrue(getMenu().isDisplayed());71 }72 public void digitarProduto(String produto) {73 wait.until(ExpectedConditions.elementToBeClickable(this.txtPesquisaProduto));74 this.txtPesquisaProduto.sendKeys(produto);75 //hideKeyboard();76 }77 public void clicarPesquisaProduto() {78 this.btnPesquisaProduto.click();79 }80 81 public void fazerLogout() {82 this.lnkLogout.click();83 this.logoutOK.click();84 }85 public void clicarNaCategoria(String nomeCategoria) {86 for (WebElement categoria : categorias) {87 if (categoria.getText().equalsIgnoreCase(nomeCategoria)) {88 categoria.click();89 return;90 }91 }92 Assert.assertTrue(String.format("Categoria %s não encontrada", nomeCategoria), false);93 }94 public void validarUsuarioLogado(String usuario, String nomeUsuario) {95 wait.until(ExpectedConditions.visibilityOf(this.menuLoginItem));96 String user = this.menuLoginItem.getText();97 if (!user.equals(usuario.trim()) & !user.equals(nomeUsuario.trim())) {98 Assert.assertTrue(String.format("Usuário '%s' não está logado", nomeUsuario), false);99 }100 fazerLogout();101 }102 private void hideKeyboard() {103 if ("android".equalsIgnoreCase(104 (String) ((AppiumDriver) this.driver).getCapabilities().getCapability("platformName"))) {105 if (((HasOnScreenKeyboard) (this.driver)).isKeyboardShown())106 ((AppiumDriver) this.driver).hideKeyboard();107 } else108 try {109 //this.driver.findElement(By.xpath("//XCUIElementTypeApplication")).click();110 this.driver.findElement(By.id("This is a demo site. Do not use real data")).click();111 if (((HasOnScreenKeyboard) (this.driver)).isKeyboardShown()) {112 try {113 ((AppiumDriver) this.driver).findElementByAccessibilityId("Seguinte").click();114 } catch (Exception e1) {115 try {116 ((AppiumDriver) this.driver).findElementByAccessibilityId("ConcluÃdo").click();117 } catch (Exception e2) {118 }119 }120 }121 } catch (Exception e) {122 try {123 ((AppiumDriver) this.driver).findElementByAccessibilityId("Seguinte").click();124 } catch (Exception e1) {125 try {...
TelaOffline.java
Source:TelaOffline.java
...7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.FluentWait;9import org.openqa.selenium.support.ui.Wait;10import io.appium.java_client.AppiumDriver;11import io.appium.java_client.HasOnScreenKeyboard;12import io.appium.java_client.MobileElement;13import io.appium.java_client.pagefactory.AndroidFindBy;14import io.appium.java_client.pagefactory.AppiumFieldDecorator;15import io.appium.java_client.pagefactory.iOSFindBy;16import io.appium.java_client.pagefactory.iOSXCUITFindBy;17public class TelaOffline {18 private WebDriver driver;19 private Wait<WebDriver> wait;20 public TelaOffline(WebDriver driver) {21 PageFactory.initElements(new AppiumFieldDecorator(driver), this);22 this.driver = driver;23 wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(15))24 .pollingEvery(Duration.ofMillis(1000))25 .ignoring(NoSuchElementException.class, org.openqa.selenium.WebDriverException.class);26 }27 //@AndroidFindBy(xpath = "//android.widget.TextView[@text='You are not connected to the internet']")28 @AndroidFindBy(uiAutomator = "textContains(\"You are not connected to the internet\")")29 //@iOSFindBy(xpath = "//XCUIElementTypeStaticText[@name='You are not connected to the internet']")30 @iOSXCUITFindBy(iOSNsPredicate="name=='You are not connected to the internet'")31 private MobileElement alertOfflineMode;32 //@AndroidFindBy(xpath = "//android.widget.Button[@text='OK']")33 @AndroidFindBy(uiAutomator = "textContains(\"OK\")")34 @iOSFindBy(accessibility = "Ok")35 private MobileElement alertConfirmButton;36 @AndroidFindBy(id = "com.Advantage.aShopping:id/editTextServer")37 //@iOSFindBy(xpath = "//XCUIElementTypeTextField")38 @iOSXCUITFindBy(iOSNsPredicate="type=='XCUIElementTypeTextField'")39 private MobileElement serverField;40 @AndroidFindBy(id = "com.Advantage.aShopping:id/buttonConnect")41 @iOSFindBy(accessibility = "APPLY")42 private MobileElement btnApply;43 //@AndroidFindBy(xpath = "//android.widget.TextView[@text='You are working in offline mode. Data is not retained for future use']")44 @AndroidFindBy(uiAutomator = "textContains(\"You are working in offline mode. Data is not retained for future use\")")45 @iOSFindBy(accessibility = "You are working in offline mode. Data is not retained for future use")46 private MobileElement confirmMessageOfflineAlert;47 //@AndroidFindBy(xpath = "//android.widget.Button[@text='OK']")48 @AndroidFindBy(uiAutomator = "textContains(\"OK\")")49 //@iOSFindBy(xpath = "//XCUIElementTypeButton[@name='Ok']")50 @iOSXCUITFindBy(iOSNsPredicate="type=='XCUIElementTypeButton' AND name=='Ok'")51 private MobileElement confirmButtonOfflineAlert;52 public void validarModoOffline() {53 this.wait.until(ExpectedConditions.visibilityOf(alertOfflineMode));54 this.alertConfirmButton.click();55 try {56 Thread.sleep(2000);57 try {58 if (this.alertConfirmButton.isDisplayed()) {59 this.alertConfirmButton.click();60 }61 } catch (Exception e) {62 }63 } catch (InterruptedException e) {64 e.printStackTrace();65 }66 }67 public void digitarValorServer(String server) {68 // this.wait.until(ExpectedConditions.visibilityOf(serverField));69 // this.serverField.sendKeys(server);70 this.serverField.clear();71 this.serverField.setValue(server);72 hideKeyboard();73 }74 public void clicarNoBotaoApply() {75 this.btnApply.click();76 }77 public void confirmarEscolhaModoOffline() {78 this.wait.until(ExpectedConditions.visibilityOf(confirmMessageOfflineAlert));79 this.confirmButtonOfflineAlert.click();80 }81 private void hideKeyboard() {82 if ("android".equalsIgnoreCase(83 (String) ((AppiumDriver) this.driver).getCapabilities().getCapability("platformName"))) {84 if (((HasOnScreenKeyboard) (this.driver)).isKeyboardShown())85 ((AppiumDriver) this.driver).hideKeyboard();86 } else87 try {88 //this.driver.findElement(By.xpath("//XCUIElementTypeApplication")).click();89 this.driver.findElement(By.id("This is a demo site. Do not use real data")).click();90 if (((HasOnScreenKeyboard) (this.driver)).isKeyboardShown()) {91 try {92 ((AppiumDriver) this.driver).findElementByAccessibilityId("Seguinte").click();93 } catch (Exception e1) {94 try {95 ((AppiumDriver) this.driver).findElementByAccessibilityId("ConcluÃdo").click();96 } catch (Exception e2) {97 }98 }99 }100 } catch (Exception e) {101 try {102 ((AppiumDriver) this.driver).findElementByAccessibilityId("Seguinte").click();103 } catch (Exception e1) {104 try {...
TelaLogin.java
Source:TelaLogin.java
...6import org.openqa.selenium.support.PageFactory;7import org.openqa.selenium.support.ui.FluentWait;8import org.openqa.selenium.support.ui.Wait;9import io.appium.java_client.AppiumDriver;10import io.appium.java_client.HasOnScreenKeyboard;11import io.appium.java_client.MobileElement;12import io.appium.java_client.pagefactory.AndroidFindBy;13import io.appium.java_client.pagefactory.AppiumFieldDecorator;14import io.appium.java_client.pagefactory.iOSFindBy;15public class TelaLogin {16 private WebDriver driver;17 18 Wait<WebDriver> wait;19 public TelaLogin(WebDriver driver) {20 PageFactory.initElements(new AppiumFieldDecorator(driver), this);21 this.driver = driver;22 wait = new FluentWait<WebDriver>(driver).withTimeout(Duration.ofSeconds(15))23 .pollingEvery(Duration.ofMillis(1000))24 .ignoring(NoSuchElementException.class, org.openqa.selenium.WebDriverException.class);25 }26 @iOSFindBy(id = "Don't have an account? SIGN UP TODAY")27 @AndroidFindBy(id = "com.Advantage.aShopping:id/textViewSingUpToday")28 private MobileElement signUpToday;29 @AndroidFindBy(xpath = "//android.widget.RelativeLayout[@resource-id='com.Advantage.aShopping:id/AosEditTextLoginUserName']/android.widget.EditText")30 @iOSFindBy(accessibility = "userNameLabel")31 private MobileElement userNameField;32 @AndroidFindBy(xpath = "//android.widget.RelativeLayout[@resource-id='com.Advantage.aShopping:id/AosEditTextLoginPassword']/android.widget.EditText")33 @iOSFindBy(accessibility = "passwordLabel")34 private MobileElement passwordField;35 @AndroidFindBy(id = "com.Advantage.aShopping:id/buttonLogin")36 @iOSFindBy(accessibility = "LOGIN")37 private MobileElement loggingButton;38 public MobileElement getSignUpToday() {39 return signUpToday;40 }41 public void clicarEmSignUpToday() {42 this.getSignUpToday().click();43 }44 public MobileElement getUserNameField() {45 return userNameField;46 }47 public void digitarNomeUsuario(String nomeUsuario) {48 getUserNameField().sendKeys(nomeUsuario);49 hideKeyboard();50 }51 public MobileElement getPasswordField() {52 return passwordField;53 }54 public void digitarSenha(String senha) {55 getPasswordField().sendKeys(senha);56 hideKeyboard();57 }58 public MobileElement getLoggingButton() {59 return loggingButton;60 }61 public void clicarEmLogin() {62 getLoggingButton().click();63 }64 private void hideKeyboard() {65 if ("android".equalsIgnoreCase(66 (String) ((AppiumDriver) this.driver).getCapabilities().getCapability("platformName"))) {67 if (((HasOnScreenKeyboard) (this.driver)).isKeyboardShown())68 ((AppiumDriver) this.driver).hideKeyboard();69 } else70 try {71 //this.driver.findElement(By.xpath("//XCUIElementTypeApplication")).click();72 this.driver.findElement(By.id("This is a demo site. Do not use real data")).click();73 if (((HasOnScreenKeyboard) (this.driver)).isKeyboardShown()) {74 try {75 ((AppiumDriver) this.driver).findElementByAccessibilityId("Seguinte").click();76 } catch (Exception e1) {77 try {78 ((AppiumDriver) this.driver).findElementByAccessibilityId("ConcluÃdo").click();79 } catch (Exception e2) {80 }81 }82 }83 } catch (Exception e) {84 try {85 ((AppiumDriver) this.driver).findElementByAccessibilityId("Seguinte").click();86 } catch (Exception e1) {87 try {...
CommonTest.java
Source:CommonTest.java
...6import org.openqa.selenium.By;7import org.openqa.selenium.support.ui.ExpectedConditions;8import org.openqa.selenium.support.ui.WebDriverWait;910import io.appium.java_client.HasOnScreenKeyboard;11import io.appium.java_client.MobileBy;12import io.appium.java_client.android.AndroidDriver;13import io.appium.java_client.android.AndroidElement;14import io.appium.java_client.android.nativekey.AndroidKey;15import io.appium.java_client.android.nativekey.KeyEvent;16import io.appium.java_client.touch.offset.ElementOption;17import io.appium.java_client.TouchAction;1819public class CommonTest {20 2122 /*23 * Prform login to app24 * All screen until Beranda25 * **/26 public static void login(String username, String password, AndroidDriver driver) throws Exception {27 28 final String otpXpath = "//android.view.View[@content-desc='Masukkan kode Aktivasi yang telah dikirimkan ke nomor ']//following-sibling::android.view.View";29 30 //wait for Login button31 AndroidElement el = (AndroidElement) new WebDriverWait(driver, 30).until(32 ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("Log in")));33 el.click();34 35 //wait for login form ready36 AndroidElement form = (AndroidElement) new WebDriverWait(driver, 30).until(37 ExpectedConditions.visibilityOfElementLocated(By.className("android.widget.EditText")));38 39 List<AndroidElement> els = driver.findElementsByClassName("android.widget.EditText");40 41 els.get(0).click();42 els.get(0).sendKeys(username);43 44 els.get(1).click();45 els.get(1).sendKeys(password);46 if(((HasOnScreenKeyboard) driver).isKeyboardShown()) driver.hideKeyboard();47 48 AndroidElement login = (AndroidElement) new WebDriverWait(driver, 30).until(49 ExpectedConditions.elementToBeClickable(MobileBy.AccessibilityId("MASUK")));50 login.click();51 52 //wait for OTP screen ready53 AndroidElement otp = (AndroidElement) new WebDriverWait(driver, 30).until(54 ExpectedConditions.elementToBeClickable(By.xpath(otpXpath)));55 56 TouchAction action = new TouchAction(driver);57 action.tap(ElementOption.element(otp,10,10));58 action.perform();59 60 //please dont repeat me
...
MobileKeyboard.java
Source:MobileKeyboard.java
1package com.epam.jdi.light.mobile.elements.common;2import com.epam.jdi.light.elements.common.Keyboard;3import io.appium.java_client.HasOnScreenKeyboard;4import io.appium.java_client.HidesKeyboard;5import io.appium.java_client.HidesKeyboardWithKeyName;6import io.appium.java_client.android.nativekey.AndroidKey;7import io.appium.java_client.android.nativekey.KeyEvent;8import io.appium.java_client.android.nativekey.PressesKey;9import static com.epam.jdi.light.mobile.MobileUtils.executeDriverMethod;10public class MobileKeyboard extends Keyboard {11 public static boolean isKeyboardShown() {12 return executeDriverMethod(HasOnScreenKeyboard.class, HasOnScreenKeyboard::isKeyboardShown);13 }14 public static void hideKeyboard() {15 executeDriverMethod(HidesKeyboard.class, HidesKeyboard::hideKeyboard);16 }17 // next two methods are for IosDriver only18 public static void hideKeyboard(String keyName) {19 executeDriverMethod(HidesKeyboardWithKeyName.class,20 (HidesKeyboardWithKeyName driver) -> driver.hideKeyboard(keyName));21 }22 public static void hideKeyboard(String strategy, String keyName) {23 executeDriverMethod(HidesKeyboardWithKeyName.class,24 (HidesKeyboardWithKeyName driver) -> driver.hideKeyboard(strategy, keyName));25 }26 // next two methods are for AndroidDriver only...
KeyboardUtils.java
Source:KeyboardUtils.java
1package framework.utilities.keyboard;2import aquality.appium.mobile.application.AqualityServices;3import io.appium.java_client.HasOnScreenKeyboard;4import io.appium.java_client.android.nativekey.AndroidKey;5import io.appium.java_client.android.nativekey.KeyEvent;6import io.appium.java_client.android.nativekey.PressesKey;7public final class KeyboardUtils {8 private KeyboardUtils() {9 }10 public static boolean isKeyboardVisible() {11 return ((HasOnScreenKeyboard) AqualityServices.getApplication().getDriver()).isKeyboardShown();12 }13 public static void hideKeyboard() {14 AqualityServices.getApplication().getDriver().hideKeyboard();15 }16 public static void pressKey(AndroidKey androidKey) {17 ((PressesKey) AqualityServices.getApplication().getDriver()).pressKey(new KeyEvent(androidKey));18 }19}
HasOnScreenKeyboard
Using AI Code Generation
1import io.appium.java_client.AppiumDriver;2import io.appium.java_client.HasOnScreenKeyboard;3import io.appium.java_client.android.AndroidDriver;4import io.appium.java_client.ios.IOSDriver;5import java.net.URL;6import org.openqa.selenium.remote.CapabilityType;7import org.openqa.selenium.remote.DesiredCapabilities;8public class AppiumTest {9 public static void main(String[] args) throws Exception {10 DesiredCapabilities capabilities = new DesiredCapabilities();11 capabilities.setCapability("deviceName", "My Phone");12 capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");13 capabilities.setCapability(CapabilityType.VERSION, "4.4.2");14 capabilities.setCapability("platformName", "Android");15 capabilities.setCapability("appPackage", "com.android.calculator2");16 capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
HasOnScreenKeyboard
Using AI Code Generation
1if (driver instanceof HasOnScreenKeyboard) {2 ((HasOnScreenKeyboard) driver).isKeyboardShown();3}4if (driver instanceof HasTouchScreen) {5 ((HasTouchScreen) driver).getTouch();6}7if (driver instanceof HasSessionDetails) {8 ((HasSessionDetails) driver).getSessionDetails();9}10if (driver instanceof HasSettings) {11 ((HasSettings) driver).getSettings();12}13if (driver instanceof HasInputDevices) {14 ((HasInputDevices) driver).getKeyboard();15}16if (driver instanceof HasSessionDetails) {17 ((HasSessionDetails) driver).getSessionDetails();18}19if (driver instanceof HasSettings) {20 ((HasSettings) driver).getSettings();21}22if (driver instanceof HasInputDevices) {23 ((HasInputDevices) driver).getKeyboard();24}25if (driver instanceof HasSessionDetails) {26 ((HasSessionDetails) driver).getSessionDetails();27}28if (driver instanceof HasSettings) {29 ((HasSettings) driver).getSettings();30}31if (driver instanceof HasInputDevices) {32 ((HasInputDevices) driver).getKeyboard();33}34if (driver instanceof HasSessionDetails) {35 ((HasSessionDetails) driver).getSessionDetails();
HasOnScreenKeyboard
Using AI Code Generation
1HasOnScreenKeyboard keyboard = (HasOnScreenKeyboard) driver;2keyboard.hideKeyboard();3keyboard = driver.hide_keyboard()4var keyboard = driver.hideKeyboard();5keyboard = driver.HideKeyboard();6$keyboard = $driver->hideKeyboard();7keyboard = driver.HideKeyboard();8$keyboard = $driver->hideKeyboard();9keyboard = driver.HideKeyboard();10$keyboard = $driver->hideKeyboard();11keyboard = driver.HideKeyboard();12$keyboard = $driver->hideKeyboard();13keyboard = driver.HideKeyboard();
HasOnScreenKeyboard
Using AI Code Generation
1package appium;2import org.openqa.selenium.remote.DesiredCapabilities;3import org.openqa.selenium.support.ui.ExpectedConditions;4import org.openqa.selenium.support.ui.WebDriverWait;5import org.openqa.selenium.By;6import org.openqa.selenium.WebElement;7import io.appium.java_client.AppiumDriver;8import io.appium.java_client.MobileElement;9import io.appium.java_client.android.AndroidDriver;10import io.appium.java_client.android.AndroidKeyCode;11import io.appium.java_client.android.HasOnScreenKeyboard;12import java.net.URL;13import java.util.concurrent.TimeUnit;14public class appium {15 public static void main(String[] args) throws Exception {16 DesiredCapabilities capabilities = new DesiredCapabilities();17 capabilities.setCapability("deviceName", "emulator-5554");18 capabilities.setCapability("BROWSER_NAME", "Android");19 capabilities.setCapability("VERSION", "9.0"); 20 capabilities.setCapability("platformName", "Android");21 capabilities.setCapability("appPackage", "com.android.calculator2");22 capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
HasOnScreenKeyboard
Using AI Code Generation
1package appium;2import java.net.URL;3import java.util.concurrent.TimeUnit;4import org.openqa.selenium.By;5import org.openqa.selenium.remote.DesiredCapabilities;6import org.openqa.selenium.support.ui.ExpectedConditions;7import org.openqa.selenium.support.ui.WebDriverWait;8import io.appium.java_client.MobileElement;9import io.appium.java_client.android.AndroidDriver;10import io.appium.java_client.android.HasOnScreenKeyboard;11public class KeyboardCheck {12 public static void main(String[] args) throws Exception {13 DesiredCapabilities caps = new DesiredCapabilities();14 caps.setCapability("deviceName", "Samsung Galaxy S7");15 caps.setCapability("udid", "5200b1e6b0a3e3d3");16 caps.setCapability("platformName", "Android");17 caps.setCapability("platformVersion", "7.0");18 caps.setCapability("appPackage", "com.android.calculator2");19 caps.setCapability("appActivity", "com.android.calculator2.Calculator");20 caps.setCapability("noReset", true);21 caps.setCapability("automationName", "UiAutomator2");
HasOnScreenKeyboard
Using AI Code Generation
1package appium;2import java.net.MalformedURLException;3import java.net.URL;4import org.openqa.selenium.By;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.remote.DesiredCapabilities;7import io.appium.java_client.AppiumDriver;8import io.appium.java_client.HasOnScreenKeyboard;9import io.appium.java_client.android.AndroidDriver;10public class Keyboard {11 public static void main(String[] args) throws MalformedURLException, InterruptedException {12 DesiredCapabilities cap = new DesiredCapabilities();13 cap.setCapability("deviceName", "Redmi");14 cap.setCapability("udid", "d0d7e8e");15 cap.setCapability("platformName", "Android");16 cap.setCapability("platformVersion", "6.0.1");17 cap.setCapability("appPackage", "com.android.contacts");18 cap.setCapability("appActivity", "com.android.contacts.activities.PeopleActivity");
HasOnScreenKeyboard
Using AI Code Generation
1package com.appium;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.remote.DesiredCapabilities;5import org.openqa.selenium.support.ui.ExpectedConditions;6import org.openqa.selenium.support.ui.WebDriverWait;7import io.appium.java_client.android.AndroidDriver;8import io.appium.java_client.android.HasOnScreenKeyboard;9import io.appium.java_client.remote.MobileCapabilityType;10public class HasOnScreenKeyboardDemo {11 public static void main(String[] args) {12 DesiredCapabilities caps = new DesiredCapabilities();13 caps.setCapability("deviceName", "Pixel4Emulator");14 caps.setCapability("platformName", "Android");15 caps.setCapability("appPackage", "com.android.calculator2");16 caps.setCapability("appActivity", ".Calculator");17 caps.setCapability(MobileCapabilityType.NO_RESET, "true");18 WebDriver driver = new AndroidDriver<WebElement>(caps);19 System.out.println("Calculator is open");20 HasOnScreenKeyboard hasOnScreenKeyboard = (HasOnScreenKeyboard) driver;21 boolean isKeyboardShown = hasOnScreenKeyboard.isKeyboardShown();22 System.out.println("Is Hardware Keyboard Shown: " + isKeyboardShown);23 hasOnScreenKeyboard.hideKeyboard();24 isKeyboardShown = hasOnScreenKeyboard.isKeyboardShown();25 System.out.println("Is Hardware Keyboard Shown: " + isKeyboardShown);26 WebDriverWait wait = new WebDriverWait(driver, 10);27 wait.until(ExpectedConditions.visibilityOf(driver.findElementById("com.android.calculator2:id/digit_1")));28 driver.findElementById("com.android.calculator2:id/digit_1").click();29 wait.until(ExpectedConditions.visibilityOf(driver.findElementById("com.android.calculator2:id/d
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!!