Repository: oppia/oppia
File: core/tests/puppeteer-acceptance-tests/utilities/user/exploration-editor.ts (TypeScript)

Code before:
      }
      await this.clickOn(mobileNavbarDropdown);
      await this.page.waitForSelector(mobileNavbarPane);
      await this.clickOn(mobileTranslationTabButton);
    } else {
      await this.clickOn(translationTabButton);
    }
  }

  /**
   * Function to navigate to the editor tab.
   */
  async navigateToEditorTab(): Promise<void> {
    if (this.isViewportAtMobileWidth()) {
      const element = await this.page.$(mobileNavbarOptions);
      // If the element is not present, it means the mobile navigation bar is not expanded.
      // The option to save changes appears only in the mobile view after clicking on the mobile options button,
      // which expands the mobile navigation bar.
      if (!element) {
        await this.clickOn(mobileOptionsButton);
      }
      await this.clickOn(mobileNavbarDropdown);
      await this.page.waitForSelector(mobileNavbarPane);
      await this.clickOn(mobileMainTabButton);
    } else {
      await this.clickOn(mainTabButton);
    }
    await this.waitForNetworkIdle();
  }

  /**
   * Function to verify if the preview is on a particular card by checking the content of the card.
   * @param {string} cardName - The name of the card to check.
   * @param {string} expectedCardContent - The expected text content of the card.
   */
  async expectPreviewCardContentToBe(
    cardName: string,
    expectedCardContent: string
  ): Promise<void> {
    await this.page.waitForSelector(stateConversationContent, {
      visible: true,
    });
    const element = await this.page.$(stateConversationContent);
    const cardContent = await this.page.evaluate(
      element => element.textContent,
      element
    );
    if (cardContent !== expectedCardContent) {
      throw new Error(
        `Preview is not on the ${cardName} card or is not loading correctly.`
      );

Review comment (refactor):
I am a bit worried that your test is reading the developer's entire Downloads folder. Generally tests related to Oppia shouldn't reach "outside" Oppia. Could you isolate the test a bit more, maybe use Downloads/oppia-acceptance-test-downloads or something like that, and only look at that subfolder (which is unlikely to contain other stuff)?

Also given that you're working with a general filesystem that might not get cleaned up, you might need to also properly handle the case where a file of the given name already exists.

Also consider having your tests clean up after themselves. E.g. when downloading any file, store the filepath in a list. At the end of the test, delete all files in the list.

Just some things to consider.

Code after:
        await this.page.waitForTimeout(5000);
        const downloadedFile =
          await this.waitForExplorationDownload(expectedFileName);
        if (downloadedFile) {
          showMessage(`${downloadedFile} file is successfully downloaded`);
          return;
        } else {
          throw new Error(
            `Download failed for Exploration version: ${explorationVersion}`
          );
        }
      }
    }
  }

  /**
   * Waits for a downloaded file to appear and cleans up only the new one.
   * @param {string} expectedFileName - The expected file name.
   * @returns {Promise<string | null>} - The verified file name or null if not found.
   */
  async waitForExplorationDownload(
    expectedFileName: string
  ): Promise<string | null> {
    // Wait for network to be idle after triggering the download.
    await this.page.waitForNetworkIdle();
    const files = fs.readdirSync(downloadPath);
    const downloadedFile =
      files.find(file => file === expectedFileName) || null;
    if (
      downloadedFile &&
      fs.existsSync(path.join(downloadPath, downloadedFile))
    ) {
      fs.unlinkSync(path.join(downloadPath, downloadedFile));
    }
    return downloadedFile;
  }

  /**
   * Function to navigate to the translations tab.
   */
  async navigateToTranslationsTab(): Promise<void> {
    if (this.isViewportAtMobileWidth()) {
      const element = await this.page.$(mobileNavbarOptions);
      // If the element is not present, it means the mobile navigation bar is not expanded.
      // The option to save changes appears only in the mobile view after clicking on the mobile options button,
      // which expands the mobile navigation bar.
      if (!element) {
        await this.clickOn(mobileOptionsButton);
      }
      await this.clickOn(mobileNavbarDropdown);
      await this.page.waitForSelector(mobileNavbarPane);