网站搜索

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: 无此文件或目录 错误之外,路径看起来是正确的。

我哪里做错了?

答案#1

你忘记等待 expect -> await expect...。

我还建议使用 path.join(__dirname, '../Assets/Autoamted_Doc_2.pdf'),这是处理路径的更好方法,否则它是基于当前工作目录(CWD)的。

要导入 path,你必须:import path from 'path';

同时使用两者应该可以使其工作。

答案#2

在你的路径中,我会使用双斜杠,而不是单斜杠。

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