Wednesday, February 29, 2012

Selenium

Selenium IDE


Selenium IDE is an IDE that record and playback test cases to debug your web applications. Selenium is opensource so you can download it free and view its source code from here


Selenium can be installed in to your firefox web browser easily as a plugin and at the very first moment you have installed Selenium you can experience its powerful capabilities of recording test cases.


There is a tool called Selenium Remote Control that has ability to map Selenium with other browsers and platforms such as Chrome , Opera and Safari. This Selenium Remote Control creates javascript injections through those browsers so that Selenium has become more powerful enough to handle test cases which uses AJAX (The technology used to transfer or retrieve data without refreshing the browser)


So... Enough introduction :) Let's get back to work :)


Say hello world to selenium


Here I hope to show how to record a test case and replay it.


First you have to install selenium IDE to you browser.


My browser is Firefox so no need of installing Selenium RC :)
Go to the above link and select proper version
Then browser will ask for installing selenium to the browser. (You know what to do :) )




Here we go...
  • Go to browser Tools -> Selenium Ide.
  • Then select record.




That's it.
  • Do some googling. Go to some links...
  • Then go to Selenium IDE and stop recording.
  • Select Play entire test suite..
You will see that all the bowsing stuff you have done is replaying :)


So it is simple as that  but you can extend it further. You can save that test case and re use it later or you can export it into many compatible language test cases.


Here is another trick ....


While you are browsing you can make sure that you are going on the correct path by right clicking on a particular waypoint and select "Verify text present...."


While you are replaying the test cases, it makes sure that particular way point is in the proper place.....


So that's all about selenium IDE.... Then let's look at the language support supplied by selenium for test cases.
-------------------------------------------------------------------------------------------------------


       Another use of selenium is providing web drivers. Web driver is a tool which we can use to test web applications. In Selenium it supports several web drivers.


Ex:- Fire fox web driver
Internet Explorer web driver
Google chrome web driver


       The importance of this selenium web driver is it hasn’t tied with any other test framework. Due to this reason these web drivers can be easily used with any test framework and without any test frame work.
Another important aspect of these web drivers is the ability of handling web elements. And we can easily automate our processes by using the selenium IDE.




Testing using Selenium Server
In Selenium IDE testing can only use in Firefox browser. Now let’s try more general approach to automated testing using Selenium Server.
First you have to download selenium Server from http://selenium.googlecode.com/files/selenium-server-standalone-2.19.0.jar  and save in your computer.
I’m using IntelliJ Idea to do my testing. You can use whatever you are familiar with, Eclipse, Netbeans, etc.
First create a new project named “SeleniumPro” in Idea. After project is created right click on “SeleniumPro” select “Open module settings”. Then go to dependencies on it and add selenium-server-standalone-2.19.0.jar and junit libries.



Now we can start writing our code. Here we are going to write an code so we can open new browser instant. First we have to make an instant of web driver. We use org.openqa.selenium.WebDriver class for this.
WebDriver driver=new FirefoxDriver();
This is how we make an WebDriver instant to open Firefox browser. We can also use this to open other browers.
  WebDriver driver=new InternetExplorer();
WebDriver driver=new ChromeDriver();
Now run your code. It will open a Browser as we expected.
Now let’s see how to close an opened browser.
                 driver.quit();
Using this method we can close the browser we opened.
Our next task is to visit a website.
                driver.get(www.google.lk);
Put this line in tour code. Now our little program will open a browser, go to google and close. This is a very simple web automation that can be done using Selenium. 


Now let’s try to add some new features to our code. Let’s try to login to Facebook. So I have to type my email and password then click login.
To type first we have to find the textbox to type our email. We can make an instant of it as a web element. Now let’s see how we can make a web element corresponds to that textbox.
               WebElement login = driver.findElement(By.name("email"));
Here “email” is the identifier(name) used to name that textbox. You can find the name of any web element by right clicking on the web page and selecting “Inspect Element

Then click on “Inspect” In the left bottom corner, select the element which you need to find the name, then click “HTML”. Then a HTML code will come where we can find the name of the element.
Now we have to type our email address on it. We can do it by using this line.
                login.sendKeys(“email address”);
We can also type password in the same manner.
                WebElement passwd = driver.findElement(By.name("pass"));
                Passwd.sendKeys(“password”);
Now we have to click the log in button. We have to create a WebElement for that button and call clickmethod of it.
                WebElement loginbutton = driver.findElement(By.id("loginbutton"));
                loginbutton.click();
Now run and check whether it is working properly. We can do more web automations using Selenium.

Drawbacks of Selenium
Even though Selenium is an open source tool its documentation is very difficult for users to use. Also upgrades are happening very slowly to it.
Selenium IDE is only working with Firefox, to work with other browsers we have to use Selenium RC (Remote Controller) or Selenium Server.







No comments:

Post a Comment