【问题标题】:Unable to select WebView element after switching context in Appium在 Appium 中切换上下文后无法选择 WebView 元素
【发布时间】:2020-07-14 19:04:59
【问题描述】:

我在尝试测试 iOS 应用程序时遇到了 webdriverio/appium 的一个奇怪问题。该应用程序有一个带有标记为“刷新”的按钮的 WebView。当我尝试测试此按钮时,我收到“未显示”错误(请参阅下面的代码)。

it('should have the Refresh button', () => {
    WebViewScreen.waitForWebsiteLoaded();

    // Verify that Refresh button is displayed
    WebViewScreen.switchToContext(CONTEXT_REF.WEBVIEW);
    const button = $('button=Refresh');
    expect(button).toBeDisplayed();
    WebViewScreen.switchToContext(CONTEXT_REF.NATIVE);
});

请注意,在测试按钮之前,我将上下文切换到 WEBVIEW。查看 Appium 日志,查找按钮 ($('button=Refresh')) 的请求返回 404。尝试了不同的选择器:['button[data-test="refresh-button"]'],但没有成功。我什至放了一个 browser.debug() 并在 REPL 中查询 - 仍然是 404 - 虽然我可以在屏幕上实际看到按钮!

我知道我的 webdriverio/appium 设置很好,因为完全相同的代码适用于另一个带有 WebView 和按钮的 iOS 屏幕 - 唯一的区别是按钮名称。

我还通过转到独立网页并测试该按钮排除了我的选择器不正确的可能性 - 在这种情况下没有错误!这是独立的网页代码 - 唯一的区别是没有上下文切换调用:

it('should have the Refresh button', () => {
    browser.url('https://example.site.com/');

    // Verify that Refresh button is displayed
    const button = $('button=Refresh')
    expect(button).toBeDisplayed();
});

关于如何调试它的任何想法?切换到 WEBVIEW 上下文是否有我遗漏的任何细微差别?

【问题讨论】:

    标签: appium webdriver-io appium-ios


    【解决方案1】:

    终于在这方面取得了突破——感谢Alexi's suggestions on the Appium forum。事实证明,我的应用程序中有多个 WebView,我必须将上下文切换到正确的。诀窍是在 Appium 上设置 fullContextList 选项以获取有关可用上下文的更多信息,然后选择正确的。

    【讨论】:

      【解决方案2】:

      当你切换到常规网页视图时会发生什么

      public static void switch(AppiumDriver driver) {
          Set<String> contextNames = driver.getContextHandles();
          for (String context : contextNames) {
              log.debug("Context available:{}", context);
              if (StringUtils.startsWithIgnoreCase(context, "WEBVIEW")) {
                  log.debug("Switch to WEBVIEW");
                  driver.context(context);
      
              }
      

      【讨论】:

      • 您好 Johnny,请根据 Alexi 的建议查看我的回答。有多个 WebView,我必须确保选择正确的。
      猜你喜欢
      • 1970-01-01
      • 2022-10-19
      • 2015-09-24
      • 2018-11-06
      • 2017-07-19
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多