iOS Webview UI 测试

Posted

技术标签:

【中文标题】iOS Webview UI 测试【英文标题】:iOS Webview UI Testing 【发布时间】:2018-06-26 20:27:23 【问题描述】:

在基于 ionic 框架的 ios 应用程序的 UI 测试期间无法访问 UI 元素。例如,我想点击带有“home Home”标签的按钮。

下面是我对显示元素层次结构的应用程序的调试说明。

Element subtree:

 →Application, 0x600000398bb0, pid: 18354, 0.0, 0.0, 414.0, 736.0,    label: 'FastlaneHelper'
  Window, 0x6000003998b0, Main Window, 0.0, 0.0, 414.0, 736.0
  Other, 0x600000398c80, traits: 8589934592, 0.0, 0.0, 414.0, 736.0
    Other, 0x600000398d50, traits: 8589934592, 0.0, 0.0, 414.0, 736.0
      WebView, 0x600000398e20, traits: 8589934592, 0.0, 0.0, 414.0, 736.0
        Other, 0x600000398ef0, traits: 35192962023424, 0.0, 0.0, 414.0, 736.0
          Other, 0x600000398fc0, 0.0, 0.0, 0.0, 0.0
            Other, 0x600000399090, 0.0, 0.0, 0.0, 0.0
              Other, 0x600000380750, traits: 146029019136, 0.0, 0.0, 414.0, 736.0
                Other, 0x600000399230, traits: 146029019136, 0.0, 0.0, 414.0, 736.0, label: 'Ionic App'
                  Other, 0x6000003994a0, traits: 146029019136, 0.0, 686.0, 414.0, 51.0
                    Button, 0x6000003997e0, traits: 146297454600, 0.0, 687.0, 138.0, 49.0, label: 'home Home'
                    Button, 0x600000399640, traits: 146297454592, 138.0, 687.0, 138.0, 49.0, label: 'information circle-outline About'
                    Button, 0x600000399710, traits: 146297454592, 276.0, 687.0, 138.0, 49.0, label: 'contacts outline Contact'
                  Other, 0x600000399570, traits: 146029019136, 0.0, 0.0, 414.0, 736.0, label: 'home Home, tab panel'
                    Other, 0x600000399300, traits: 146029019136, 90.0, 30.0, 234.0, 23.0
                      StaticText, 0x6000003993d0, traits: 146029019200, 183.0, 30.0, 48.0, 23.0, label: 'Home'
                    Other, 0x6000003809c0, traits: 146029084672, 16.0, 77.0, 382.0, 28.0, label: 'Welcome to Ionic!', value: 2
                      StaticText, 0x6000003808f0, traits: 146029084736, 16.0, 76.0, 190.0, 29.0, label: 'Welcome to Ionic!', value: 2
                    Other, 0x600000380b60, traits: 146029019136, 16.0, 119.0, 382.0, 36.0
                      StaticText, 0x600000380a90, traits: 146029019200, 16.0, 119.0, 368.0, 36.0, label: 'This starter project comes with simple tabs-based layout for apps that are going to primarily use a Tabbed UI.'
                    Other, 0x600000380680, traits: 146029019136, 16.0, 169.0, 382.0, 36.0
                      StaticText, 0x600000380820, traits: 146029019200, 16.0, 169.0, 117.0, 18.0, label: 'Take a look at the'
                      Other, 0x600000399160, traits: 146029019136, 133.0, 172.0, 84.0, 15.0
                        StaticText, 0x6000003805b0, traits: 146029019200, 132.0, 172.0, 85.0, 15.0, label: 'src/pages/'
                      StaticText, 0x600000399a50, traits: 146029019200, 16.0, 169.0, 377.0, 36.0, label: 'directory to add or change tabs, update any existing page or create new pages.'
    ScrollView, 0x60000039b790, traits: 8589934592, 0.0, 0.0, 414.0, 736.0
Window, 0x60000039b860, 0.0, 0.0, 414.0, 736.0
  StatusBar, 0x60000039b930, 0.0, 0.0, 414.0, 20.0
    Other, 0x60000039ba00, 0.0, 0.0, 414.0, 20.0
    Other, 0x60000039bad0, 0.0, 0.0, 414.0, 20.0
      Other, 0x60000039bc70, traits: 8388608, 6.0, 0.0, 39.0, 20.0
      Other, 0x60000039bd40, traits: 8388608, 50.0, 0.0, 14.0, 20.0, label: '3 of 3 Wi-Fi bars', value: SSID
      Other, 0x60000039be10, traits: 8389120, 181.0, 0.0, 56.0, 20.0, label: '12:32 AM'
      Other, 0x60000039bee0, traits: 8388608, 383.0, 0.0, 26.0, 20.0, label: '87% battery power'

我尝试通过以下方式访问元素但没有成功:

let app = XCUIApplication()
let tabs = app.tabBars
tabBars.buttons["Home"].tap()

let app = XCUIApplication()
let tabs = app.windows.tabBars
tabBars.buttons["Home"].tap()

let app = XCUIApplication()
let tabs = app.windows.webViews.tabBars
tabBars.buttons["Home"].tap()

甚至尝试从元素层次结构中包含“其他”元素,但测试失败。谁能帮助我们解决这个问题?

【问题讨论】:

【参考方案1】:

您不应该搜索标签栏,因为您的应用中没有标签栏。可能有一些视觉上类似于标签栏的东西,但它只是 html 内容。您应该查找 Web 视图,然后查找按钮或静态文本。比如:

let app = XCUIApplication()
let web = app.webViews
web.buttons["home Home"].tap()

【讨论】:

以上是关于iOS Webview UI 测试的主要内容,如果未能解决你的问题,请参考以下文章

Xcode UI 测试 - 如何在 WebView 中查找和点击()元素

Qt5.WebView.添加节点的测试代码

无法在 iOS Appium 测试(AWS Device Farm)上切换到 WebView 上下文

iOS-关于WebView在长时间使用下的卡顿现象测试

Xamarin表单:ios平台上的webview中视频未完全填充

Xamarin iOS WebView 为空白