Getting style attributes of web-elements using Selenium IDE

Let’s assume you have an element of a web-page that contains an attribute, that you need to have access to, within its style property. Even more, this attribute is changed by your application, and you want to test whether it now corresponds to a correct value, or there is a mistake.

Element’s HTML:

<span id="imgStats" class="imgStats" style="background-image: url("yoursite.com/image.png");">

What we are looking for is a comparison of  yoursite.com/image.png to an actual value.

Solution: access it by xPath and use getXpathCount() method of SeleniumIDE in PHPUnit:

$this->assertEquals("1", $this->getXpathCount("xpath=id('imgStats')[@style='background-image: url(\"/yoursite.com/img.png\");']"));

This method counts a number of elements that have a particular xpath. In this case it is expected to be equal to 1. If you have any other attributes in style, you must specify them in the selector. Otherwise it won’t work. Also take into account a syntax of this expression, it took one hour to figure out a correct sequence of all these semicolons and brackets 🙂

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s