为什么剧作家这么早就关闭我的浏览器窗口?

问题描述 投票:0回答:1
我没有很多经验,但是开始使用剧作家,它可以使用暂停方法手动运行,但是当我运行它时,它会迅速关闭浏览器。值得注意的是,我手动进行操作时确实需要时间。知道我该怎么办?

const { test, expect } = require('@playwright/test'); test('has title', async ({ page }) => { //Opens zillow and log the user in await page.goto('https://www.zillow.com'); // await page.pause(); // //It closes the google popup await page.frameLocator('iframe[title="Sign in with Google Dialog"]').getByLabel('Close').click(); await page.getByRole('link', { name: 'Sign In' }).click(); //Add your email and password to the fill strings await page.getByPlaceholder('Enter email').fill("NEED EMAIL"); await page.getByPlaceholder('Enter password').fill("NEED PASS"); await page.getByRole('button', {name: 'Sign in'}).click(); await page.waitForTimeout(2000); //Searchbar fill await page.getByRole('button', { name: 'Got it!' }).click(); await page.getByPlaceholder('Enter an address, neighborhood, city, or ZIP code').fill("Chattanooga"); await page.waitForTimeout(2000); await page.getByRole('button', {name: 'Submit Search'}).click(); await page.waitForTimeout(2000); // await page.pause(); const ulElement = await page.$$('.List-c11n-8-84-3__sc-1smrmqp-0.StyledSearchListWrapper-srp__sc-1ieen0c-0.doa-doM.fgiidE.photo-cards.photo-cards_extra-attribution li'); if (ulElement.length > 0) { // Process each li element in the array for (const liElement of ulElement) { const textContent = await page.evaluate(li => li.textContent, liElement); if (textContent?.includes('37421')) { const button = await liElement.$('button'); if (button) { await button.click(); console.log('Text content of li element:', textContent); } } } } else { console.log('No li elements found inside the ul with role="list".'); } });

我尝试添加诸如等待页面之类的超时。但是它不起作用

javascript automation playwright
1个回答
1
投票
Tobevisible
验证登录/提交状态 tobobsible是一个locotator的断言
,这意味着它将等待和 重试目标,以达到预期状态,直到超时,但 如果物体出现早期出现,它的同时也足够灵活 与硬编码等待不同。

注:您可以根据情况调整超时参数值。 使用示例

// A specific element is visible. await expect(page.getByText('Welcome'+ userName)).toBeVisible({timeout:5000}) // At least one item in the list is visible. await expect(page.getByTestId('todo-item').first()).toBeVisible() // At least one of the two elements is visible, possibly both. await expect( page.getByRole('button', { name: 'Sign in' }) .or(page.getByRole('button', { name: 'Sign up' })) .first() ).toBeVisible()
ful更多指针:

不使用硬编码等待,而是使用loscators sovertions

,因为它们是动态的,不会不必要地等待。

不要使用$$识别对象,使用

插管器
作为动态,因此您不会陷入“陈旧元素参考错误”之类的问题(如果您来自Selenium World,您会理解它!)
不使用“长度”之类的JS级别属性,而是使用playwright API方法(例如
count

)获得更可靠和一致的结果。

    也请参见:
  1. 如何防止浏览器在JavaScript中的剧作家测试中运行代码时关闭浏览器
  2. 浏览器窗口在剧作家测试中过早关闭 为什么剧作家这么早关闭页面?
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.