我对剧作家很陌生。由于我的测试套件,我需要在运行每个测试之前登录我的应用程序。在一个简单的规范文件中,我可以简单地调用test.beforeEach. 我的问题是:我需要在每个规范文件的每次测试之前登录。
test.describe('Test', () => {
//I need to make the code inside this beforeEach a exported
//function to call inside the before each of every spec file I have
test.beforeEach(async ({ page }) => {
await page.goto('/login');
await page.click('text=Log in with Google account');
await page.fill('id=identifierId', LoginAutomationCredentials.USER);
await page.click('button[jsname="LgbsSe"]');
await page.fill('input[type="password"]', LoginAutomationCredentials.PASSWORD);
await page.click('button[jsname="LgbsSe"]');
const otp = authenticator.generateToken(LoginAutomationCredentials.TOKEN);
await page.fill('id=totpPin', otp);
await page.click('button[jsname="LgbsSe"]');
});
it('Some description', async ({ page }) => {
await page.goto('/foo');
const dateFilter = await …Run Code Online (Sandbox Code Playgroud) javascript automated-tests typescript playwright playwright-test
当我编写要在 playwright 中运行的测试时,我希望能够设置浏览器认为测试开始的日期。有没有办法使用剧作家来实现这一点?
我正在尝试使用 Playwright 迭代动态元素列表,我已经尝试了一些方法,但没有一个有效:
await this.page.locator('li').click();
const elements = await this.page.locator('ul > li');
await elements.click()
Run Code Online (Sandbox Code Playgroud)
await this.page.$$('ul > li').click();
Run Code Online (Sandbox Code Playgroud)
await this.page.click('ul > li');
Run Code Online (Sandbox Code Playgroud)
const divCounts = await elements.evaluateAll(async (divs) => await divs.click());
Run Code Online (Sandbox Code Playgroud)
this.page.click('ul > li > i.red', { strict: false, clickCount: 1 },)
Run Code Online (Sandbox Code Playgroud)
const elements = await this.page.$$('ul > li > i.red')
elements.forEach(async value => {
console.log(value)
await this.page.click('ul > li > i.red', { strict: false, clickCount: 1 },)
await value.click();
})
Run Code Online (Sandbox Code Playgroud)
我已经使用 TypeScript 编写了 Playwright 测试,并且想在测试文件中使用 .env 中的变量,我该怎么做?
就像在 selenium 中一样,我们在 Playwright 中是否可以选择等待元素可点击?
headers["user-agent"] = fakeUa();
console.log(fakeUa())
let firstReq = true;
page.route('**/*', route => {
const request = route.request()
//console.log(request.url(), JSON.stringify(request.headers()));
if("x-j3popqvx-a" in request.headers()){
headers = request.headers();
//console.log(headers);
console.log("exiting");
return;
}
else {
console.log("in");
return route.continue({headers: headers});
}
});
let pageRes = await page.goto(url, {waitUntil: 'load', timeout: 0});
Run Code Online (Sandbox Code Playgroud)
我想在向 url 发送请求时添加假用户代理。但它不会添加假用户代理,而是使用默认用户代理。
我需要一些帮助来使用剧作家测试做出条件语句。
我有一个给定的选择器,比如说一个按钮,我需要编写如下的条件语句:
if (selector is not present/visible)
do nothing and proceed with the *next lines of code*
else
await page.click(*selector*)
*next lines of code*
Run Code Online (Sandbox Code Playgroud)
有没有有效的方法来做这些事情?我在剧作家文档中没有找到任何内容。
我试图等待一个指示页面仍在加载并且多次存在于页面上的元素不可见(想想带有加载数据占位符的表)。
Playwright 文档建议使用定位器是最佳实践,因此我最初尝试通过执行以下操作来实现此目的:
locator.waitFor({state: "hidden")
Run Code Online (Sandbox Code Playgroud)
然而,由于定位器非常严格并且只允许匹配一个元素,所以会出现错误。
我现在用下面的代码来做:
page.waitForSelector(".foo .bar", {state: "hidden"})
Run Code Online (Sandbox Code Playgroud)
由于以下几个原因,这并不理想:
有什么办法可以关闭定位器的严格约束吗?或者使用定位器来实现此目的的方法。我知道您可以.count在匹配多个元素的定位器上执行此操作,但我还没有找到将其与等待计数为 0 结合起来的好方法。
我正在寻找针对当前情况的更简单的解决方案。例如,您打开 google(任何其他网站),并且希望通过单击按钮(例如 Gmail)- 使用 Playwright 在新选项卡中打开此页面。
let browser, page, context;
describe('Check the main page view', function () {
before(async () => {
for (const browserType of ['chromium']) {
browser = await playwright[browserType].launch({headless: false});
context = await browser.newContext();
page = await context.newPage();
await page.goto(baseUrl);
}
});
after(async function () {
browser.close();
});
await page.click(tax);
const taxPage = await page.getAttribute(taxAccount, 'href');
const [newPage] = await Promise.all([
context.waitForEvent('page'),
page.evaluate((taxPage) => window.open(taxPage, '_blank'), taxPage)]);
await newPage.waitForLoadState();
console.log(await newPage.title());
Run Code Online (Sandbox Code Playgroud) 我正在通过 Playwright 上的非输入 HTML 标签上传文件。
例如,您可以setInputFiles像这样使用,并且效果如下:
await page.setInputFiles('input[type="file"]', './headphone.png')
Run Code Online (Sandbox Code Playgroud)
但显然setInputFiles只适用于输入元素,这样的事情会出错:
await page.setInputFiles('label.ImageUpload__label ', './headphone.png');
Run Code Online (Sandbox Code Playgroud)
我正在处理的 HTML 是这样的:
<div id="ImageUpload" class="ImageUpload u-marginB10">
<label class="ImageUpload__label js-dragdrop-area" for="selectFileMultiple">
<span class="ImageUpload__hide">drag and drop or select files</span>
<span class="ImageUpload__text"><span class="js-dragdrop-num">10</span>up to</span>
</label>
</div>
Run Code Online (Sandbox Code Playgroud)
那么,是否可以使用 Playwright 将文件上传到此类 HTML 元素呢?
playwright ×10
node.js ×5
javascript ×4
typescript ×3
automation ×1
dotenv ×1
e2e-testing ×1
tabs ×1
testing ×1