【发布时间】:2023-02-10 05:56:02
【问题描述】:
问题:我正在尝试使用 .selectFile 上传图片,但我遇到了一个错误,它被隐藏了,因此这是不可能的。
使用 Material UI 反应代码:
<Button
datacy="uploadImage"
size={"medium"}
disableRipple
disableTouchRipple
disableFocusRipple
component="label"
variant={"text"}
sx={{ marginTop: { xs: 2, md: 0 }, p: 0, width: "fit-content" }}
>
<input
type="file"
onChange={(e) => onChange(e)}
hidden
accept="image/png, image/jpeg"
/>
{hasProfilePicture ? "Change" : "Upload"}
</Button>
HTML 组件:
<label role="button" datacy="uploadImage">
<input type="file" accept="image/png, image/jpeg" hidden="">
Change
</label>
失败的尝试:
-
cy.get('[datacy="uploadImage"] input').selectFile("cypress/fixtures/Headshot 2.jpg");4000 毫秒后重试超时:cy.selectFile() 失败,因为此元素不可见: 该元素不可见,因为它具有 CSS 属性:display: none 修复此问题,或使用 {force: true} 禁用错误检查。
-
cy.get("input[type='file'] hidden").selectFile("cypress/fixtures/Headshot 2.jpg");4000 毫秒后重试超时:预期会找到隐藏的元素:input[type='file'],但从未找到它。
问题:我可以编写什么 Cypress 前端测试来单击组件并上传图像?
【问题讨论】: