I am using appium to automate the testing of our iOs app. Tests are written in Java.
I am trying to check if an element is present. If yes, I want to click it, if not proceed with the rest of the test without failing my tests.
For example, when I am trying to launch the map, the first time I access the map feature, i get a pop-up if I want to let my app use Location services. This does not come up every time, but only the first time I try to use maps.
So if there is this pop-up, I want to click “ok”, else proceed with the rest of the tests. how do I do this?
If I check using appium.findElement(By.xpath(path)), it times out if the element is not present.
Replace “using” with the text you are looking for.
Please note you may want to add some delay or additional logic, before calling this method to make sure you don’t run this code before that element is displayed.
RamS solution is good. Basically you count the number of elements corresponding to your search criteria and check if it’s 0 or 1. Of course, try to identify an unique criteria.
Thanks a lot!! your proposed solution totally worked for me. I somehow missed this and was trying the findElement(), which used to fail if element is not found!
Not very critical, but do let me know if there is a way to reduce the default time until which findElements() keeps looking for the element.
@Hassan_Radi Hey, not sure if it’s the best idea about 10000, don’t forget that timeout is in second.
* @param driver The WebDriver instance to pass to the expected conditions
* @param timeOutInSeconds The timeout in seconds when an expectation is called
* @see WebDriverWait#ignoring(java.lang.Class)
*/
public WebDriverWait(WebDriver driver, long timeOutInSeconds)
Hi,
I have a list of 5 elements. While clicking on first element and performing some changes to first element and after saving first element the second element is deleted automatically.
How to verify that second element is present or not because issue is “id” of all element is same .
while verifying through id it is giving me element is present.
Thanks.
Can some one help me out for my if else condition
like i am testing an app for location related listing
I have set location and there is a message which can be said as location is not valid
and then again if i have set the location invalid it should continue until the location is valid
like
if (popupdisplayed) is met
perform the set location
else
location is valid
if then too after location is set
but still popup is displayed
then i have to run again the step to set location
Hi, in your case you can use fluent wait which in my opinion is very useful when you know how to use it.
private FluentWait<AppiumDriver> fluentWait(MobileElement locator, int time) {
return new FluentWait<>(AppiumController.instance.driver)
.withTimeout(Duration.ofSeconds(time))
.pollingEvery(POLLING_TIMEOUT) //Set query/check/control interval
.ignoring(NoSuchElementException.class)
.withMessage(locator + "is not displayed");
}
and you can have 2 methods, one is only waiting until an element is clickable and another one is returning a boolean value if an element is displayed after a defined time
public void waitForElement(MobileElement locator, int time) {
try {
logger.info("Checking the " + locator + " having " + time + " seconds limit");
fluentWait(locator, time).until(ExpectedConditions.elementToBeClickable(locator));
} catch (TimeoutException | NoSuchElementException e) {
logger.debug("The " + locator + " isn't clickable after timeout");
}
}
public boolean isElementDisplayed(MobileElement locator, int time) {
boolean status = true;
try {
logger.info("Checking the " + locator + " having " + time + " seconds limit");
fluentWait(locator, time).until(ExpectedConditions.elementToBeClickable(locator));
} catch (TimeoutException e) {
status = false;
}
return status;
}