Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 剧作家测试中的切换标签_Javascript_Playwright - Fatal编程技术网

Javascript 剧作家测试中的切换标签

Javascript 剧作家测试中的切换标签,javascript,playwright,Javascript,Playwright,我正在尝试使用剧作家测试在标签之间切换 但它并没有控制windows元素。 我们有没有类似于剧作家中selenium driver.switchto().window()的方法 const { chromium } = require('playwright'); (async () => { const browser = await chromium.launch({ headless: false, args: ['--start-maximized'] }); co

我正在尝试使用剧作家测试在标签之间切换 但它并没有控制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"]')

})()
假设
“从仪表板启动应用程序”
正在创建新的页面标记,您可以使用以下模式在新页面上运行后续代码行。有关更多示例,请参见

//在特定操作(例如单击链接)后获取页面
const[newPage]=等待承诺([
context.waitForEvent('page'),
页面。单击('a[target=“\u blank”]”)//打开一个新选项卡
])
等待newPage.waitForLoadState();
log(等待newPage.title());

由于您运行headless,因此在浏览器中使用
页面切换可见选项卡也可能很有用。bringToFront
()。

我想我在这里的回答会有所帮助
`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`