首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用React的Jest-Puppeteer测试不会在输入中键入文本

使用React的Jest-Puppeteer测试不会在输入中键入文本
EN

Stack Overflow用户
提问于 2019-10-25 22:01:45
回答 2查看 661关注 0票数 0

我有一个从"create-react- app“创建的应用程序,我还添加了用于运行端到端测试的puppeteer。在尝试运行登录测试时,我无法在表单输入的日志中键入文本。

jest-puppeteer.config.js:

代码语言:javascript
复制
module.exports = {
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};

jest.config.js:

代码语言:javascript
复制
module.exports = {
  preset: 'jest-puppeteer',
  testRegex: './*\\index.test\\.js$'
};

我的登录测试:

代码语言:javascript
复制
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');

代码语言:javascript
复制
await page.click('input[name="username"]');
await page.type('input[name="username"]', 'abcd');

await expect(page).toFill('input[name="username"]', 'abcd');

但是,它仍然不会输入任何文本。我想知道我的设置是否适合create-react-app。你知道怎么解决这个问题吗?

EN

回答 2

Stack Overflow用户

发布于 2019-10-28 15:52:40

默认情况下,Puppeteer是无头运行的,我相信它是在后台运行的。

将以下行添加到jest-puppeteer.config.js中:

代码语言:javascript
复制
launch: {
    headless: false,
}

所以它看起来是这样的:

代码语言:javascript
复制
module.exports = {
  launch: {
        headless: false,
  },
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};

这将实际打开浏览器,您将能够看到发生了什么。

票数 2
EN

Stack Overflow用户

发布于 2019-10-28 17:18:19

在继续之前,请检查您的应用程序,并检查选择器是否错误或有什么问题。不知何故,在使用Jest或任何你喜欢的测试框架之前,我更喜欢只使用puppeteer,看看代码是否运行良好和流畅。

不要忘了在开始测试时添加headless : false

你可以试试这个。

代码语言:javascript
复制
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' })
    }
})
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58560063

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档