Nuz*_*zob 8 testing pageobjects jestjs puppeteer jest-puppeteer
我刚刚进入了 puppeteer 和 jest 的测试世界,我想知道在文件夹架构和逻辑方面的最佳实践是什么。
我以前从未做过测试,我认为我对不同的原则和概念以及它们如何组合在一起有点迷失。
我学会了基于页面对象模型进行测试,因此我为每个页面以及每个模块(或组件)都有类。例如,在我的应用程序中,标题或登录模式是组件。
然后我每个页面或每个组件都有一个测试文件。(例如landingPage.tests.js文件,它使用文件中LandingPage类的模型LandingPage.js)
这是一个具体的例子:我有不同的登录案例,我想测试它们。例如,我想测试与“普通”用户的连接,该过程只是登录然后输入密码。然后,我需要与已激活 2FA 的用户或使用 SSO 的公司的用户进行测试。
我首先考虑将我的不同测试放在authentication.tests.js不同的describe块中,认为它每次都会打开一个新标签,但它没有......我在隐身模式下使用 puppeteer 来确保每个标签都是一个独立的会话。
所以我的问题是:
哪里是做这些测试套件的最佳地点?
我是否应该拥有“描述”页面的测试文件(例如,按钮必须存在,此类文本必须在此处等)并且还有“场景类型”测试文件(对用户的一组上下文操作,例如对于我不同的登录情况)?
这是authentication.tests.js,我想测试我所有不同的登录方式:
import HeaderComponent from "../../../pages/components/HeaderComponent";
import AuthenticationComponent from "../../../pages/components/AuthenticationComponent";
import LandingPage from "../../../pages/landing/LandingPage";
import {
JEST_TIMEOUT,
CREDENTIALS
} from "../../../config";
describe('Component:Authentication', () => {
let headerComponent;
let authenticationComponent;
let landingPage;
beforeAll(async () => {
jest.setTimeout(JEST_TIMEOUT);
headerComponent = new HeaderComponent;
authenticationComponent = new AuthenticationComponent;
landingPage = new LandingPage;
});
describe('Normal login ', () => {
it('should click on login and open modal', async () => {
await landingPage.visit();
await headerComponent.isVisible();
await headerComponent.clickOnLogin();
await authenticationComponent.isVisible();
});
it('should type a normal user email adress and validate', async () => {
await authenticationComponent.typeUsername(CREDENTIALS.normal.username);
await authenticationComponent.clickNext();
});
it('should type the correct password and validate', async () => {
await authenticationComponent.typePassword(CREDENTIALS.normal.password);
await authenticationComponent.clickNext();
});
it('should be logged in', async () => {
await waitForText(page, 'body', 'Success !');
});
});
describe('SSO login ', () => {
// todo ...
});
});
Run Code Online (Sandbox Code Playgroud)
谢谢,如果这听起来令人困惑,很抱歉,就像我说的那样,我正在努力弄清楚它们是如何组合在一起的。
关于文件夹结构,Jest 将根据匹配配置查找任何文件,基本上是任何名为 *.spec.js 或 *.test.js 的文件。看起来你已经知道了。
这意味着文件夹结构完全取决于您。有些人喜欢将组件测试放在与组件本身相同的文件夹中。就我个人而言,我更喜欢将所有测试放在一个文件夹中,因为它使项目看起来更干净。
将所有测试放在一个文件夹中的另一个好处是,您可以开始区分测试类型。组件测试检查纯组件是否按预期呈现和运行。为此,您不需要 Puppeteer,如果您使用的是 React 应用程序,请使用快照。Puppeteer 非常适合使用无头 Chromium 浏览器浏览所谓的“快乐路径”、登录、注册、添加到购物车等集成测试。
要回答您在每次测试的新页面上使用 Jest / Puppeteer 时遇到的具体问题:
//keep a reference to the browser
let browser
//keep a reference to the page
let page
// open puppeteer before all tests
beforeAll(async () => {
browser = await puppeteer.launch()
})
// close puppeteer after all tests
afterAll(async () => {
await browser.close()
})
// Get a new page for each test so that we start fresh.
beforeEach(async () => {
page = await browser.newPage()
})
// Remember to close pages after each test.
afterEach(async () => {
await page.close()
})
describe('Counter', () => {
// "it" blocks go here.
})
Run Code Online (Sandbox Code Playgroud)
希望有点帮助。
| 归档时间: |
|
| 查看次数: |
865 次 |
| 最近记录: |