抓取字符串以在cypress的.visit()调用中使用 [英] Grab a string to use in a .visit() call in cypress

查看:141
本文介绍了抓取字符串以在cypress的.visit()调用中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个dom元素,其中包含我想访问的字符串或URL。我为dom元素添加了data属性,以方便参考。

I have a dom element that contains the string or a url that I would like to visit. I have labelled the dom element with a data attribute for easy reference.

上面以粗体显示 Create Topic的是字符串,在控制台中,您可以看到它具有 data-test ='topicUrl 属性

Above where it says 'Create Topic' in bold is the string and in the console, you can see it has a data-test='topicUrl attribute.

我想捕获此字符串值,以便以后可以访问网址。

I want to capture this string value so that I can visit the url a a later point.

I关注了有关变量和别名的文档,并尝试了

cy.get('[data-test="topicUrl"]').invoke('text').as('Url')

这样我就可以使用

cy.visit(this.Url)

但是这不起作用,它会出现 TypeError错误:Cann在控制台中读取了未定义的属性 Url

But that doesn't work, it errors out with TypeError: Cannot read property 'Url' of undefined in the console.

如何获取DOM元素中的文本以便可以使用它要在以后访问URL?

How do I grab the text in a DOM element so that I can use it to visit a url at a later point?

推荐答案

您可以将其缓存到变量并发出 cy.visit ,以确保变量已填充:

You can cache it to a variable and issue the cy.visit in a callback to ensure the variable is populated:

let url;
cy.get('[data-test="topicUrl"]').invoke('text')
    .then( value => {
        url = value;
    });

cy.then(() => {

    return cy.visit(url);
});

直接使用 cy.then()(而不是将其链接到另一个命令上),没有记录,并且将来可能在实现 cy.resolve(promise)时删除。

Using cy.then() directly (instead of chaining it off of another command) isn't documented and might be removed in the future when cy.resolve( promise ) is implemented.

也许最好使用 cy.wrap()。then(()=> {}),它应该永远有效(?)。

Maybe it's better to use cy.wrap().then(() => {}) which should work forever (?).

(完全来自内存,因此lemme知道它是否不起作用,有机会运行时我会进行更新。)

(going entirely from memory, so lemme know if it's not working and I'll update when I get a chance to run it.)

这篇关于抓取字符串以在cypress的.visit()调用中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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