当使用Appium和Python测试iOS应用时,等待元素加载



我正在测试一个原生iOS应用,需要等待一些元素在我的一些测试中加载。阿哌啶现在在某些屏幕上显示得太快了。

有人能告诉我使用WebDriverWait等待Appium iOS测试的风格的一个例子吗?这里有一个关于Ruby的问题:在使用Appium和Ruby测试iOS应用程序时,等待元素加载?在Python中寻找类似的东西。

Python客户端文档似乎没有记录等待函数。

谢谢。

在测试的顶部导入这些。

from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

TL/DR这应该工作,你可能需要改变自我。在wait =部分中,Driver变成了Driver,这取决于你是怎么做的。当然,要改变By。XPath到您正在使用的任何定位器:

wait = WebDriverWait(self.driver, 20)
currently_waiting_for = wait.until(EC.element_to_be_clickable((By.XPATH,'//UIAApplication[1]/UIAWindow[1]/UIAButton[@text="example text"]')))

或者你可以直接告诉驱动程序使用隐式等待。

self.driver.implicitly_wait(10)
myElement = driver.find_element_by_id("fakeid")
myElement.click()

这里解释了大部分内容。

这里有一个使用等待登录到Android应用程序的例子(没有在ios上使用它,但应该是类似的)使用默认帐户选择器,然后断言正确的文本出现。在安装过程中,我从另一个文件加载所需的功能。

class TrainUpSmokeTests(unittest.TestCase): 
    def setUp(self):
         desired_caps = desired_capabilities.get_desired_capabilities('app-debug.apk')
         self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
    def tearDown(self):
        self.driver.quit()
    def example_test(self):
        wd = self.driver
        ## Depending on how you're running the test the first variable may just be driver. Or in my case self.driver which I shortened above. The number is how many seconds it should wait before timing out. 
        wait = WebDriverWait(wd, 20)
        ## Waiting for account selector to be clickable.
        currently_waiting_for = wait.until(EC.element_to_be_clickable((By.XPATH,'//android.widget.CheckedTextView[@text="FakeEmail@example.com"]')))
        ## Locating test account and selecting it.
        account = wd.find_element_by_android_uiautomator('text("FakeEmail@example.com")')
        account.click()
        ok_button = wd.find_element_by_android_uiautomator('text("OK")')
        ok_button.click()
        ## Waiting for an Element on the home screen to be locatable.
        currently_waiting_for = wait.until(EC.presence_of_element_located((By.XPATH,'//android.widget.RelativeLayout[@resource-id="com.name.app:id/overview"]')))
        hero_headline = wd.find_element_by_android_uiautomator('new UiSelector().description("Example Header")')
        self.assertIsNotNone(hero_headline)
if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase(TrainUpSmokeTests)
unittest.TextTestRunner(verbosity=2).run(suite)

这是你使用appium(而不是应用程序)测试一个网站。将设置更改为self。驱动程序打开浏览器

 self.driver = webdriver.Firefox()

然后使用By选择器,如class, name, id,例如下面的例子。

currently_waiting_for = wait.until(EC.element_to_be_clickable((By.CLASS_NAME,'search-results')))

这篇文章也有帮助。此外,如果您不知道如何查找xpath,请考虑设置appium附带的uiautomatorviewer。

python的用法是:

driver.implicitly_wait(timeToWaitSec)

硒源代码里(Py)

您可以使用waitforeelement类:

class WaitForElement:
    @staticmethod
    def wait(driver, id, time_out=100):
        try:
            WebDriverWait(driver, time_out).until(
                lambda driver: driver.find_element(*id))
        except TimeoutException:
            print('Not able to find ID:' + id)

你可以在selenium.webdriver.support.expected_conditions找到你需要的一切

然后你可以这样做:

def wait_for_element_visible(self, by=By.XPATH, value=None, text=None, wait_time=20):
    if text is not None:
        value = value % text
    wait = WebDriverWait(self.driver, wait_time)
    return wait.until(EC.visibility_of_element_located((by, value)))

可以使用implicitWait。比如remoteWebDriver.implicitlyWait(time, timeUnit),这当然是java的。python应该有类似的功能

最新更新