如何使用赛普拉斯获取 html 代码中未显示的元素值?
How to get an element value that is not showing in html code with Cypress?
这是我的元素的代码
<input type="text" class="nameOfClass" id="someid" name="somename" maxlength="255" placeholder="justholder" ng-model="model" tooltip-placement="top" tooltip-trigger="mouseenter" tooltip-animation="false" style="">
如您所见,没有属性值,但我可以清楚地看到我试图自动化的 Web 应用程序的文本字段中有文本。
所以我的问题是,我不知道如何获取文本字段的值。
我试过 google chrome inspector 来查找值在哪里,但没有任何运气。我在某处读到,缓存可能导致此问题,但在网络控制台中我可以看到请求响应中的值。
谢谢
假设你想获取写在文本字段中的值,你可以通过调用val
.
来获取它
cy.get('#someid')
.invoke('val')
.then((value) => {
cy.log(value) //logs the value
})
如果您想对值应用任何断言,您可以:
cy.get('#someid').should('have.value', 'your-value')
如果您指的是 type="text"
,那不是用户输入的文本 - 它是一个属性,告诉输入允许哪些值。
您还可以有 type="number"
、type="date"
、type="color"
、etc
检查输入的类型是否为“文本”,
cy.get('input#someid').should('have.attr', 'type', 'text')
检查值 属性 将像这样完成
cy.get('input#someid')
.type('entering a value') // there's nothing in value yet
.should('have.value', 'entering a value')
这是我的元素的代码
<input type="text" class="nameOfClass" id="someid" name="somename" maxlength="255" placeholder="justholder" ng-model="model" tooltip-placement="top" tooltip-trigger="mouseenter" tooltip-animation="false" style="">
如您所见,没有属性值,但我可以清楚地看到我试图自动化的 Web 应用程序的文本字段中有文本。
所以我的问题是,我不知道如何获取文本字段的值。 我试过 google chrome inspector 来查找值在哪里,但没有任何运气。我在某处读到,缓存可能导致此问题,但在网络控制台中我可以看到请求响应中的值。
谢谢
假设你想获取写在文本字段中的值,你可以通过调用val
.
cy.get('#someid')
.invoke('val')
.then((value) => {
cy.log(value) //logs the value
})
如果您想对值应用任何断言,您可以:
cy.get('#someid').should('have.value', 'your-value')
如果您指的是 type="text"
,那不是用户输入的文本 - 它是一个属性,告诉输入允许哪些值。
您还可以有 type="number"
、type="date"
、type="color"
、etc
检查输入的类型是否为“文本”,
cy.get('input#someid').should('have.attr', 'type', 'text')
检查值 属性 将像这样完成
cy.get('input#someid')
.type('entering a value') // there's nothing in value yet
.should('have.value', 'entering a value')