網站搜尋

typescript - ENOENT:沒有此類檔案或目錄,當嘗試使用 Playwright 上傳檔案時


我似乎無法弄清楚為什麼我想用作上傳的檔案無法使用,我已經建立了這個上傳功能

async uploadFile(file: string) {
    await this.fileUploadIcon.click();
    expect(this.page.locator('.file-upload-input')).toBeVisible();
    await this.page.setInputFiles('.file-upload-input', file);
}

正在測試中使用的:

test.only('Upload a document', async () => {
    await uploadActions.uploadFile(firstDoc);
});

第一個文件正在使用此路徑初始化:

const firstDoc = '../Assets/Automated_Doc_2.pdf';

但測試失敗了,而且除了 ENOENT: no such file or directory 錯誤之外,路徑看起來是正確的。

我做錯了什麼?

答案#1

你忘記了在 expect 前使用 await -> await expect...。

我也建議使用 path.join(__dirname, '../Assets/Autoamted_Doc_2.pdf'),這是一種更好的處理路徑的方式,因為否則它是基於當前工作目錄(CWD)的。

要導入 path,你必須:import path from 'path';

同時使用兩者應該會有效。

答案#2

在你的路徑中,我會使用雙斜線而不是單斜線。

 C:\\Users\\myName\\Desktop\\Automation\\TestAutomation\\playwright.