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

查看:174
本文介绍了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() .

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

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

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

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