0

我有一个从“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。知道如何解决这个问题吗?

4

2 回答 2

2

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-28T07:52:39.790 回答
1

在继续之前,请检查您的应用程序并检查选择器是否错误或其他什么。不知何故,在使用 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' })
    }
})
于 2019-10-28T09:18:19.477 回答