使用Appium和Python测试iOS应用程序时是否等待加载元素?

时间:2014-06-13 01:53:46

标签: python ios appium

我正在测试原生iOS应用,需要等待一些元素加载到我的一些测试中。 Appium在某些屏幕上的速度太快了。

有人可以指点我使用WebDriverWait样式等待Appium iOS测试吗? Ruby在这里回答了一个问题:Wait for element to load when testing an iOS app using Appium and Ruby?。在Python中寻找类似的东西。

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

感谢。

5 个答案:

答案 0 :(得分:8)

在测试的顶部导入这些。

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 =部分中将self.driver改为just 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()

大部分内容都是here解释的。

这是一个使用等待登录到Android应用程序的示例(Haven在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.driver打开浏览器。

 self.driver = webdriver.Firefox()

然后使用By选择器,例如class,name,id,例如以下示例。

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

article也有帮助。此外,如果您不知道如何找到xpath,请查看设置appium附带的uiautomatorviewer。

答案 1 :(得分:1)

python用法是:

driver.implicitly_wait(timeToWaitSec)

Selenium sourcecode(Py)

答案 2 :(得分:0)

您可以使用implicitWait。像remoteWebDriver.implicitlyWait(time, timeUnit)这样的东西当然是针对java的。类似的东西应该可用于python。

答案 3 :(得分:0)

您可以使用WaitForElement类:

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)

答案 4 :(得分:0)

您可以在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)))