我有一个从"create-react- app“创建的应用程序,我还添加了用于运行端到端测试的puppeteer。在尝试运行登录测试时,我无法在表单输入的日志中键入文本。
jest-puppeteer.config.js:
module.exports = {
server: {
command: `npm start`,
port: 3000,
launchTimeout: 10000,
debug: true
}
};jest.config.js:
module.exports = {
preset: 'jest-puppeteer',
testRegex: './*\\index.test\\.js$'
};我的登录测试:
it('should login test user', async () => {
await page.waitForSelector('form[name="login"]');
await expect(page).toFillForm('form[name="login"]', {
username: 'abcd',
password: 'abcd'
});
await expect(page).toMatchElement('input[name="username"]', { text: 'abcd' });
}我还尝试使用以下任意一种方法:
await page.type('#username', 'abcd');
await page.click('input[name="username"]');
await page.type('input[name="username"]', 'abcd');await expect(page).toFill('input[name="username"]', 'abcd');
但是,它仍然不会输入任何文本。我想知道我的设置是否适合create-react-app。你知道怎么解决这个问题吗?
发布于 2019-10-28 15:52:40
默认情况下,Puppeteer是无头运行的,我相信它是在后台运行的。
将以下行添加到jest-puppeteer.config.js中:
launch: {
headless: false,
}所以它看起来是这样的:
module.exports = {
launch: {
headless: false,
},
server: {
command: `npm start`,
port: 3000,
launchTimeout: 10000,
debug: true
}
};这将实际打开浏览器,您将能够看到发生了什么。
发布于 2019-10-28 17:18:19
在继续之前,请检查您的应用程序,并检查选择器是否错误或有什么问题。不知何故,在使用Jest或任何你喜欢的测试框架之前,我更喜欢只使用puppeteer,看看代码是否运行良好和流畅。
不要忘了在开始测试时添加headless : false。
你可以试试这个。
describe('Login', () => {
beforeAll(async () => {
await page.goto('https://localhost:3000');
})
it('should login test user', async () => {
const waitForTheName = await page.waitForSelector('input[name="username"]');
const focusInputName = await page.focus('input[name="username"]')
const writeInputName = await page.keyboard.type('abcd')
const focusInputPaswd = await page.focus('input[name="password"]')
const writeInputPaswd = await page.keyboard.type('abcd')
await expect(page).toFillForm('form[name="login"]', {
username: 'abcd',
password: 'abcd'
})
await expect(page).toMatchElement('input[name="username"]', { text: 'abcd' })
}
})https://stackoverflow.com/questions/58560063
复制相似问题