admin管理员组文章数量:1289362
My problem
I'm trying to simulate a response from the server containing an image with cy.intercept
however, the browser won't display my mocked image. The browser just can't interpret my response as a proper image for <img>
tag.
I can still copy the response in debug tools and it seems to be the image I need but probably encoded in the wrong way.
My approach
cy.readFile('src/assets/logo.png', 'binary').then((imgContent) => {
cy.intercept('/uploads/test.png', {
headers: { 'Content-Type': 'image/jpg' },
body: imgContent,
})
});
I've also tried to return base64 image string like this:
cy.intercept(
'/uploads/test.png',
{
headers: {"Content-Type": "image/jpg"},
body:
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAARCUlEQVR4Xu1deXQURRr...
But it didn't work as well.
My problem
I'm trying to simulate a response from the server containing an image with cy.intercept
however, the browser won't display my mocked image. The browser just can't interpret my response as a proper image for <img>
tag.
I can still copy the response in debug tools and it seems to be the image I need but probably encoded in the wrong way.
My approach
cy.readFile('src/assets/logo.png', 'binary').then((imgContent) => {
cy.intercept('/uploads/test.png', {
headers: { 'Content-Type': 'image/jpg' },
body: imgContent,
})
});
I've also tried to return base64 image string like this:
cy.intercept(
'/uploads/test.png',
{
headers: {"Content-Type": "image/jpg"},
body:
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAARCUlEQVR4Xu1deXQURRr...
But it didn't work as well.
Share Improve this question asked May 31, 2021 at 0:15 QbackQback 4,9183 gold badges29 silver badges43 bronze badges 01 Answer
Reset to default 11This seems like a job for a fixture
cy.intercept("/uploads/test.png", { fixture: "logo.png" })
By default, you would place your logo.png
file into the cypress/fixtures
directory however you can configure it to use another location
本文标签: javascriptHow to mock the image response in cypressStack Overflow
版权声明:本文标题:javascript - How to mock the image response in cypress - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741415081a2377463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论