从元素中获取文本并存储在柏树的变量中
get text from an element and store in a variable in cypress
我正在尝试从元素(输入标签)中获取文本并将其存储在变量中。
下面的语句用于在文本字段中设置数据。
cy.get('app-screen).find('input[id="studentName"]').type("Viola");
尝试使用以下语句获取文本:
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 );
});
两种方式的输出都是空的,如下所示。
Student Name:
谁能帮忙解决这个问题?
假设在 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
})
您必须检查元素如何存储您在 UI 中查看的文本。
注意:.invoke('text')
使用 jquery 方法 .text()
获取元素的 textContent
。要获得 innerText
那么你必须使用回调函数。
如果您要注销文本值,则可以执行以下操作之一:
cy.get('app-screen')
.find('input[id="studentName"]')
.invoke('val')
.then(cy.log) // this will log out the text
cy.get('app-screen')
.find('input[id="studentName"]')
.then($el => { cy.log($el[0].innerText }) // this will log out the innerText
我正在尝试从元素(输入标签)中获取文本并将其存储在变量中。
下面的语句用于在文本字段中设置数据。
cy.get('app-screen).find('input[id="studentName"]').type("Viola");
尝试使用以下语句获取文本:
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 );
});
两种方式的输出都是空的,如下所示。
Student Name:
谁能帮忙解决这个问题?
假设在 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
})
您必须检查元素如何存储您在 UI 中查看的文本。
注意:.invoke('text')
使用 jquery 方法 .text()
获取元素的 textContent
。要获得 innerText
那么你必须使用回调函数。
如果您要注销文本值,则可以执行以下操作之一:
cy.get('app-screen')
.find('input[id="studentName"]')
.invoke('val')
.then(cy.log) // this will log out the text
cy.get('app-screen')
.find('input[id="studentName"]')
.then($el => { cy.log($el[0].innerText }) // this will log out the innerText