Check tag classes with watir?

2019-02-23 07:29发布

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>

2条回答
唯我独甜
2楼-- · 2019-02-23 08:13

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
查看更多
你好瞎i
3楼-- · 2019-02-23 08:15

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

查看更多
登录 后发表回答