【问题标题】:cy.url() and/or cy.location('href') does not return a stringcy.url() 和/或 cy.location('href') 不返回字符串
【发布时间】:2019-06-27 11:46:41
【问题描述】:

我有一个编辑器页面。当我添加任何内容并单击“保存”按钮时,我的 URL 将更改,在 URL 中添加一个随机 ID。每次单击“保存按钮”时,我想检查我的 ID 是否正在更改。

我将 URL 结果保存在变量中并想检查它,我这样做:

const currentURL = cy.url();
cy.get('.editor-toolbar-actions-save').click();
cy.url().should('not.eq', currentURL);

但是我的currentURL 变量的类型不是字符串:

预计 http://localhost:8080/editor/37b44d4d-48b7-4d19-b3de-56b38fc9f951 不等于 { Object (chainerId, firstCall) }

如何使用我的变量?

【问题讨论】:

    标签: url automated-tests e2e-testing cypress


    【解决方案1】:

    tl;博士

    Cypress commands are asynchronous,您必须使用then 来处理他们的收益。

    cy.url().then(url => {
      cy.get('.editor-toolbar-actions-save').click();
      cy.url().should('not.eq', url);
    });
    

    说明

    GitHub 上也有人问过类似的问题,official document on aliases 非常详细地解释了这种现象:

    您不能分配或使用任何赛普拉斯命令的返回值。命令被排队并异步运行。

    也显示了解决方案:

    要访问每个赛普拉斯命令产生的内容,请使用 .then()

    cy.get('button').then(($btn) => {
      // $btn is the object that the previous
      // command yielded us
    })
    

    查看core concepts docs's section on asynchronicity也是一个好主意。

    【讨论】:

    • 感谢我亲爱的,在我的代码上添加 cy.wait(2000) 后,它的工作
    • 请注意,Cypress 不是普通的等待/异步 JavaScript,而是使用自己的可链接的回调机制。只是为了确保您说“赛普拉斯是异步的”时没有混淆。
    • @totymedli 我有一个问题:``` it.only("LoginFunctionality", function () { cy.visit("hardCoddedLinkIsHere"); cy.get(".chakra-stack > .css-1n94901").click(); cy.contains("用xxxx登录").click(); cy.url().then((url) => { href = url; cy.log("href " , href); }); }); ''' it.only("验证登录", function () { cy.visit(href); cy.url().should("include", "something"); //断言我们在这个网址}); ``` 错误:cy.visit() 必须使用 url 或包含在 url 中的选项对象作为其第一个参数调用
    • @totymedli 请您检查
    • @Luna 您需要使用完整代码在一个新问题中提出这个问题。没有细节很难帮助你。检查您传递给cy.visit() 的内容。也许在你的第二个函数href 有一个意想不到的值。该变量未在该函数中声明,因此它必须来自上层范围。确保它具有适当的值。
    【解决方案2】:

    这些命令返回可链接的类型,而不是像字符串这样的原始值,因此将它们分配给变量将需要进一步的操作来“提取”字符串。

    为了获取url字符串,你需要这样做

    cy.url().then(urlString => //do whatever)
    

    【讨论】:

    • 感谢您的回答。但是当我使用这种格式时,我的 URL 会部分显示。预期 localhost:8080/editor/37b33c72-04a5-497e-882a-1c87aa7e48c7 不等于 localhost:8080/editor 我的 ID 未显示在 URL 中
    • @NarinePoghosyan 是的,因为您在单击按钮之前保存了 URL。你说随机id是在你保存物品后添加的。
    • 对不起,我说错了。首先,我添加任何文本并单击“保存”并想要获取 URL,在我更改文本后再次单击“保存”。我的 2 个带 ID 的 URL
    • 我更改了我的内容。
    • 谢谢亲爱的,添加 cy.wait(2000) 后就可以了
    【解决方案3】:

    参考下面的代码sn-p,在这里你可以得到当前的URL并将其存储在一个变量中,通过cy.log()进行打印

    context('Get Current URL', () => {
     
        it('Get current url and print', () => {
            cy.visit('https://docs.cypress.io/api/commands/url')
        
            cy.url().then(url => {
                const getUrl = url
                cy.log('Current URL is : '+getUrl)
            })
        })
    })
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,到目前为止,最一致的方法是将 URL 保存到文件中,并在您需要再次访问时从文件中读取它:

      //store the url into a file so that we can read it again elsewhere
      cy.url().then(url => {
          const saveLocation = `cypress/results/data/${Cypress.spec.name}.location.txt`
          cy.writeFile(saveLocation, getUrl)
      })
      
      //elsewhere read the file and do thing with it
      cy.readFile(`cypress/results/data/${Cypress.spec.name}.location.txt`).then((url) => {
          cy.log(`returning back to editor ${url}`)
          cy.visit(url)
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-16
        • 1970-01-01
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-13
        • 2014-12-20
        相关资源
        最近更新 更多