代码之家  ›  专栏  ›  技术社区  ›  paul

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

  •  0
  • paul  · 技术社区  · 1 年前

    我正在尝试读取中下载的文件 cypress/downloads 文件夹我正在这样准备文件的路径

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

    但是 cy.readFile 给出以下错误

    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)
    

    然而,当我这样做的时候

    cy.readFile('cypress/downloads/Publisher_5_25_2023.html'); 那么它运行得非常好

    为什么串联失败?我该怎么修?

    0 回复  |  直到 1 年前
        1
  •  3
  •   ProfDFrancis    1 年前

    很奇怪!你能尝试一些调试步骤吗?

    对我来说,动态文件名似乎很好用。

    试试这个:

    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)
    
    

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

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

    我想知道时区,但这不能解释,因为您的错误消息明确说明了它正在寻找的路径。

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

    enter image description here

    拥有测试代码 console.log 动态字符串,然后分别是手动字符串和相等性测试,也可能有助于调试。