Selenium Introduction

What is Selenium?

Just like any other automation tools to automate some task or list of tasks, Selenium automates the web-browser for you. In very simple words: “it tries to act as a real user and interacts with web pages(or web-applications)”.

Few Scenarios

For Testing

Imagine a situation where a there has been few upgrades(changes) to a web application and testers want to verify that the upgrade didn’t break any wxisting feature. One way is do this is simply sit back in front of the application and open the application, navigate through the whole app and test every functionality. This seems to be easy enough but what if the upgrades are more frequent; the tester has to repeat the same old steps again and again. For a normal user it might take several hours to do this as the human actions are bound to their speed. Here the Selenium comes into picture; you can write a selenium script to simulate the whole procedure, which will surely take less time and human effort. So next time if there are any changes the tester can simply run the scripts and get the results.

For Automation

Lets say there is a program in execution which takes a lot of time(like several minutes or even hours) and the user wants to update the results in their enterprise web-application. Surely it would take the user to wait for the taks to complete, get results and login to the web application and update the results, which is a time consuming effort. It would have been better if the user was able to automate the process after the job is finished. Again, the same can be done by writing some selenium scripts.

Selenium scripts generally consist of several browser/webpage interactions, typing into an input box, clicking on a button, opening a dropdown menu for instance. All these interaction can be written in various language like Java, C#, python and many more. We will be using the Java programming language throught this course. Now to perform these interactions Selenium needs another component called a driver(generally an executable), which can execute direct browser commands to simulate the interactions. This driver component is different for each browser and platform architecture.

An Example

Lets say we want to enter some text into an input field and the input field has an id “demo_input_field” in the web-page:


WebDriver driver = new FirefoxDriver(); //create a driver object(this one for firefox)
driver.get("http://www.example.com"); //open the web page
driver.findElement(By.id("input_field_id")).sendKeys("Dummy Text"); //enter the text in the element selected by its id

Explaination:

  • In first line we simply create an object(driver) that we can use to perform tasks
  • Next we go to the page which we want to interact with
  • Finally selected the element with the By.id() method using the elements “id” and enter the text “Dummy Text” in it with the help of sendKeys() method

We can break the third line of the example into more precise components. Here is how:



WebElement element = driver.findElement(By.id("input_field_id"));//Get the element as an object by the id of the element
element.sendKeys("Dummy Text"); //enter the text

From the code above one can notice that since we have now got the reference to the element, we can use it later on too.

Another Example

Lets say we want to enter some text into an input field and the input field has an id “demo_input_field” in the web-page:


WebDriver driver = new FirefoxDriver(); //create a driver object
driver.get("http://www.example.com"); //open the web page
webElement element = driver.getElement(By.linkText("Click Me"));//select the link by its text
element.click(); //perform click

That’s it. The examples and description is enough to start with. Though, few might want to take a look for earlier version of Selenium

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s