Tuesday 4 September 2012

Verify the height and width of image in Selenium


You can verify image height and width with help of selenium.

I have shared sample script. In this script i'm storing width and height of image and then comparing it with expected width and height.

CODE - 

int wid = (Integer) selenium.getElementWidth("css=p > img.medialeft");
int hght = (Integer) selenium.getElementHeight("css=p > img.medialeft");
System.out.println(wid);
System.out.println(hght);
 if ((hght == 234) && (wid == 140))
    {
      
       System.out.println("image size" +wid + " X "  + hght + "pixels");
  
     }

 Else
   {
       System.out.println("Image width and height is not matching with expected");
   }



Here expected width and height is 140 and 234 respectively.
In if condition it is checking whether width and height is same as required, if it is same it will print, else will move to other part or any error message.





Compare a variable with blank value in selenium



To compare variable with blank value, you can try solution like suggested      below - 
Here 'test' and 'j' are two variable. j having blank value.
It will compare 'test' and 'j' variable and accordingly it will display message  whether test variable is blank or not.


storeValue  |  element  |  test
store          |           |  j  (blank value)
gotoIf        |  storedVars['test']==storedVars['j']  |  true
getEval      |  alert("test Variable Value is not blank")
gotolabel   |  finish
label         |  true
getEval      |  alert("test Variable Value is blank")
label         |  finish


Xpath to determine checkbox “checked” attribute

If there is multiple check-boxes with same ids and you have to select the next check-box which is not selected/checked. 
For such scenario you can refer below script - 




Here 'stnewrecord' is class of check-box. Script will store number of check-box, and according to that loop will follow. It will check whether check-box is checked or not if not it will check else move to next step.
xpath=(//input[@class='stnewrecord'])[${i}]  
This is xpath of check-box. 'i' is position, it will increase on each iteration.

Configure Selenium RC With JUnit Using Eclipse



Before configuring Selenium RC with Junit Using Eclipse below components are required - 


1.Install JRE : http://www.java.com/en/download/
2.Download Eclipse IDE for Java Developers : http://www.eclipse.org/downloads/
3. Download Latest version of JUnit: https://github.com/KentBeck/junit/downloads
4.Download and Install Selenium IDE firefox plugin : http://seleniumhq.org/download/
5.Download Selenium RC server and Selenium Client Drivers for Java:http://seleniumhq.org/download/



Once done with above steps launch Eclipse.
After launching eclipse, you need to Create New Project for selenium scripts

Go to File > New > Project then In Select a Wizard Click Java Project. Then provide all details like project name.

Once new project is created, right click on project go to Build Path > Configure Build Path.
Click on Libraries menu when pop-up appears, then click to "Add External Jar's ". Select Selenium RC server and Selenium Client Drivers for Java which is downloaded from http://seleniumhq.org/download/.



Once it is done, now build path is configured and project is ready to use.
Now create a package for project.
Once package is created then next step will be to create class.Right click on Package and add new class, it will ask for name. Click on Finish once class name is provided.



Once done with all above steps, your package explorer looks like below -




Once class is created you are ready to write script for selenium scenario. As well you can execute created script on eclipse IDE.

Thank you...