[英]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.如果您需要比较oldText
和newText
,您可以通过嵌套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.