稍后在测试中如何重用产值 [英] How to reuse yielded value later in the test

查看:94
本文介绍了稍后在测试中如何重用产值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:我在第1页中有一个具有innerText的元素,在第2页中有另一个具有innerText的元素。我需要在第3页中比较这两个值。因此,我需要找到一种将这些值全局保存到变量中的方法,以便

Context: I have an element with innerText in Page1 and another element with innerText on Page 2. And I need to compare these two values in Page 3. So I need to find a way to save these values in a variable globally so that they can be used later.

我尝试过的事情:

试验1:无效,因为page1value范围仅限于cy.get()

Trial 1: Didn't work as page1value scope is limited to the cy.get()

  cy.get('#selector').invoke('text').then(text => {
      const page1value = text
  })

试用2:并没有像我每次尝试在外部打印该值,它是未定义值或用来初始化它的值一样。

Trial 2: Didn't work as whenever I try to print the value outside, it comes as undefined or the value with which it was initialized it with.

it('TC Name', () => {

    let page1value,
        cy.get('#selector').invoke('text').then(text => {
            page1value = text
        })

    cy.log(page1value) //comes as undefined

})

试用3:使用.as()以及未定义的名称。

Trial 3: Using .as() as well its coming as undefined.

let page1value;
cy.get('#selector').invoke('text').as('page1value');
cy.log(page1value) //comes as undefined

如果有人可以告诉我我在哪里做错了。

It would be great if someone could tell me where I am doing wrong.

推荐答案

将Cypress命令推入(排队)到队列中(称为命令队列- -基本上是一个数组),然后依次执行(一个接一个)并异步执行。

Cypress commands are pushed (enqueued) into a queue (called the Command queue --- which is basically an array), and then executed serially (one after another), and asynchronously.

而您的 cy.log() 也将异步执行,在上一条命令之后,您传递给它的值( page1value )将同时同步传递/求值。您将命令推送到队列(在测试开始时调用传递给 it()的回调---时进行评估)。

While your cy.log() will be executed asynchronously, too, after the previous command, the value you pass to it (page1value) is passed/evaluated synchronously, at the time you push the command to the queue (which is evaluated at the time the callback passed to it() is called --- at the beginning of the test).

这只是常规的JavaScript行为,与赛普拉斯无关。所有 cy。* 命令只是 cy 对象上的方法(函数),它们会立即被调用。立即不被调用(执行)的是每个命令执行的逻辑(例如,查询DOM以查找您提供给 cy.get()的选择器,登录到命令日志)当您调用 cy.log('string')等)时。

This is just regular JavaScript behavior, and has nothing to do with Cypress. All the commands cy.* are just methods (functions) on the cy object, and they're called immediately. What is not called (executed) immediately, is the logic that each command does (e.g. query the DOM for the selector you supply to cy.get(), log to Command log when you call cy.log('string'), etc.).

因此,在第二个示例中:

Thus, in your 2nd example:


  1. 您声明 page1value

  2. 您随后立即将命令 cy.get() cy.invoke cy排队。 then()

  3. 您还立即将 cy.log 入队,您将 page1value (目前仍未定义 )。

  4. 排队,他们开始执行,从上到下。当 cy.then 命令轮流执行时,会分配 page1value 变量,但不再使用(读)在其余测试中的任何位置(回想一下,在上一步将其传递给 cy.log 命令时,您已经阅读了它。)
  1. You declare page1value.
  2. You then immediately enqueue commands cy.get(), cy.invoke, cy.then().
  3. And you also immediately enqueue cy.log, to which you pass page1value (which at this time is still undefined).
  4. After all commands are enqueued, they start to execute, from top to bottom. When the cy.then command takes turn to execute, the page1value variable is assigned, but it's no longer used (read) anywhere for the rest of the test (recall that you've already read it when you passed it to the cy.log command in the previous step).

因此,您要做的是:

cy.get('#selector').invoke('text').then(text => {
  cy.log(text);
});

在第3个示例中,如果您对某些内容进行别名,则需要使用另一个命令来访问该值(请记住,所有内容都是异步的,因此您无法以同步的方式访问异步设置的值),在这种情况下,是 cy.get('@ aliasName')

In your 3rd example, if you alias something, you need to access that value using another command (remember, everything is asynchronous, so you can't access values that are set asynchronously, in a synchronous manner as you're doing), in this case cy.get('@aliasName'):

cy.get('#selector').invoke('text').as('page1value');
cy.get('@page1value').then( value => {
  cy.log(value);
});

请注意,以上解释略有不准确和无遗漏之处(幕后发生的事情更多) , 为了简单起见。但是作为介绍工作原理的介绍,它们应该做。

Note that the above explanations are slightly inaccurate and inexhaustive (there are more things going on behind the scenes), for the sake of simplicity. But as an intro to how things work, they should do.

无论如何,您绝对应该阅读赛普拉斯简介

Anyway, you should definitely read Introduction to Cypress.

您也可以看看我的较早答案,这些答案涉及相关概念:

You may also take a look at my older answers that touch on related concepts:

  • How to use a variable as a parameter in an API call in Cypress
  • In Cypress when to use Custom Command vs Task?

这篇关于稍后在测试中如何重用产值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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