赛普拉斯:设置属性值 [英] Cypress: set attribute value

查看:126
本文介绍了赛普拉斯:设置属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始研究赛普拉斯,遇到了一个问题:是否可以借助 JavascriptExecutor 选择一个具体的属性并像Selenium中那样更改其值?例如,让我们看一下这段简单的代码

  input id = mapsearch type = textbox class = form-control  name = address test = 

是否可以获取测试属性并分配我的

解决方案

是。在赛普拉斯测试中,您可以使用JavaScript进行任何操作。对于上面的html,您可以在Cypress中使用 .invoke()

  cy.get('input [test]')
.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 code

input 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆