简体   繁体   English

如何保存元素的文本供以后在测试中使用

[英]How can I save the text of an element for later usage in the test

I want to get the text of a displayed time on a website.我想获取网站上显示时间的文本。

The object looks like this对象看起来像这样

<strong class="date-time ng-tns-c226-30 ng-star-inserted">Dec 14, 2022, 5:07:08 PM</strong>

I want to change the timezone on the website and then get the text-content of it again and compare whether the changes are correct.我想更改网站上的时区,然后再次获取它的文本内容并比较更改是否正确。

I've tried this:我试过这个:

cy.get('.date-time').invoke("text").as("oldtext")
cy.log("saved text: " + this.oldtext)

//Do some changes

cy.get('.date-time').invoke("text").as("newtext")
cy.log("saved text: " + this.newtext)

//Do comparison with the two strings

I have also tried doing this with resolving the promise that .invoke() returns but I have not found a way to get the text as string outside of a .then() block我也尝试通过解决.invoke()返回的承诺来做到这一点,但我还没有找到一种方法来将文本作为.then()块之外的字符串

let currentTime;
cy.get('.date-time') // select the element that displays the time
   .then(element => {
       currentTime = element.text()// get the text content of the element
       cy.log("inside: " + element.text()) // log the time inside the callback function
   });
cy.log("outside: " + currentTime)

But this always resulted in the output of the log being as follows但这总是导致日志的输出如下

log inside: Dec 14, 2022, 5:07:08 PM
log outside: undefined

To compare old and new text, you can track an element's changing property by saving the old values on the element.要比较新旧文本,您可以通过在元素上保存旧值来跟踪元素的更改属性。

cy.get('.date-time')
  .then($el => $el.oldtext = $el.text())    // save the text at this time

//Do some changes

cy.get('.date-time')
  .then($el => {
    expect($el.oldtext).not.to.eq($el.text())
  })

It's very useful for multiple "snapshots", like testing (x,y) positions in drag-and-drop.它对于多个“快照”非常有用,例如在拖放中测试 (x,y) 位置。 In this case, there may be many values, and you can just save them in an array.在这种情况下,可能会有很多值,你可以将它们保存在一个数组中。

BUT be careful if testing a React page and //Do some change causes the element to be re-created.但是如果测试 React 页面和//Do some change会导致重新创建元素,请小心。

In that case, you loose the old value, and you are better off saving the value externally在那种情况下,你失去了旧的价值,你最好在外部保存价值

let oldtext;

cy.get('.date-time')
  .then($el => oldtext = $el.text())    // save the text at this time

//Do some changes

cy.get('.date-time')
  .then($el => {
    expect(oldtext).not.to.eq($el.text())
  })

cy.log() has some interesting behavior when it enqueues the values to log, namely that it will evaluate the string when it is enqueued and not when it is actually run. cy.log()在将要记录的值排入队列时有一些有趣的行为,即它会在字符串排入队列时评估字符串,而不是在实际运行时评估字符串。

For example, the following:例如,以下内容:

let myVar = "foo";
cy.log(myVar) // prints "foo"
  .then(() => {
    myVar = "bar";
  })
  .log(myVar) // prints "foo"

To avoid this, this easiest way using aliases would be to only use cy.log() inside of a command in a Cypress chain, so it isn't enqueued before the value is set.为避免这种情况,使用别名的最简单方法是仅在 Cypress 链中的命令内部使用cy.log() ,因此它不会在设置值之前排队。

cy.get('.date-time').invoke("text").as("oldtext")
cy.get('@oldtext').then((oldText) => {
  cy.log(oldText);
});

If you need to compare the oldText and newText , you can do so by nesting the cy.get() commands.如果您需要比较oldTextnewText ,您可以通过嵌套cy.get()命令来实现。

cy.get('@oldText').then((oldText) => {
  cy.get('@newText').then((newText) => {
    cy.log(oldText);
    cy.log(newText);
  });
});

If you instead did not want to use aliases at all, you could store the values as Cypress.env() variables.相反,如果您根本不想使用别名,则可以将值存储为Cypress.env()变量。

cy.get('.date-time').invoke("text").then((oldText) => {
  Cypress.env('oldText', oldText);
});
// changes
cy.get('.date-time').invoke("text").then((newText) => {
  Cypress.env('newText', newText);
});
cy.then(() => {
  expect(Cypress.env('oldText')).to.not.eql(Cypress.env('newText'));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存变量/文本以便稍后在 Cypress 测试中使用? - How to save a variable/text to use later in Cypress test? 如何在测试用例中保存和重用动态DOM内容 - How to save & reuse dynamic DOM-content later in test case 如何在环境变量或 json 中保存 API 响应正文或属性,以便以后在赛普拉斯的其他请求中使用它 - How can I save API response body or property in an environment variable or json to use it later in other requests in Cypress 如何保存div文本以在其他地方使用? - How i can save div text for use in another place? 如何从 cypress 中括号中的文本元素中提取文本,即测试(5) - How to extract text from element having text in brackets in cypress i.e. test(5) 如何测试cypress中第二个元素的文本? - How to test the text of the second element in cypress? 如何通过按钮上的文本找到并单击 cypress 中的按钮元素 - How can I find and click button element in cypress by the text on the button 如何在稍后的测试中重用产生的值 - How to reuse yielded value later in the test 我们可以测试元素文本是否包含带有 cypress 的 text_A 或 text_B 吗? - Can we test if element text includes text_ A or text_B with cypress? 我如何计算元素以便以后可以使用该值? - how can i count the elements so i can use the value later?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM