What Are Some Best Practices When Asserting iOS Elements Are Displayed?

烂漫一生 提交于 2020-01-05 07:04:38

问题


I'm trying to write my first UI Automation test for an iOS app with Appium/Python.

I find that when I list 10 assertions like the one below, I get very inconsistent results ... sometimes it passes, but it usually fails the third assertion, sometimes it fails the eighth.

assert driver.find_element_by_name('Settings').is_displayed()

I've also tried to use waits:

driver.wait_for_element_by_name_to_display('Settings')
assert driver.find_element_by_name('Settings').is_displayed()

Does anyone know of any good resources on this issue? Any tips or advice?


回答1:


I don't know python code, i am showing how i am doing it in java. Hope you can convert it in python code.

Create a method like following:

public boolean isElementDisplayed(MobileElement el){
     try{
        return el.isDisplayed();
     }catch(Exception e){
        return false;
     }
}

Then you can check if the element is displayed by calling above method:

MobileElement element = driver.findElementById('element id');
boolean isElementVisible = isElementDisplayed(element);
if(isElementVisible){
   //element is visible
}else{
   //element is not visible
}

If you don't use try catch, then the exception will be thrown when element is not found.




回答2:


There is a good util class that can be used for this EC. Hereès the link to the git documentation

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Then you can use it this way to detect if an element is present:

from appium.webdriver.common.mobileby import MobileBy
# time in seconds
timeout = 10
wait = WebDriverWait(driver, timeout)
wait.until(EC.presence_of_element_located((MobileBy.NAME, 'Settings'))

If you need to detect present and visible use:

wait.until(EC.visibility_of_any_elements_located((MobileBy.NAME, 'Settings'))



回答3:


You can wait until target element located as below.

https://github.com/appium/python-client/blob/6cc1e144289ef3ee1d3cbb96ccdc0e687d179cac/test/functional/android/helper/test_helper.py

Example:

from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

TIMEOUT = 3
WebDriverWait(self.driver, TIMEOUT).until(
    EC.presence_of_element_located((MobileBy.ACCESSIBILITY_ID, 'Text'))
)


来源:https://stackoverflow.com/questions/56008481/what-are-some-best-practices-when-asserting-ios-elements-are-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!