Wil*_*iam 14 javascript automated-tests typescript playwright playwright-test
我对剧作家很陌生。由于我的测试套件,我需要在运行每个测试之前登录我的应用程序。在一个简单的规范文件中,我可以简单地调用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 page.inputValue('input[placeholder="2011/03/02"]');
expect(dateFilter).toBe('2021/12/07');
});
});
Run Code Online (Sandbox Code Playgroud)
我尝试简单地获取该代码并将其设为单独的 .ts 文件中的函数,然后导入它,但我认为需要上下文才能执行此操作。这可能是每个使用剧作家的测试人员都知道并经常使用的东西,但是,我没有找到任何关于这个主题的内容。
如何避免复制整个代码beforeEach并将其粘贴到我的所有规范文件中?我怎样才能让它成为一个函数并在我需要的时候调用它?
小智 13
使用固定装置。
固定装置.js:
const base = require('@playwright/test')
const newTest = base.test.extend({
login: async({page}, use) => {
await login();
await use(page); //runs test here
//logic after test
}
})
exports.newTest = newTest
exports.expect = newTest.expect
Run Code Online (Sandbox Code Playgroud)
然后在你的测试中:
const {newTest} = require('fixture.js')
newTest('mytest', async ({login}) => {
//test logic
login.goto(''); // using login here since I pass it as page in the fixture.
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21351 次 |
| 最近记录: |