这是我第一次使用 playwright,我不知道如何等待请求并验证响应。我已经使用 cypress 很长时间了,管理网络请求非常容易。例如,我需要在单击按钮后验证响应,这就是我使用 cypress 执行此操作的方法:
cy.server()
cy.route('POST', '/api/contacts').as('newContact')
cy.get('.btn').click()
cy.wait('@newContact').then((response) => {
expect(response.status).to.eq(400)
expect(response.responseBody.data.name).to.eq('abcde')
})
Run Code Online (Sandbox Code Playgroud)
这就是我尝试对剧作家做同样的事情的方式,但它会验证GET
在单击“保存”按钮之前很久就发送的请求。我不知道如何正确管理这个请求,这对我的测试套件来说是一个阻碍:
await contacts.clickSaveBtn()
await page.waitForResponse((resp) => {
resp.url().includes('/api/contacts')
expect(resp.status()).toBe(400)
})
Run Code Online (Sandbox Code Playgroud)
任何帮助或建议将非常感激
我正在使用 Playwright 1.15.2 进行测试并面临元素可见性的问题。我想检查模式在屏幕上是否可见,以便我可以将其关闭。情态动词以display:none
开头并变为display:block
。此外,模式会通知表单数据不正确,因此它可能会出现,也可能不会出现(即我不能waitForSelector
)。
目前,我有类似以下的代码:
const myModal = await page.$("#modal");
if (await myModal.isVisible()) {
await page.waitForSelector('#modal > .modal-dialog > .modal-content > .modal-footer > .btn-close');
await page.click('#modal > .modal-dialog > .modal-content > .modal-footer > .btn-close');
}
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
const myModal = await page.$("#modal:visible");
if (myModal) {
...
Run Code Online (Sandbox Code Playgroud)
使用page.$("text=modal title >> visible=true")
或切换page.$
到page.locator
(使用所有上述选择器)也不起作用。
这个问题的公认答案效果不佳。
有人能帮我吗?
我正在尝试使用 Python 和 Playwright 自动抓取具有“无限滚动”功能的网站。
问题是,到目前为止,Playwright 还不包含滚动功能,更不用说无限自动滚动功能了。
根据我在网上发现的内容和我的个人测试,我可以使用该page.evaluate()
函数和一些 Javascript 代码自动执行无限或有限滚动。
例如,这有效:
for i in range(20):
page.evaluate('var div = document.getElementsByClassName("comment-container")[0];div.scrollTop = div.scrollHeight')
page.wait_for_timeout(500)
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于,它要么通过指定滚动数量,要么通过告诉它永远循环下去来工作while True
。
我需要找到一种方法来告诉它继续滚动,直到加载最终内容。
这是我目前正在尝试的 Javascript page.evaluate()
:
var intervalID = setInterval(function() {
var scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;
console.log('fail')
}, 1000);
var anotherID = setInterval(function() {
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
clearInterval(intervalID);
}}, 1000)
Run Code Online (Sandbox Code Playgroud)
这在我的 Firefox 浏览器或 Playwright 的 Firefox 浏览器中都不起作用。它立即返回并且不会间隔执行代码。
如果有人能告诉我如何使用 Playwright 创建一个自动滚动功能,该功能将在到达动态加载网页的底部时检测并停止,我将不胜感激。
我们在量角器中有这样的: browser.driver.manage().window().setSize( width - 30, height );
剧作家如何实现这一目标?
automated-tests ui-automation typescript playwright playwright-test
我试图弄清楚如何检查 Playwright 中的页面是否已完全加载。await page.waitForLoadState('networkidle');
在 Javascript 密集的网站上并不总是适合我。我采取了截屏的方法base64
,等待 100 毫秒,截取新的屏幕截图,然后比较它们是否相同。然而这似乎并不理想,有什么办法可以询问Playwright最后一个动画帧何时重绘?
我想知道如何通过 ID 访问元素。我想提交一份表格。
await page.click("id= 'next'");
--> 不可能await page.getByRole('button', { id: 'next' }).click();
--> 无法编译await page.getByRole('button', { name: 'Sign in' }).click();
--> 可以工作,但依赖于语言对我来说,通过 ID 选择元素似乎是最可靠的。我错过了什么吗?
在 Protractor 中,我当前在命令行中传递一个标志,指示我正在测试的应用程序的 URL。现在我们要转向剧作家,我想做类似的事情。由于相同的测试将用于在不同的环境(开发、测试、CI)中测试应用程序,因此我需要一种传递不同 URL 的方法,理想情况下,如果我可以通过命令行控制它,那就太好了。
我正在使用 Playwright 和 JavaScript 测试 UI。我的代码找到一个输入元素,有时可以是下拉菜单,有时是文本,有时是日期。为了解决这个问题,我分两步输入值。首先,我填充文本,然后单击 Tab 键来调用格式化元素中的值的 JavaScript。
await page.fill("#myID", inputText);
await page.keyboard.press('Tab'); // this line trigger the JS
// continue to the next element
Run Code Online (Sandbox Code Playgroud)
问题是,它没有等待 JavaScript 完成。我怎样才能等待 JS 完成然后代码继续。
我正在使用 jest-playwright 库(https://github.com/playwright-community/jest-playwright)来执行端到端测试。在 jest.config.js 文件中,您可以设置一个选项来忽略 SSL 错误:
contextOptions: {
ignoreHTTPSErrors: true,
}
Run Code Online (Sandbox Code Playgroud)
当用 jest 运行测试时,这工作得很好。
现在,我希望剧作家在使用命令单击网站元素时生成代码npx playwright codegen example.com
。然而,剧作家在打开网站时由于 SSL 错误而停止。
使用剧作家代码生成时是否可以选择忽略 SSL 错误?
我将 Playwright 与 Nodejs 一起使用,并且将几个测试分组在一起,如下所示
import { test, expect } from '@playwright/test';
test.describe('Add a simple invoice test', () => {
test('01.Login & add an invoice', async ({ page }) => {
await page.goto("https://someUrl.com");
await page.fill('input[id="email"]', "someEmailAddress");
await page.fill('input[ng-model="ctrl.user.password"]', "somePassword");
await page.click('button[id="login-btn"]');
});
test('02.Add an invoice', async ({ page }) => {
await page.click('[name="invoice"]');
await page.click('button[id="addInvoiceButton"]');
await page.click('a[ng-click="ctrl.goToAddInvoice()"]');
await page.fill('#invoiceTitle', Math.random().toString(36).substring(7));
await page.fill('#exampleInputAmount', "120");
await page.click("#invoiceCategory")
await page.fill("#invoiceCategory > input", "Car")
await page.keyboard.press("Enter");
await page.click('button[id="submitInvoiceButton"]');
});
});
Run Code Online (Sandbox Code Playgroud)
问题是这两个测试并行运行,而 02 依赖于 01,因为需要登录。 …
playwright ×10
javascript ×4
typescript ×2
end-to-end ×1
jestjs ×1
node.js ×1
puppeteer ×1
python ×1
python-3.x ×1
ssl ×1