赛普拉斯:设置属性值 [英] Cypress: set attribute value
问题描述
我刚刚开始研究赛普拉斯,遇到了一个问题:是否可以借助 JavascriptExecutor
选择一个具体的属性并像Selenium中那样更改其值?例如,让我们看一下这段简单的代码
input id = mapsearch type = textbox class = form-control name = address test =
是否可以获取测试属性并分配我的
是。在赛普拉斯测试中,您可以使用JavaScript进行任何操作。对于上面的html,您可以在Cypress中使用 .invoke()
:
cy.get('input [test]')
$ Cypress在后台使用jQuery,因此您可以像这样调用jQuery中的任何函数。您也可以使用
.invoke(' attr','test','my new value')
.should('have.attr','test','my new value')
。 ()
:cy.get('input [test]')。 then(function($ input){
$ input [0] .setAttribute('test','my new value')
})
.should('have.attr','测试,我的新值)
I have just started exploring Cypress and came across such a problem: is it possible to select a concrete attribute and change its value like in Selenium with the help of
JavascriptExecutor
? For example lets take this simple piece of codeinput id="mapsearch" type="textbox" class="form-control" name="address" test=""
Is it possible to get the test attribute and assign my own value?
解决方案Yes. Anything you can do in JavaScript is possible within Cypress tests. For the html above, you could do this within Cypress using
.invoke()
:cy.get('input[test]') .invoke('attr', 'test', 'my new value') .should('have.attr', 'test', 'my new value')
Cypress uses jQuery under the hood, so you can invoke any function in jQuery like this. You could alternatively use plain JavaScript after yielding the input using
.then()
:cy.get('input[test]').then(function($input){ $input[0].setAttribute('test', 'my new value') }) .should('have.attr', 'test', 'my new value')
这篇关于赛普拉斯:设置属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!