Search code examples
javaiosxpathappium

How to use a if/else to click on IOS button if it appears on the Screen


Hi every time the app is updated to a new version it should show a message about the terms that the user has to accept before using the app. But if he already accepted the message in a previous version it won't show up anymore. screenshot

I tried to do a restriction like this:

Boolean Allow = driver.findElements(By.xpath("//[@id=‘Allow’]")).size()<0;
if (Allow.TRUE) {
new WebDriverWait(driver, 30, esperandogif).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//[@text=‘Allow’]"))).click();
else {
driver.findElement(By.xpath("//[@text=‘Cadastre-se com seu e-mail pessoal’]")).click();
driver.findElement(By.xpath("//[@placeholder=‘Email’]")).sendKeys("[email protected]");
driver.swipe(165, 759, 175, 534, 1234);
driver.findElement(By.xpath("//[@placeholder=‘Senha’]")).sendKeys(“lucasmoreira”);
driver.findElement(By.xpath("//[@placeholder=‘Confirmer Senha’]")).sendKeys(“lucazmoreira”);
driver.swipe(309, 856, 390, 406, 888);
driver.findElement(By.xpath("//[@placeholder=‘Nome e Sobrenome’]")).sendKeys(“teste”);
driver.findElement(By.xpath("//[@placeholder=‘CPF’]")).sendKeys(“12345678901”);
driver.swipe(168, 946, 334, 615, 2240);
driver.findElement(By.xpath("//[@placeholder=‘Data de Nascimento (dd/mm/aaaa)’]")).click();
try{Thread.sleep(threadSleep);} catch(Exception ignore){}
//driver.findElement(By.xpath("//[@text=‘2018’]")).click();
// driver.findElement(By.xpath("//[@text=‘2020’]")).click();
driver.findElement(By.xpath("//[@id=‘Feito’]")).click();
driver.swipe(84, 887, 259, 493, 1461);
driver.findElement(By.xpath("//[@placeholder=‘Telefone’]")).sendKeys(“99999999999”);
driver.swipe(450, 812, 500, 640, 1292);
driver.findElement(By.xpath("//[@id=‘Cadastrar’]")).click();
System.out.println(“report URL : " + driver.getCapabilities().getCapability(“reportUrl”));
driver.quit();
}

But it’s not working. If it doesn’t show up on the screen the code fails and it doesn’t click on driver.findElement(By.xpath(”//*[@text=‘Cadastre-se com seu e-mail pessoal’]")).click(); How can I do that restriction?


Solution

  • Your comparison is turned the wrong way. First line should be greater than 0, not less than.

    Boolean Allow = driver.findElements(By.xpath("//[@id=‘Allow’]")).size()>0;
    

    You should probably also find by id since it's available to you.

    Boolean Allow = driver.findElements(By.id("Allow")).size()>0;
    

    I write such statements with isEmpty() and find them a bit more readable and not prone to this error:

    Boolean Allow = !driver.findElementsById("Allow")).isEmpty();