Pages

Search This Blog

Thursday, October 21, 2010

How to verify the text ignoring cases sensitivity in selenium ?

How to verify the text ignoring cases sensitivity in selenium ?

Some times we need to verify the text on the webpage no matter is upper cases or lower cases or proper.
We just need to verify the text on the web page ignoring their cases sensitivity.
REGEXPI is used to avoid cases sensitive to verify the text on the page.

Lets say i want to verify the text "Automationtricks is good blog for selenium" No matter its in upper cases or lower case or proper case.

Then use REGEXPI in selenium command selenium.isTextPresent or verifyTextPresent.


<tr>
    <td>verifyTextPresent</td>
    <td>regexpi:AUTOMATIONTRICKS IS GOOD BLOG FOR SELENIUM</td>
    <td></td>
</tr>

<tr>
    <td>verifyTextPresent</td>
    <td>regexpi:AUTOMATIONTRICKS is good blog for selenium</td>
    <td></td>
</tr>

<tr>
    <td>verifyTextPresent</td>
    <td>regexpi:AUTOMAtionTRICKS Is GoOd Blog for SELENIUM</td>
    <td></td>
</tr>

<tr>
    <td>verifyTextPresent</td>
    <td>regexpi:automationtricks is good blog for selenium</td>
    <td></td>
</tr>


In Selenium RC

verifyTrue(selenium.isTextPresent("regexpi:AUTOMATIONTRICKS IS GOOD BLOG FOR SELENIUM"));

verifyTrue(selenium.isTextPresent("regexpi:AUTOMATIONTRICKS is good blog for selenium"));

verifyTrue(selenium.isTextPresent("regexpi:AUTOMAtionTRICKS Is GoOd Blog for SELENIUM"));

verifyTrue(selenium.isTextPresent("regexpi:automationtricks is good blog for selenium"));


Wednesday, October 6, 2010

How to verify value in drop down list.

How to verify value in drop down list.
We somehow manage to find the element on the page but its difficult to fund element with some specific attributes.
Take an example How to find some value is in drop down list or now? To find element drop down is easy but to find a drop down having some specific
value in their list is difficult.

Go to www.ebay.in you will see there is drop down along with search text box.
How to veify a specific value "Collectibles" is in drop down list.

Using Xpath:

<tr>
 <td>verifyElementPresent</td>
 <td>//select[@id='_sacat']/option[contains(text(),'Collectibles')]</td>
 <td></td>
</tr>

verifyTrue(selenium.isElementPresent("//select[@id='_sacat']/option[contains(text(),'Collectibles')]"));


Using CSS:

<tr>
 <td>verifyElementPresent</td>
 <td>css=select#_sacat>option:contains("Fun")</td>
 <td></td>
</tr>

verifyTrue(selenium.isElementPresent("css=select#_sacat>option:contains(\"Fun\")"));

<tr>
 <td>verifyElementPresent</td>
 <td>css=select>option:contains("Fun")</td>
 <td></td>
</tr>


How to select the value in drop down using regular expression.

There are situation when we need to select the drop down value with some partial text .
With the help of regular expression we can select the drop down value

<tr>
 <td>select</td>
 <td>_sacat</td>
 <td>label=regexp:Fun*</td>
</tr>

selenium.select("_sacat", "label=regexp:Fun*");


Friday, October 1, 2010

How to use CSS in Selenium to locate an element

XPath locator is one of the most precise locator. But this has a disadvantage of its locator types thats is its slowness.
This disadvantage of xpath you can see when running the tests under IE while Firefox works with xpath pretty faster than IE.
The main thing is that tests which intensively use XPath work extremely slow under IE and this feature is the cause of huge variety of problems related to execution speed as well as quality of the tests itself especially during operations with dynamic content.
For this reason CSS locators can be good alternative of XPath. What can we do with CSS locators?

CSS locator will give you clear picture of your element hierarchy
lets say your xpath of an element is like,

xpath=//div//span/a

the same locatore can be identified by CSS is .

css=div * span > a

from the above example there are two symbol are being used to locate an element.

1) "/" in xpath is just a next level of DOM element hierarchy and same used in CSS is ">"
2) "//" in xpath is any DOM object hierarchy level under current object and same used in CSS is "*"

The way we use attributes in xpath we can use attributes in css as well. lets say your element's xpath is like this

//input[@name='continue' and @type='button']

can be written in CSS

css=input[name='continue'][type='button']
or
css=input[name=continue][type=button]

in xpath we can check the partial attribute value matching but in CSS we can't.

lets say your element is like this

<div title="here is m multiword title" />

so the xpath to locate this element is .

xpath=//div[contains(@title,"title")]

and same can be used in CSS like

css=div[title~=title]

But if your element is like this

<div title="my_title" />

then CSS locator css=div[title~=title] wont work here .

CSS provides us the easy way to specify the element.
lets say your element is like this,

<input id="username"></input>

we can write in xpath like this

xpath=//input[@id="username"]

and same can be specified in CSS

css=input#username

so whenever we want to identify any element with their id then we use #

lets say your element is like this,

<input class="password"></input>

then xpath is

xpath=//input[@class="password"]

and corresponding CSS is

css=input.password

so whenever we want to identify any element with their class then we use .

below are the element sturcture used in above examples.

<html>
<body>
<form>
 <input id="username"></input>
 <input class="password"></input>
 <input name="continue" type="button"></input>
 <input name="cancel" type="button"></input>
 <input value="a86b504a-faff-4a18-92d8-68720331c798" name="vid" type="hidden"><input value="LF_c10cf6d6" name="lf_cid" type="hidden"></form>
 </body>
</html>



An interesting feature of CSS in Selenium :Sub-String matches.

^= Match a prefix
$= Match a suffix
*= Match a substring

1 css=a[id^='id_prefix_']

A link with an “id” that starts with the text “id_prefix_”

2 css=a[id$='_id_sufix']

A link with an “id” that ends with the text “_id_sufix”

3 css=a[id*='id_pattern']

A link with an “id” that contains the text “id_pattern”

Without specifying the type of element we can identify the element
to identify the google search button we can use Css like this

css=center > #sbds > #sblsbb > input

these CSS features are awesome more details can be found on
How to use CSS in Selenium extensively
and if you want to know in depth then visit this page.
http://www.w3.org/TR/css3-selectors/