Search This Blog

Subscribe:

Wednesday, September 8, 2010

How to use selenium fire event

I recently faced the challenge of using Selenium to automate a web application that stubbornly resisted my attempts to automate it - until I found the fireEvent!

No need to know the name of the application in question, but the basic situation I faced involved a form with a 'save' button.

I used Selenium to fill in the details in the form and click the save button.

When I run selenium I can see the text I enter on the form, and when the 'save' gets a click the little Ajax image for 'saving' appears, but when I check the data saved - nothing got saved.

I struggled with this on my own for a while then paired with someone to help.

Pairing brings a number of benefits:

* 2 minds instead of one
* you engage in the 'ah!' moments that only happen when you explain something to others
* you get a fresh perspective on the problem

So instead of focusing on the 'save' event we had a look at the form input fields.

The input fields had a few events - one having an onblur event. The onblur event code seemed to suggest that it persisted the form data into an in-memory store which the 'save' event then processed. So the problem did not seem to involve the save event - the type command did not trigger an onblur when it 'moved' between fields.

Onblur fires when you lose focus so we first tried clicking on the other fields to make that happen.

But that didn't work.

And then we noticed the Selenium API command fireEvent. This lets you trigger the event for a particular element on a page so we simply fired the 'blur' event for the component


selSession.fireEvent("edit_field_1", "blur");

And that worked - deep joy.

So lessons learned for me:

* Selenium has a large API - which I need to spend more time learning the scope and extent of
* When testing I need to use the Firefox/Firebug 'inspect element' functionality to learn a bit more about the app under test
* I need to use fireEvent more instead of some of the fudges present in my existing tests - this might help make them more robust.

General lessons:

* Any testing you do, learn the technology that underpins it
* Work with developers and use their technical knowledge to help

Tuesday, September 7, 2010

How to create selenium cookies

create cookie as:

selenium.createCookie("name=cookieone", "path=/");
selenium.createCookie("name=cookietwo", "max_age=60");

display it as:
String cook1=selenium.getCookie();
System.out.println("cookie"+cook1);

delete it:
selenium.deleteAllVisibleCookies();

How to use Selenium ClickAt Command

Folks nowadays I'm working on automation and open source tool selenium is been used. Frankly speaking, tool is awesome but still it has some loop holes, workarounds need to be done to get work done. I was trying to click on the rows on the grid and took me 2 hours to figure out how to do.

Here with an example I will tell how and what needs to be done.

Problem description: You had a grid with rows have values and need to click on the basis of values.

Grid looks like this.


abc

Some-Description1

Date1

Comments1

def

Some-Description2

Date2

Comments2

ghi

Some-Description3

Date3

Comments3

jkl

Some-Description4

Date4

Comments4

mno

Some-Description5

Date5

Comments5

pqr

Some-Description6

Date6

Comments6






Now, the requirement says, click on ghi which might open another window. (How to capture the window is not dealt here). When recording from selenium IDE, gave me click command with some XPATH but when I ran same command from IDE, it wasn't working at all.



Solution: Use clickAt(String locator, String co-ordinates)



Selenium IDE do not records clickAt(this was the catch here). I changed click to clickAt and it worked perfectly fine.



so command becomes :



selenium.clickAt("//div[contains(text(),\"ghi\")]","");





locator = "//div[contains(text(),\"ghi\")]"

co-ordinates = ""



This command says, click at the link (given by the XPATH) which has text ghi with no co-ordinates.



Same way to click on grid abc =



selenium.clickAt("//div[contains(text(),\"abc\")]","");














How to capture screenshot in selenium

Its easy to take a screenshot in selenium . We just need to use the below command.

selenium.captureEntirePageScreenshot("F:\\fullscreenshot","background=#CCFFDD" );

Screenshot will be taken at the time of execution of testcase and will be stored as png file in respective folder as mentioned .In our case it is F:\ fullscreenshot.png.

Enjoy Testing
www.mpowerglobal.com

Thursday, September 2, 2010

Running your first selenium test case in eclipse

1- In FireFox ,Start Your Selenium-IDE from tools->Selenium IDE
2- Record your script
3- Go to File->Export test case as->Choose the programing language as JAVA and save your script.
4-Save it with .java extension

The next step is to open your eclipse and create a project.

1- Creating your java project in eclipse
The first step in eclipse is to create/add your project. To do this follow the steps below:
-> Launch Eclipse
-> Right click in navigator->Create a java project named TestProject (for example)

2- The next step is to add JUnit Libraries which consists of following steps:
-> Go to Project Properties
-> Go to Java Build Path
-> Click on Add external-jar button in Libraries tab
-> Browse and go to the location where u stored ur junit.jar.

4-Add Selenium Java Client Jar into Project:-
Once you added the Junit jars , the next step is to add selenium jar file. you can find selenium-server.jar under selenium-remote-control\selenium-java-client-driver folder what you have downloaded from

http://selenium-rc.openqa.org/download.html

To add this Jar file follow the below steps:
-> Go to Project Properties
-> Go to Java Build Path
-> Click on Libraries tab
-> Click on 'Add External Jars' button
-> Browse and add the 'selenium-server.jar' from the source where you had saved it.

5- Initiating the Selenium Test Server
To start the selenium test server, first go to the location where your selenium-server.jar is placed.
and then Run the command java -jar selenium-server.jar

This will start you selenium server.

6- Adding your test (Java class ) in our Project
To add you test (which you have saved earlier) as a java class file to the project (TestProject, already saved) follow the steps:
-> In eclipse go to create a new java class
-> Create it with name MyFirstTestCase (for example) and paste the code from the file u had saved it
-> Choose "com.thoughtworks.selenium.SeleneseTestCase" as its super class
-> Finish it

7- Modifying your test script (java class)
Once you are done with ur class file, it will look like,

Package.*
import com.thoughtworks.selenium.SeleneseTestCase ;
public class MyFirstTestCase extends SeleneseTestCases {
public void setUp() throws Exception {
setUp( " http://www.google.com/ " , "*firefox'); }

............ You test script..............
}

In case you want to run this in diff browser then simply you can change your script
setUp( " http://www.google.com/ " , "*firefox');
to setUp( " http://www.google.com/ " , "*chrome'); or whatever browser you need to run with.

8- Running your test
Finally we are ready to tun our first test. To run this in eclipse you click on Run button
select option Run as "JUnit Test'

So finally I am done with your end to end execution of ur java test case using selenium RC in eclipse. For report generation using JUnit in eclipse,check my other post Thanks.



For more info,check blogs of http://www.mpowerglobal.com

how to create report in JUnit

Its very simple to create JUnit reports using eclipse

follow these steps and it worked for me

1) First generate an Ant build file for your project.
In the Export page select "Ant Buildfiles" it is under General. Then select a
project that contains your JUnit tests. The default JUnit output directory is
appropriately named "junit".


2) Next resolve dependencies by making sure that the junit.jar is added to Ant's
"Global Entries."
Window->Preferences->Ant->Runtime->Global Entries and Add External JARs...
Navigate to your "eclipse" directory (where eclipse is installed). It is under
"plugins" directory and presently the junit.jar is in a directory named
"org.junit_3.8.1". If this is missing then you will see error messages since
junit is an optional Ant task.




3) Run then right click on the Ant build file (by default build.xml) and Run
As->Ant Build... This will display a list of targets. All of the launch
configurations you have previously configured will have a corresponding target
in your Ant build file. Select the desired target(s), and also select the
"junitreport" target. Check the "Target execution order" text area to make sure
the junitreport is run last.



Whenever you run a test case it will generate results and will be placed in the
junit output directory. These results are formatted into an HTML report by the
junitreport target and stored in the junit output directory. This is a quick explanation of the process, but hopefully it helps get you off your feet.



For more info,check out http://www.mpowerglobal.com/web/guest/blog

Integrate jmeter with selenium junit

Step1. Record and convert your script to selenium java-junit format
using selenium ide.

Step2. open the eclipse and save that file as a java file.

Step3. Using junit run that file but before that you need to add
selenium-server.jar file in eclipse.

Step4. After that test runs successfully you will get .class file
under bin folder.

Step5. Setup your Run configuration settings for converting java file
to jar.

Step6. Select .java file and .class together and convert to .jar file.

Step7. Paste that .jar file into lib/junit folder.

Step8. start your j-meter server and see the magic.