如何在其他非cypres函数中使用HTML元素中的文本?

发布于 2025-01-25 19:09:27 字数 434 浏览 2 评论 0原文

我需要从HTML元素中获取文本以在其他检查中使用它。

但是,此检查必须在其他地方进行,而不是文本获取的内部功能。

这就是为什么

> cy.get('#at__title').invoke('text').then((storedValue) => {
>    storedValue
> })

不适合套件的变体的原因,因为我只能在然后中使用文本。

我也尝试使用

> cy.get('#at__title').invoke('text').as('element_text')
> this.element_text

,但控制台日志显示this.element_text undefined。

I need to get text from html element to use it in other checking.

But this checking must be done elsewhere later, not inside function of text getting.

That's why such variant as

> cy.get('#at__title').invoke('text').then((storedValue) => {
>    storedValue
> })

doesn't suite, because I can use text only inside then.

Also I tried to use

> cy.get('#at__title').invoke('text').as('element_text')
> this.element_text

but console log shows this.element_text as undefined.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随风而去 2025-02-01 19:09:27

使用别名是正确的cy.get('#at __title')。indoke('text')。as('element_text'),但要访问这样的值,

this.element_text

就需要测试对 <> <强>不是 是箭头函数。

使用此格式

it('test something', function() { 
  this.element_text

或使用较长的语法访问别名

cy.get('@element_text').then(element_text => { 

Using an alias is correct cy.get('#at__title').invoke('text').as('element_text') but to access the value like this

this.element_text

requires the test to not be an arrow function.

Use this format

it('test something', function() { 
  this.element_text

Or use the longer syntax for accessing an alias

cy.get('@element_text').then(element_text => { 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文