JavaScript Cypress测试运行时行为

huangapple go评论72阅读模式
英文:

Javascript cypress test runtime behavior

问题

I think this is a common Javascript question, rather than cypress test, yet I am not sure.

So I have a UI in web, which will show a text message like "found 3234 matches", I would like to use cypress to extract the number 3234, then on the next page, I will input 3234 in another input box.

My code is like

const MATCHING_MESSAGE = '[data-cy=matchingMessages]';

let count = 0;
cy.get(MATCHING_MESSAGE)
  .invoke('text')
  .then((text) => {
    const pattern = /[0-9]+/g;
    count = text.match(pattern).pop();
    console.log({ count1: count });
  })

// move to the next page

console.log({ count2: count });
if (count > 0) {
  // input count
} else {
  // input a min number like 5
}

However, when I execute this, the browser console is like

log: count2: 0
log: count1: 3234

Then the next page is always behaving like count is 0.

  1. 我知道这是执行顺序的问题,但为什么 "count2: 0" 这个日志会这么快执行,甚至在我转到第二页之前就被记录下来了?
  2. 此外,我缺少哪些有关JS的知识,以更好地理解执行顺序和Promise问题?
  3. 有办法可以强制执行顺序吗?

谢谢!

英文:

I think this is a common Javascript question, rather than cypress test, yet I am not sure.

So I have a a UI in web, which will show a text message like found 3234 matches, I would like to use cypress to extract the number 3234, then on next page, I will input 3234 in another input box.

My code is like

const MATCHING_MESSAGE = '[data-cy=matchingMessages]';

let count = 0;
cy.get(MATCHING_MESSAGE)
  .invoke('text')
  .then((text) => {
    const pattern = /[0-9]+/g;
    count = text.match(pattern).pop();
    console.log({count1: count});
  })

// move to next page

console.log({count2: count});
if (count > 0) {
  // input count
} else {
  // input a min number like 5
}

However, when I execute this. the browser console is like

log: count2: 0
log: count1: 3234

Then the next page is always behaving like count is 0

  1. I know this is execution order issue, but why log count2: 0 execute so fast like this get logged even before I moved to the second page.
  2. Also, what knowledge I am missing for JS, to get more of this execution order and promise issue.
  3. Is there a way I can enforce the execution order?

many thanks

答案1

得分: 2

你可以使用一个cy.then()命令,将其包装在使用count的代码周围。

这将确保在使用它之前已经对该值进行了评估。

const MATCHING_MESSAGE = '[data-cy=matchingMessages]';

let count = null;    // null不是有效的值,所以如果评估是0或3243,或者实际上没有发生,那么这是显而易见的

// 这个块是Cypress队列代码,所以它将在队列运行时稍后评估
cy.get(MATCHING_MESSAGE)
  .invoke('text')
  .then((text) => {
    const pattern = /[0-9]+/g;
    count = text.match(pattern).pop()
    console.log({count1: count});
  })

// 转到下一页

cy.then(() => {

  // 在then()内部:这段代码也在Cypress队列上运行,所以现在您可以获得正确的执行顺序

  console.log({count2: count});

  // parseInt,因为您要进行数值比较
  if (parseInt(count) > 0) {
    // 输入计数
  } else {
    // 输入最小数字,比如5
  }
})

页面刷新

执行// 转到下一页的部分可能会刷新浏览器。如果是这样,count变量将被完全清除。

如果您发现是这种情况,请将count添加到Cypress环境中。

const MATCHING_MESSAGE = '[data-cy=matchingMessages]';

cy.get(MATCHING_MESSAGE)
  .invoke('text')
  .then((text) => {
    const pattern = /[0-9]+/g;
    count = text.match(pattern).pop()
    console.log({count1: count});
    Cypress.env('count', count)         // 保存到环境存储
  })

// 转到下一页

cy.then(() => {

  // 在then()内部:这段代码也在Cypress队列上运行,所以现在您可以获得正确的执行顺序

  cont count = Cypress.env('count')   // 恢复值

  console.log({count2: count});

  // parseInt,因为您要进行数值比较
  if (parseInt(count) > 0) {
    // 输入计数
  } else {
    // 输入最小数字,比如5
  }
})
英文:

You can use a cy.then() command as a wrapper around the code that uses count.

This will make sure the value has been evaluated before using it.

const MATCHING_MESSAGE = '[data-cy=matchingMessages]';

let count = null;    // null is not a valid value, so it's obvious if the
                     // evaluation is 0 or 3243 - or not actually happened

// this block is Cypress queue code, so it will evaluate later when the queue runs
cy.get(MATCHING_MESSAGE)
  .invoke('text')
  .then((text) => {
    const pattern = /[0-9]+/g;
    count = text.match(pattern).pop()
    console.log({count1: count});
  })

// move to next page

cy.then(() => {

  // inside then(): this block of code also runs on the Cypress queue
  // so now you get the correct ordering of execution

  console.log({count2: count});

  // parseInt since you do a numeric compare
  if (parseInt(count) > 0) {
    // input count
  } else {
    // input a min number like 5
  }
})

Page refresh

The part that does // move to next page may refresh the browser. If so the count variable will be erased altogether.

If you find that is the case, add the count to the Cypress environment instead.

const MATCHING_MESSAGE = '[data-cy=matchingMessages]';

cy.get(MATCHING_MESSAGE)
  .invoke('text')
  .then((text) => {
    const pattern = /[0-9]+/g;
    count = text.match(pattern).pop()
    console.log({count1: count});
    Cypress.env('count', count)         // save to environment store
  })

// move to next page

cy.then(() => {

  // inside then(): this block of code also runs on the Cypress queue
  // so now you get the correct ordering of execution

  cont count = Cypress.env('count')   // restore the value

  console.log({count2: count});

  // parseInt since you do a numeric compare
  if (parseInt(count) > 0) {
    // input count
  } else {
    // input a min number like 5
  }
})

huangapple
  • 本文由 发表于 2023年6月2日 13:17:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定