cy.url() 和/或 cy.location('href') 不返回字符串 [英] cy.url() and/or cy.location('href') does not return a string

查看:18
本文介绍了cy.url() 和/或 cy.location('href') 不返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an editor page. When I add any content and click the "Save" button my URL will change, adding a random id in the URL. I want to check if my ID's are changing every time when I click the "Save button".

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

I save the URL result in variable and want to check it, I do it like this:

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

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

But my currentURL variable's type is not string:

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

expected http://localhost:8080/editor/37b44d4d-48b7-4d19-b3de-56b38fc9f951 to not equal { Object (chainerId, firstCall) }

如何使用我的变量?

推荐答案

tl;dr

Cypress 命令是异步的,您必须使用 then 才能工作与他们的产量.

tl;dr

Cypress commands are asynchronous, you have to use then to work with their yields.

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

说明

GitHub关于别名的官方文档 非常详细地解释了这种现象:

Explanation

A similar question was asked on GitHub, and the official document on aliases explains this phenomenon in great detail:

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

You cannot assign or work with the return values of any Cypress command. Commands are enqueued and run asynchronously.

也显示了解决方案:

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

To access what each Cypress command yields you use .then().

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

查看 核心概念文档关于异步性的部分.

这篇关于cy.url() 和/或 cy.location('href') 不返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆