英文:
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()
一个已经是字符串的东西,但这肯定不会导致问题。
我想到时区,但那不能是解释,因为你的错误消息明确指出了它要查找的路径。
在我的系统上,它们完全相等。
让你的测试代码先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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论