如何在 Cypress 中访问位于 React 源代码文件夹外部的本地 HTML 文件?

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

How to access local html in cypress out of the react src folder?

问题

我有一个应用程序,其中包含 Cypress 可视化测试。我在该文件夹中创建了一个本地 HTML 文件,尝试将其呈现到 Cypress 代码中。但目标位置始终在 HTML 位置前添加本地基本 URL 的前缀。那么,我该如何访问该文件?

it('visits local file', () => {
  cy.visit('chat.html')
  cy.contains('local file')
})

我的文件夹结构如下:

app
  - src
  - test
    - index.html
    - cypress.test.ts

我的测试位于 test 文件夹中,它是一个 React 应用程序。

英文:

I have an application where I have cypress visual test. I have created a local HTML file in that folder and try to rendering it to the cypress code. But the target location always adding prefix of the local base URL to the HTML location. So how can I access that file?

<!-- language: js -->

it(&#39;visits local file&#39;, () =&gt; {
  cy.visit(&#39;chat.html&#39;)
  cy.contains(&#39;local file&#39;)
})

The folder structure I have...

app
  - src
  - test
    - index.html
    - cypress.test.ts

My test is in the test folder and it's a react app.

答案1

得分: 3

你可以使用 cy.mount() 命令,类似于组件测试,但在端对端测试中使用。

这样就完全与 baseUrl 分离了。

Cypress.Commands.add('mount', (html) => {
  const testEl = Cypress.$(html)
  cy.$$('body').append(testEl)
})

it('访问本地文件', () => {
  cy.readFile('./src/chat.html').then(chat => {
    cy.mount(chat)
  })

  cy.contains('本地文件')  
})

这对于纯HTML是有效的,例如

// chat.html

<div>本地文件</div>

但由于你有一个React应用,有可能需要将它与React一起挂载。

在这种情况下,完整的组件测试可能更好。

参考链接:React组件测试

英文:

You can use a cy.mount() command, similar to Component testing but in an e2e test.

That way it's completely separate to baseUrl.

Cypress.Commands.add(&#39;mount&#39;, (html) =&gt; {
  const testEl = Cypress.$(html)
  cy.$$(&#39;body&#39;).append(testEl)
})

it(&#39;visits local file&#39;, () =&gt; {
  cy.readFile(&#39;./src/chat.html&#39;).then(chat =&gt; {
    cy.mount(chat)
  })

  cy.contains(&#39;local file&#39;)  
})

This passes for plain HTML, for example

// chat.html

&lt;div&gt;local file&lt;/div&gt;

but since you have a React app chances are it will need mounting along with React itself.

In this case, the full Component test would be better.

For reference: React Component Testing

答案2

得分: 1

If you have a baseUrl set in your cypress config, Cypress will prepend any cy.visit() that has a relative URL with that value. So, if my baseUrl is http://localhost:8080, using cy.visit('chat.html') will attempt to visit http://localhost:8000/chat.html.

To avoid this, you will have to clear the baseUrl.

英文:

If you have a baseUrl set in your cypress config, Cypress will prepend any cy.visit() that has a relative URL with that value. So, if my baseUrl is http://localhost:8080, using cy.visit(&#39;chat.html&#39;) will attempt to visit http://localhost:8000chat.html.

To avoid this, you will have to clear the baseUrl.

it(&#39;visits local file&#39;, { baseUrl: null }, () =&gt; {
  cy.visit(&#39;./chat.html&#39;)
  cy.contains(&#39;local file&#39;)
})

Note: the above will clear the baseUrl only in that specific test. You can apply the same override in describe() and context() blocks as well.

huangapple
  • 本文由 发表于 2023年6月12日 22:37:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76457727.html
匿名

发表评论

匿名网友

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

确定