我正在为我的React网站使用Jest puppeteer创建测试。单独运行时,每个测试均通过,但同时运行时,则不通过。
import './testFunctions'
import {
cleanSmokeTest,
testErrorsPage1,
testErrorsPages2,
} from '. /testFunctions'
describe('app', () => {
beforeEach(async () => {
jest.setTimeout(120000)
await page.goto('http://localhost:3000')
})
it('should for through all pages with no issue', async () => {
await cleanSmokeTest()
})
it('test errors on page 1', async () => {
await testErrorsPage1()
})
it('test errors on page 2', async () => {
await testErrorsPage2()
})
我对解决方案的最佳猜测涉及清除会话存储或在新页面中打开浏览器(因为如果该页面已经通过一次,则不会发生错误)
以下内容不会打开网页的网址,因此我在解决此问题上遇到了困难
import './testFunctions'
import {
cleanSmokeTest,
testErrorsPage1,
testErrorsPages2,
} from '. /testFunctions'
describe('app', () => {
beforeEach(async () => {
jest.setTimeout(120000)
const puppeteer = require('puppeteer')
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto('http://localhost:3000')
})
it('should for through all pages with no issue', async () => {
await cleanSmokeTest()
})
it('test errors on page 1', async () => {
await testErrorsPage1()
})
it('test errors on page 2', async () => {
await testErrorsPage2()
})
使用该行:
sessionStorage.clear()
产生错误
ReferenceError: sessionStorage is not defined
和:
window.sessionStorage.clear()
产生错误
ReferenceError: window is not defined
我认为您正在nodejs环境中运行sessionStorage.clear()
函数。 sessionStorage在客户端javascript上下文中定义。下面的代码在页面上下文中执行该功能。
const html = await page.evaluate(() => {sessionStorage.clear() });
找到我的解决方案
await page.goto('http://localhost:3000')
await page.evaluate(() => {
sessionStorage.clear()
})
await page.goto('http://localhost:3000')
之所以这样,是因为除非到达页面,否则未定义sessionStorage。清除后,页面需要刷新,因为redux将其保留在内存中。