在剧作家测试中切换标签

时间:2020-10-14 07:24:22

标签: javascript playwright

我正在尝试使用剧本测试在选项卡之间切换 但它没有控制Windows元素。 我们是否有任何类似于剧本中的selenium driver.switchto()。window()的方法?

const { chromium } = require('playwright');
(async () => {
    const browser = await chromium.launch({ headless: false, args: ['--start-maximized'] });
    const context = await browser.newContext({ viewport: null });
    context.on("page", async newPage => {
        console.log("***newPage***", await newPage.title())
    })
    const page = await context.newPage()
    const navigationPromise = page.waitForNavigation()

    // dummy url
    await page.goto('https://www.myapp.com/')
    await navigationPromise

    // User login
    await page.waitForSelector('#username-in')
    await page.fill('#username-in', 'username')
    await page.fill('#password-in', 'password')
    await page.click('//button[contains(text(),"Sign In")]')
    await navigationPromise

    // User lands in application home page and clicks on link in dashboard 
    // link will open another application in new tab 
    await page.click('(//span[text()="launch-app-from-dashboard"])[2]')

    await navigationPromise
    await page.context()
    // Waiting for element to appear in new tab and click on ok button
    await page.waitForTimeout(6000)
    await page.waitForSelector('//bdi[text()="OK"]')
    await page.click('//bdi[text()="OK"]')

})()

2 个答案:

答案 0 :(得分:0)

假设 cid pos new_pos 0 11 29 3 1 22 29 1 2 22 29 1 3 33 29 4 4 44 29 4 正在创建新页面标签,则可以使用以下模式在新页面上运行后续代码行。有关更多示例,请参见multi-page scenarios doc

"launch-app-from-dashboard"

由于无头运行,因此在浏览器中使用// Get page after a specific action (e.g. clicking a link) const [newPage] = await Promise.all([ context.waitForEvent('page'), page.click('a[target="_blank"]') // Opens a new tab ]) await newPage.waitForLoadState(); console.log(await newPage.title()); docs)切换可见选项卡也很有用。

答案 1 :(得分:0)

`it('Open a new tab and check the title', async function () {
    await page.click(button, { button: "middle" }); //to open an another tab
    await page.waitForTimeout(); // wait for page loading 
    let pages = await context.pages();
    expect(await pages[1].title()).equal('Title'); /to compare the title of the second page`