Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用Appium和Python测试iOS应用程序时,是否等待元素加载?_Python_Ios_Appium - Fatal编程技术网

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

在使用Appium和Python测试iOS应用程序时,是否等待元素加载?,python,ios,appium,Python,Ios,Appium,我正在测试一个本机iOS应用程序,需要等待在一些测试中加载一些元素。Appium现在在一些屏幕上运行得太快了 有人能给我举一个使用WebDriverWait风格等待Appium iOS测试的例子吗?在这里,Ruby回答了一个问题:。在Python中寻找类似的东西 Python客户端文档似乎没有记录等待函数 谢谢。您可以使用隐式等待。类似于remoteWebDriver.implicitlyWait(时间,时间单位)这当然是针对java的。python也应该有类似的功能。python的用法是: d

我正在测试一个本机iOS应用程序,需要等待在一些测试中加载一些元素。Appium现在在一些屏幕上运行得太快了

有人能给我举一个使用WebDriverWait风格等待Appium iOS测试的例子吗?在这里,Ruby回答了一个问题:。在Python中寻找类似的东西

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


谢谢。

您可以使用隐式等待。类似于
remoteWebDriver.implicitlyWait(时间,时间单位)
这当然是针对java的。python也应该有类似的功能。

python的用法是:

driver.implicitly_wait(timeToWaitSec)

在测试的顶部导入这些

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()
这其中大部分都得到了解释

下面是一个使用默认帐户选择器使用wait登录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.driver打开浏览器

 self.driver = webdriver.Firefox()
然后由诸如class、name、id之类的选择器使用,例如下面的示例

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

这也有帮助。另外,如果您不知道如何查找XPath,请查看appium附带的uiautomatorviewer的设置

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

您可以在
selenium.webdriver.support.expected\u条件中找到所需的一切

然后,您可以从中执行以下操作:

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)))