使用带有新Playwright 0.1101的config实施自定义灯具 [英] Implementing Custom Fixtures using config with new Playwright 0.1101

查看:103
本文介绍了使用带有新Playwright 0.1101的config实施自定义灯具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这不是TL; DR.我真的需要有关Playwright的帮助.

I hope this isn't TL;DR. I really need help with Playwright.

简而言之:有人知道如何将设备设置迁移到Playwright v.01101进行配置吗?没有有关如何执行此操作的文档.

v0.1101之前的Playwright/Test版本启用了自定义固定装置,您可以从Playwright/Test模块中进行覆盖或扩展.我利用这一点来实现页面对象模型(POM).这是我的示例装置:

Versions of Playwright/Test prior to v0.1101 enabled customize fixtures that you could override or extend from the Playwright/Test modules. I took advantage of this to implement a page object model (POM). Here is my example fixture:

// In tests/fixtures.ts
import { folio as baseFolio } from '@playwright/test'; //now missing in v.0.1101
import { IndexPage } from "../PO/IndexPage";

require('dotenv').config();
const baseURL = process.env.BASE_URL;

// Extend built-in fixtures and declare types for new fixtures
const builder = baseFolio.extend<{ indexPage: IndexPage }>();

// In fixtures.ts
builder.indexPage.init(async ({ page }, runTest) => {
    // get built-in page and wrap with POM
    const indexPage = new IndexPage(page, baseURL);

    // pass this to your test
    runTest(indexPage);
});

// Build and export the modified fixtures
const folio = builder.build();
export const it = folio.it;
export const expect = folio.expect;
export const describe = folio.describe;

这是我使用此POM运行的测试的示例:

Here is an example of a test I run with this POM:

// In tests/e2e.spec.ts
import { IndexPage } from '../PO/IndexPage';
import { describe, it, expect } from './fixtures'
        
it('EN/FR', async ({ indexPage }) => {
     await indexPage.navigateHome();
     await indexPage.getCurrentLanguage()
     await assertGreetingLanguage(indexPage);
     await indexPage.toggleLanguage();
     await assertGreetingLanguage(indexPage);
});

async function assertGreetingLanguage(indexPage: IndexPage) {
    const lang = await indexPage.getCurrentLanguage();
    const greeting = lang === 'English' ? 'Welcome!' : 'Bienvenue!';
    // console.log(`LANG: ${lang} Greet: ${greeting}`);
    expect(await indexPage.getGreeting()).toBe(greeting);
    // expect(await indexPage.getGreeting()).not.toBe(greeting);
}

尽管使用v0.1101,似乎不再支持固定装置, Folio是实现此配置设置的.

With v0.1101 though, it seems fixtures aren't supported anymore, and instead can be setup with a config file. Of course, Folio is what implements this config setup.

推荐答案

已解决.:)

Playwright在此 Github问题上与我联系.这是完成的过程.官方文档即将发布:

Playwright got back to me on this Github issue. Here's how it's done. Official documentation coming soon:

// config.ts

import { PlaywrightEnv, newTestType, setConfig, TestInfo, reporters, setReporters } from "@playwright/test";
import { IndexPage } from "./PO/IndexPage";


require('dotenv').config();
const baseURL = process.env.BASE_URL;

setConfig({
    testDir: __dirname,  // Search for tests in this directory.
    timeout: 30000,  // Each test is given 30 seconds.
    // timeout: 90000,  // Each test is given 90 seconds.
    retries: 0,  // Failing tests will be retried at most two times.
});

setReporters([
    // Report to the terminal with "line" reporter.
    new reporters.line(),
    // Additionally, output a JUnit XML file.
    new reporters.junit({ outputFile: 'junit.xml', stripANSIControlSequences: true }),
    new reporters.list(),
]);

// Extend the default environment to add any test arguments, for example POMs.
class MyEnv extends PlaywrightEnv {
    async beforeEach(testInfo: TestInfo) {
        // Get all default arguments, including Page.
        const result = await super.beforeEach(testInfo);
        // Create your POM.
        const indexPage = new IndexPage(result.page, baseURL);
        // Return default arguments and new POM.
        return { ...result, indexPage };
    }
}

// Declare "indexPage" test argument for types in IDE.
export const test = newTestType<{ indexPage: IndexPage }>();
export { expect } from "@playwright/test";

// Run tests in three browsers.
const options = {
    // Launch options:
    headless: true,
    slowMo: 50,
    // Context options:
    viewport: { width: 800, height: 600 },
    ignoreHTTPSErrors: true,
    // Testing options:
    // video: 'retain-on-failure',
    screenshot: 'only-on-failure'
};

// Run tests in three browsers.
test.runWith(new MyEnv('chromium', options), { tag: 'chromium' });
test.runWith(new MyEnv('firefox', options), { tag: 'firefox' });
test.runWith(new MyEnv('webkit', options), { tag: 'webkit' });

这篇关于使用带有新Playwright 0.1101的config实施自定义灯具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆