[英]How to upload image or file using cypress-file-upload
I am trying to upload an image for E2E testing but could succeed我正在尝试上传图像以进行 E2E 测试,但可能会成功
First of all, I have installed首先,我安装了
npm install --save-dev cypress-file-upload npm 安装 --save-dev cypress-file-upload
and then using page object model然后使用页面 object model
setting_page.js enter image description here setting_page.js在此处输入图像描述
export class SettingPage {
doc_uploadImage=('#fileDocument')
uploadimage(){
cy.get(this.uploadimage).attachFile('D:\Cypress(Projects)\Agorz_Automation_Project\cypress\fixtures\images.jpg')}
}
Demo.js演示.js
import { SettingPage } from "./Pages/setting_page"
import 'cypress-file-upload'
const settingpage = new SettingPage()
it.only('upload file', function () {
settingpage.uploadimage()
})
Error message screen is attached here enter image description here此处附有错误消息屏幕 在此处输入图像描述
I want to upload an image for testing purposes, you're help will be highly appreciated in this regard.我想上传一张图片用于测试目的,在这方面您的帮助将不胜感激。
The error statement is clear which means that your given path is invalid.错误声明很清楚,这意味着您给定的路径无效。
Could you try moving the image file to your testing project instead like this?您可以像这样尝试将图像文件移动到您的测试项目吗?
.attachFile('images.jpg')}
cypress-file-upload
"recognizes cy.fixture format, so usually this is just a file name." cypress-file-upload
“识别 cy.fixture 格式,所以通常这只是一个文件名。” This means that anything that lives in your cypress/fixture
directory can be referenced by a relative file path.这意味着可以通过相对文件路径引用
cypress/fixture
目录中的任何内容。
cy.get(this.uploadimage)
.attachFile('images.jpg');
If you are wanting to use the cypress-file-upload
plugin to be uploading a file in cypress you should use the attachFile
command provided by the plugin如果您想使用
cypress-file-upload
插件在 cypress 中上传文件,您应该使用插件提供的attachFile
命令
Using it to upload an image would look something like this使用它上传图片看起来像这样
const filePath = 'path/to/image.jpg';
cy.get('#fileInput').attachFile(filePath);
Also in your code you are trying to use the attachFile
command on an element you are selecting using this.uploadimage
but that is a string that corresponds to the selector #fileDocument
but that is not valid for the attachFile
command.同样在您的代码中,您尝试在使用
this.uploadimage
选择的元素上使用attachFile
命令,但这是一个与选择器#fileDocument
对应的字符串,但对attachFile
命令无效。
here is what you could try to fix this, you could use the cy.get
command to select the file input, then use the attachFile
command on it.这是您可以尝试解决此问题的方法,您可以使用
cy.get
命令将文件输入 select,然后对其使用attachFile
命令。
export class SettingPage {
doc_uploadImage = '#fileDocument';
uploadimage() { cy.get(this.doc_uploadImage).attachFile('D:\Cypress(Projects)\Agorz_Automation_Project\cypress\fixtures\images.jpg');
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.