【问题标题】:What Are Some Best Practices When Asserting iOS Elements Are Displayed?显示断言 iOS 元素时有哪些最佳实践?
【发布时间】:2019-09-24 06:47:41
【问题描述】:

我正在尝试使用 Appium/Python 为 iOS 应用编写我的第一个 UI 自动化测试。

我发现当我列出如下 10 个断言时,我得到的结果非常不一致……有时它通过了,但通常第三个断言失败,有时第八个断言失败。

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

我也尝试过使用等待:

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

有没有人知道关于这个问题的任何好的资源?有什么提示或建议吗?

【问题讨论】:

    标签: python appium qa appium-ios python-appium


    【解决方案1】:

    我不知道 python 代码,我正在展示我是如何在 java 中做到这一点的。希望你能把它转换成python代码。

    创建如下方法:

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

    然后你可以通过调用上述方法检查元素是否显示:

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

    如果不使用try catch,则在找不到元素时会抛出异常。

    【讨论】:

      【解决方案2】:

      有一个很好的 util 类可以用于这个 EC。这是git documentation的链接

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

      然后你可以用这种方式来检测一个元素是否存在:

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

      如果您需要检测当前和可见的使用:

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

      【讨论】:

        【解决方案3】:

        您可以等到目标元素位于如下。

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

        例子:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-04-16
          相关资源
          最近更新 更多