Check tag classes with watir?

我的未来我决定 提交于 2019-12-01 18:14:15

问题


I have a div that changes based on if the form was submitted correctly or not.
I wanted to know if it's possible to check a specific element for a class or not?

Starting out the element looks like this.

<div id="myerrortest" class="input text"></div>

If the input isn't correct add the error class.

<div id="myerrortest" class="input text error"></div>

回答1:


Try this:

browser.div(:id => "myerrortest").class_name

More information:

http://watir.github.com/watir-webdriver/doc/Watir/HTMLElement.html#class_name-instance_method

Another alternative would be to just see if the div with class you expect exists or not

browser.div((:id => "myerrortest", :class => 'input text').exists?

If using rSpec type matchers it would be

browser.div((:id => "myerrortest", :class => 'input text').should exist



回答2:


You can do this with jquery (or not, jquery sintax is only more simple but with getelement you can do anything).

This is an example that add class with a button click:

$(".button").click(function(){  
$("#myerrortest").attr("class",$("#myerrortest").attr("class")+" error");
});

this work also with $(".classname"). Hope it help



来源:https://stackoverflow.com/questions/10807743/check-tag-classes-with-watir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!