如何在一个文件内同步执行测试?

huangapple go评论59阅读模式
英文:

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(&#39;my first test&#39;, () =&gt; {
  it(&#39;should visit en!&#39;, () =&gt; {
    cy.visit(&#39;https://myurl.com&#39;)
    cy.contains(&#39;#the-element&#39;) #passes
  })
  it(&#39;should have basics&#39;, () =&gt; {
    cy.contains(&#39;#the-element&#39;) #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(&#39;cypress&#39;)

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

huangapple
  • 本文由 发表于 2023年5月25日 07:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327986.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定