英文:
How to synchronize the test execution within one file?
问题
我想访问页面一次,然后对加载的页面执行多个验证,这些验证分为多个测试。我不想在每个测试中重新加载页面。
我有以下代码:
describe('我的第一个测试', () => {
it('应该访问en!', () => {
cy.visit('https://myurl.com')
cy.contains('#the-element') // 通过
})
it('应该包含基本信息', () => {
cy.contains('#the-element') // 失败,可能是因为页面没有加载完成
})
})
我如何使第二个 cy.contains
通过?
英文:
I want to visit the page once, and then execute multiple validations against the loaded page, organized in multiple tests. I don't want to reload the page with each and every test.
I have the following:
<!-- language: js -->
describe('my first test', () => {
it('should visit en!', () => {
cy.visit('https://myurl.com')
cy.contains('#the-element') #passes
})
it('should have basics', () => {
cy.contains('#the-element') #fails, presumable because the page is not loaded
})
How do I get the second cy.contains to pass?
答案1
得分: 2
你可以将testIsolation
选项关闭。
cypress.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
testIsolation: false,
},
})
默认情况下是开启的,这会导致浏览器在测试之间重置为about:blank
。
参见 https://docs.cypress.io/guides/core-concepts/test-isolation#Quick-Comparison
testIsolation | beforeEach test |
---|---|
true | 通过访问 about:blank清除页面 |
false | 不会改变当前浏览器上下文 |
英文:
You can set the testIsolation
option off.
cypress.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
testIsolation: false,
},
})
The default is on, which causes the browser to reset to about:blank
between tests.
See https://docs.cypress.io/guides/core-concepts/test-isolation#Quick-Comparison
testIsolation | beforeEach test |
---|---|
true | clears page by visiting about:blank |
false | does not alter the current browser context |
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论