简体   繁体   English

如何将夹具图像添加到赛普拉斯中的元素?

[英]How to add fixture image to element in Cypress?

I have a react application that requires the URL of an image as text input, then after submitting the URL, the image from the URL gets displayed in an <img> by setting it's src attribute.我有一个 React 应用程序,它需要图像的 URL 作为文本输入,然后在提交 URL 之后,来自 URL 的图像通过设置其 src 属性显示在<img>中。

I'm writing a test in Cypress and I want to either stub this request to return the fixture image or just display my fixture image on the page by attaching it to an element.我正在 Cypress 中编写测试,我想将此请求存根以返回夹具图像,或者只是通过将其附加到元素来在页面上显示我的夹具图像。 What I tried:我尝试了什么:

  • Used route2 to stub this image request and return my fixture image as a response, but it put the cypress runner in an infinite loop by calling the url of the image endessly:使用 route2 存根这个图像请求并返回我的夹具图像作为响应,但它通过不断调用图像的 url 将赛普拉斯赛跑者置于无限循环中: 在此处输入图像描述 The documentation of route2 say it's able to intercept resource loads, so should it be possible somehow with this? route2 的文档说它能够拦截资源负载,那么它应该以某种方式实现吗?

     cy.route2('GET', '**/kids.jpg', { statusCode: 200, fixture: `${FIXTURES_APP_PATH}kids.jpg` })
  • Used the 'cypress-file-upload' package to attach the fixture image file as a blob to the img element (had to replace special chars because the base64StringToBlob threw an 'Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded' error).使用 'cypress-file-upload' package 将夹具图像文件作为 blob 附加到 img 元素(必须替换特殊字符,因为base64StringToBlob抛出了一个 'Failed to execute 'atob' on 'Window': The string to be解码未正确编码'错误)。

     cy.fixture(`${FIXTURES_APP_PATH}kids.jpg`, 'base64').then(fixture => { Cypress.Blob.base64StringToBlob(fixture.replace(/^[^,]+,/, ''), 'image/jpg') }).then((fileContent: Blob) => { cy.get(AppPage.INPUT_IMG).attachFile({filePath: `${FIXTURES_APP_PATH}kids.jpg`, fileContent, mimeType: 'image/jpg'}); });

What am I doing wrong?我究竟做错了什么? How could this be done?这怎么可能呢? Thanks!谢谢!

A fix for the stubbing issue was released in https://github.com/cypress-io/cypress/releases/tag/v5.5.0https://github.com/cypress-io/cypress/releases/tag/v5.5.0中发布了针对存根问题的修复

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM