cy.readfile() 无法从动态路径读取文件。

huangapple go评论65阅读模式
英文:

cy.readfile() fails to read the file from dynamic path

问题

I am trying to read a file that is downloaded in the cypress/downloads folder. I am preparing the path to the file like this:

let filename = 'Publisher_' + new Date().toLocaleDateString('en-US').replaceAll('/', '_') + '.html';
let filepath = 'cypress/downloads/' + filename.toString();
cy.readFile(filepath);

But cy.readFile is giving the below error:

AssertionError
Timed out retrying after 4000ms: 
cy.readFile("cypress/downloads/Publisher_5_25_2023.html") failed because the file 
does not exist at the following path:

/Users/myMacbook/Desktop/cy-automation/cypress/downloads/Publisher_5_25_2023.html

However, when I do cy.readFile('cypress/downloads/Publisher_5_25_2023.html'); then it works perfectly fine.

Why it is failing for concatenation? How can I fix it?

英文:

I am trying to read a file that is downloaded in the cypress/downloads folder. I am preparing the path to the file like this

let filename = 'Publisher_' + new Date().toLocaleDateString('en-US').replaceAll('/', '_').toString() + '.html';
let filepath = 'cypress/downloads/' + filename.toString();
cy.readfile(filepath)

But cy.readFile is giving the below error

AssertionError
Timed out retrying after 4000ms: 
cy.readFile("cypress/downloads/Publisher_5_25_2023.html") failed because the file 
does not exist at the following path:

/Users/myMacbook/Desktop/cy-automation/cypress/downloads/Publisher_5_25_2023.htmlLearn more
View stack trace


Print to console
at Context.eval (http://localhost:8080/__cypress/tests?p=cypress/integration/features/publisherMenu/publisher-diary-publisher.feature:13508:23)
at Registry.runStepDefininition (http://localhost:8080/__cypress/tests?p=cypress/integration/features/publisherMenu/publisher-diary-publisher.feature:9254:48)
at Object.fn (http://localhost:8080/__cypress/tests?p=cypress/integration/features/publisherMenu/publisher-diary-publisher.feature:10155:41)
at runStepWithLogGroup (http://localhost:8080/__cypress/tests?p=cypress/integration/features/publisherMenu/publisher-diary-publisher.feature:9859:36)
at Context.eval (http://localhost:8080/__cypress/tests?p=cypress/integration/features/publisherMenu/publisher-diary-publisher.feature:10152:60)

However when I do

cy.readFile('cypress/downloads/Publisher_5_25_2023.html'); then it works perfectly fine

Why it is failing for concatenation? How can I fix it?

答案1

得分: 3

Here's the translated content:

非常奇怪!你可以尝试一些调试步骤吗?

对我来说,动态文件名似乎正常工作。

尝试这个:

const filename = 'Publisher_' + new Date().toLocaleDateString('en-US').replaceAll('/', '_') + '.html';
const filepath = 'cypress/downloads/' + filename;

const manualString = "cypress/downloads/Publisher_5_25_2023.html";

console.log("相等性:", filepath === manualString)

(显然,你需要将manualString更新为今天的日期。)

没有必要.toString()一个已经是字符串的东西,但这肯定不会导致问题。

我想到时区,但那不能是解释,因为你的错误消息明确指出了它要查找的路径。

在我的系统上,它们完全相等。

cy.readfile() 无法从动态路径读取文件。

让你的测试代码先console.log动态字符串,然后单独console.log手动字符串,以及相等性测试,可能也有助于调试。

英文:

Very strange! Can you try some debugging steps?

For me, dynamic filenames seem to work fine.

Try this:

const filename = 'Publisher_' + new Date().toLocaleDateString('en-US').replaceAll('/', '_').toString() + '.html';
const filepath = 'cypress/downloads/' + filename;

const manualString = "cypress/downloads/Publisher_5_25_2023.html"

console.log("Equality: ", filepath === manualString)

(Obviously you will need to update the manualString to today's date.)

There is no need to .toString() a thing that is already a string, but that is certainly not going to cause a problem.

I wondered about time zones, but that cannot be the explanation because your error message explicitly states the path it was looking for.

On my system they are exactly equal.

cy.readfile() 无法从动态路径读取文件。

Having your test code console.log the dynamic string, and then separately the manual string, and the test of equality, might also help your debugging.

huangapple
  • 本文由 发表于 2023年5月25日 18:29:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76331303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定