我正在尝试从元素(输入标签)获取文本并将其存储在变量中。
以下语句用于在文本字段中设置数据。
cy.get('app-screen).find('input[id="studentName"]').type("Viola");
Run Code Online (Sandbox Code Playgroud)
尝试使用以下语句来获取文本:
cy.get('app-screen).find('input[id="studentName"]').then(($text1) => {
let textValue1 = $text1.text());
cy.log('Student Name: ' + textValue1 );
});
cy.get('app-screen).find('input[id="studentName"]').invoke('text').then(text2 => {
let textValue2 = text2;
cy.log('Student Name: ' + textValue2 );
});
Run Code Online (Sandbox Code Playgroud)
这两种方式的输出都是空的,如下所示。
Student Name:
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙解决这个问题吗?
假设在type命令之后键入的值被保存在value属性中。另外,您可以使用别名来保存内部文本,例如:
cy.get('app-screen').find('input[id="studentName"]').invoke('val').as('name')
cy.get('@name').then((name) => {
cy.log('Student Name: ' + name) //prints name
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25140 次 |
| 最近记录: |