【Playwright】playwright标签页切换

ui 自动化需要跳转新的 tab 页

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
async addMonitorEnterprise() {
    const pagePromise = this.page.context().waitForEvent('page')
    await this.searchInput.focus()
    await this.searchInput.fill('乐视')
    await expect(this.hisEntList).toBeVisible({})
    await this.hisEntItem.first().click()
    // 等待新页面跳转并获取新的页面
    const newPage = await pagePromise
    // 使用新页面的定位器
    const addMonitorButton = newPage
      .locator('.info-basic-box')
      .getByRole('button', {
        name: ' 添加监控',
      })
    await expect(addMonitorButton).toBeVisible()
    await addMonitorButton.click()
  }

注意点

  • 使用page.context().waitForEvent()得到新页面的 promise
  • 等待自动化代码触发打开新页面
  • await 这个 promise 获得新页面
  • 使用新页面的对象调用方法
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
使用 Hugo 构建
主题 StackJimmy 设计