我正在testcafe
应用程序中使用Electron-React
,试图集成一些基本的e2e tests
。
该测试有效,但是无关紧要。
我想知道我可以通过登录页面并在其他页面上获得额外点击。
App.e2e.js
import { Selector } from 'testcafe';
import { getPageTitle, getPageUrl, fixture, test, login } from './helpers';
const assertNoConsoleErrors = async browser => {
const { error } = await browser.getBrowserConsoleMessages();
await browser.expect(error).eql([]);
};
fixture`Electron Client`.page('../../app/app.html').afterEach(assertNoConsoleErrors);
test('should atempt login without credentials', async browser => {
await login({
email: '[email protected]',
password: '123456',
browser,
});
await browser
.click('button[type=submit]')
.expect(getPageUrl())
.contains('/login');
const email = Selector('[name="email"]');
const password = Selector('[name="password"]');
await browser.expect(email.value).eql('[email protected]');
await browser.expect(password.value).eql('123456');
});
helpers.js
import { ClientFunction } from 'testcafe';
export const getPageTitle = ClientFunction(() => document.title);
export const fixture = (...args) => global.fixture(...args);
export const test = (...args) => global.test(...args);
export const getPageUrl = ClientFunction(() => window.location.href);
export const login = async ({ email, password, browser }) => {
await browser.typeText('[data-test="email"]', email);
await browser.typeText('[data-test="password"]', password);
await browser.click('button[type=submit]');
};
您可以使用User Roles API满足您的要求。请参见下面的示例。
const loginAsTestUser = Role('../../app/app.html', async t => {
await t
.typeText('[data-test="email"]', email)
.typeText('[data-test="password"]', password)
.click('button[type=submit]');
});
fixture`Electron Client`
.page('../../app/app.html')
.beforeEach(async t => {
await t.useRole(loginAsTestUser);
});
.afterEach(assertNoConsoleErrors);
test('should atempt login without credentials', async browser => {
await browser
.click('button[type=submit]')
.expect(getPageUrl())
.contains('/login');
const email = Selector('[name="email"]');
const password = Selector('[name="password"]');
await browser.expect(email.value).eql('[email protected]');
await browser.expect(password.value).eql('123456');
});