Reactjs 小丑木偶测试与反应不';在输入中键入文本

Reactjs 小丑木偶测试与反应不';在输入中键入文本,reactjs,jestjs,create-react-app,puppeteer,jest-puppeteer,Reactjs,Jestjs,Create React App,Puppeteer,Jest Puppeteer,我有一个从“CreateReact应用程序”创建的应用程序,我还添加了用于运行端到端测试的Puppeter。 在尝试运行登录测试时,我无法在登录表单输入中键入文本 jest-puppeter.config.js: module.exports = { server: { command: `npm start`, port: 3000, launchTimeout: 10000, debug: true } }; module.exports = {

我有一个从“CreateReact应用程序”创建的应用程序,我还添加了用于运行端到端测试的Puppeter。 在尝试运行登录测试时,我无法在登录表单输入中键入文本

jest-puppeter.config.js:

module.exports = {
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};
module.exports = {
  preset: 'jest-puppeteer',
  testRegex: './*\\index.test\\.js$'
};
launch: {
    headless: false,
}
jest.config.js:

module.exports = {
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};
module.exports = {
  preset: 'jest-puppeteer',
  testRegex: './*\\index.test\\.js$'
};
launch: {
    headless: false,
}
我的登录测试:

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' });
  }
我还尝试使用以下任何一种:

wait page.type(“#用户名”,“abcd”)

wait expect(page.toFill('input[name=“username”]”,'abcd')


但它仍然不会键入任何文本。我想知道我的设置是否适合创建react应用程序。你知道怎么解决这个问题吗?

木偶演员在默认情况下是无头运行的,我相信它是在后台运行的

在jest-puppeter.config.js中添加以下行:

module.exports = {
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};
module.exports = {
  preset: 'jest-puppeteer',
  testRegex: './*\\index.test\\.js$'
};
launch: {
    headless: false,
}
所以看起来是这样的:

module.exports = {
  launch: {
        headless: false,
  },
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};

这将实际打开浏览器,您将能够看到正在发生的事情。

默认情况下,Puppeter是无头运行的,我相信它确实在后台运行

在jest-puppeter.config.js中添加以下行:

module.exports = {
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};
module.exports = {
  preset: 'jest-puppeteer',
  testRegex: './*\\index.test\\.js$'
};
launch: {
    headless: false,
}
所以看起来是这样的:

module.exports = {
  launch: {
        headless: false,
  },
  server: {
    command: `npm start`,
    port: 3000,
    launchTimeout: 10000,
    debug: true
  }
};

这将实际打开浏览器,您将能够看到正在发生的事情。

继续之前,请检查您的应用程序,并检查选择器是否有错误或其他问题。不知何故,在使用测试框架(如Jest或任何您喜欢的东西)之前,我更喜欢只使用Puppeter,看看代码是否运行良好、顺利

不要忘记在开始测试时添加
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' })
    }
})

在继续之前,请检查您的应用程序,并检查选择器是否错误或其他问题。不知何故,在使用测试框架(如Jest或任何您喜欢的东西)之前,我更喜欢只使用Puppeter,看看代码是否运行良好、顺利

不要忘记在开始测试时添加
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' })
    }
})

到底发生了什么?到底发生了什么?哦,天哪,它运行了应用程序,我还以为它是测试应用程序。谢谢悬赏仍然锁定,我会奖励你一旦它打开我的,它运行的应用程序,我认为这是测试应用程序。谢谢赏金锁上了,一打开我就奖励你