是否可以通过GitHub动作中的柏树测试在Firebase存储中上传图像?

发布于 2025-02-12 07:20:08 字数 2606 浏览 0 评论 0原文

我正在尝试在柏树E2E测试中将文件(JPEG文件)添加到firebase存储中。它运行良好,测试用例在当地通过。我登录用户,执行所有必需的操作(单击按钮,将文件从固定文件夹连接到输入标签以进行文件,通过单击按钮保存更改)。 但是问题发生在GitHub操作中CI/CD管道中,所有测试容器都通过,但是在此测试案例上发生了错误,即无法读取未定义(读取名称)的属性。我正在访问fileSelected.name更改时。 我的假设是,该文件是由柏树找到的,或者存在与Firebase存储中的权限有关的问题,这就是为什么失败的原因。有人可以帮助/指导我为什么不起作用吗?

我的柏树测试:

it("adds company successfully", () => {
    cy.visit("http://127.0.0.1:3000/dashboard/company", {
      headers: { "Accept-Encoding": "gzip, deflate" },
    }).then(() => {
      cy.wait(10000);
      cy.get("#newCompany").click();
      cy.get("#companyName").type("Test company").blur();
      cy.get("#companyEmail").type("[email protected]").blur();

      cy.get("input[type=file]").selectFile(
        {
          contents: "cypress/fixtures/file.jpeg",
          fileName: `file${new Date().toISOString()}.jpeg`,
        },
        {
          force: true,
        }
      );

      cy.get(".purple-button").contains("Save Changes").click();
    });
  });

HandleSave()函数在单击“保存更改”按钮时运行。

const handleSave = () => {
    setIsSubmitted(true);
    if (isFormValid()) {
      if (fileSelected.name) {
        handleUpload();
      } else {
        saveCompany(companyPictureUrl);
        props.handleClose();
      }
    }
  };

  const handleUpload = () => {
    try {
      setIsUploading(true);
      const storageRef = ref(storage, "company/" + fileSelected.name);
      const uploadTask = uploadBytesResumable(storageRef, fileSelected);

      uploadTask.on(
        "state_changed",
        (snapshot: { bytesTransferred: number; totalBytes: number }) => {
          const progress =
            Math.round(
              (snapshot.bytesTransferred / snapshot.totalBytes) * 1000
            ) / 10;
          setUploadProgress(progress);
        },
        (error: any) => {
          console.log(error);
        },
        async () => {
          try {
            const url = await getDownloadURL(storageRef);
            setCompanyPictureUrl(url);
            saveCompany(url);
            props.handleClose();
          } catch (error: any) {
            console.log(error);
          }
        }
      );
    } catch (ex) {
      console.log("Error while uploading file :: ", ex);
    }
  };`

github动作错误:

I am trying to add a file (jpeg file) into the firebase storage in a Cypress e2e test. It runs fine and the test case passes locally. I login the user, perform all the required actions (clicking button, attach file from fixtures folder to the input tag for file, save changes by clicking button).
But issue occurs in Github actions CI/CD pipeline, All the testcases pass but error occurs on this test case that Cannot read properties of undefined (Reading name). I am accessing fileselected.name when changes are saved.
My assumption is that the file is not found by cypress or there is an issue related to permissions in firebase storage that is why it is failing. Can anybody help/guide me about why is it not working?

My Cypress test:

it("adds company successfully", () => {
    cy.visit("http://127.0.0.1:3000/dashboard/company", {
      headers: { "Accept-Encoding": "gzip, deflate" },
    }).then(() => {
      cy.wait(10000);
      cy.get("#newCompany").click();
      cy.get("#companyName").type("Test company").blur();
      cy.get("#companyEmail").type("[email protected]").blur();

      cy.get("input[type=file]").selectFile(
        {
          contents: "cypress/fixtures/file.jpeg",
          fileName: `file${new Date().toISOString()}.jpeg`,
        },
        {
          force: true,
        }
      );

      cy.get(".purple-button").contains("Save Changes").click();
    });
  });

The handleSave() function runs on clicking Save Changes button.

const handleSave = () => {
    setIsSubmitted(true);
    if (isFormValid()) {
      if (fileSelected.name) {
        handleUpload();
      } else {
        saveCompany(companyPictureUrl);
        props.handleClose();
      }
    }
  };

  const handleUpload = () => {
    try {
      setIsUploading(true);
      const storageRef = ref(storage, "company/" + fileSelected.name);
      const uploadTask = uploadBytesResumable(storageRef, fileSelected);

      uploadTask.on(
        "state_changed",
        (snapshot: { bytesTransferred: number; totalBytes: number }) => {
          const progress =
            Math.round(
              (snapshot.bytesTransferred / snapshot.totalBytes) * 1000
            ) / 10;
          setUploadProgress(progress);
        },
        (error: any) => {
          console.log(error);
        },
        async () => {
          try {
            const url = await getDownloadURL(storageRef);
            setCompanyPictureUrl(url);
            saveCompany(url);
            props.handleClose();
          } catch (error: any) {
            console.log(error);
          }
        }
      );
    } catch (ex) {
      console.log("Error while uploading file :: ", ex);
    }
  };`

Github actions error:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文