About Selenium
Selenium is a software testing framework for the web that facilitates the automation of browsers. The Selenium project produces various tools for automation testing such as Selenium IDE, Selenium Remote Control (RC), Selenium Grid and Selenium 2.0 & WebDriver. Learning all the tools will give you many different options for approaching different automation problems. The entire suits of tools result in a rich set of testing functions specially geared to the needs of testing of web application of all types.Why Selenium
- - Selenium is an open source tool with Corporate backing.
- - The tests can then be run against most modern web browsers.
- - Selenium deploys on Windows, Linux, and Macintosh platforms.
- - It allows recording, editing, and debugging tests.
- - Recorded tests can be exported in most language e.g. html, Java, .net, perl, ruby etc.
- - Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser.
Selenium Components
- - Selenium 1/ Selenium RC or Remote Control: Selenium RC was the main Selenium project for a long time before the Selenium Webdriver merge brought up Selenium 2, the newest and more powerful tool.
- - Selenium IDE (Integrated Development Environment): The Selenium IDE is a simple but powerful Firefox extension that lets users record and replay sets of browser interactions as test cases.
- - Selenium Grid: Selenium Grid is a server that allows tests to use web browser instances running on remote machines. It allows the Selenium RC solution to scale for large test suites and for test suits that must be run in multiple environments. Different tests can be run at the same time on different remote machines.
- - Selenium Web Driver : It also provides a test domain-specific language (Selenese/Webdriver) to write tests in a number of popular programming languages, including Java, C#, Groovy, Perl, PHP, Python and Ruby. It is the newest addition to the Selenium toolkit. This provides all sort of awesome features, including a more cohesive and object oriented API as well as an answer to the limitation of the old implementation.
Selenium WebDriver
The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to provide a simpler, more concise programming interface in addition to addressing some limitations in the Selenium-RC API. It enables you to use a programming language to write test scripts in different programming languages like html, Java, .net , perl, ruby and which enables you to use conditional operations, looping and other programming concepts which makes you test script robust. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.In the following post I will go step by step and explain how to write your first test case using Selenium Webdriver and execute it using TestNG.
1. Right click on the src folder ->New -> Package.
2. Provide package name something like com.stm.test and click “Finish”.
3. Right click on the newly created package – > New -> Class.
4. Provide class name as “RegistrationTest” and click Finish.
5. Write the code given below for your first test.
I am taking an example of automating register user functionality for demo site
The code for the first test is as follows:
package com.stm.test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;
public class RegistrationTest
{
@Test //This is TestNG annotation
public void testRegister()
{
WebDriver driver = new FirefoxDriver();
driver.get(“http://newtours.demoaut.com/”);
driver.findElement(By.linkText(“REGISTER”)).click();
driver.findElement(By.name(“firstName”)).sendKeys(“User1″);
driver.findElement(By.name(“lastName”)).sendKeys(“Surname1″);
driver.findElement(By.name(“phone”)).sendKeys(“123456789″);
driver.findElement(By.name(“userName”)).sendKeys(“user1@test.com”);
driver.findElement(By.name(“address1″)).sendKeys(“Test Address”);
driver.findElement(By.name(“city”)).sendKeys(“Test City”);
Select select = new Select(driver.findElement(By.name(“country”)));
select.selectByVisibleText(“ANGOLA”);
driver.findElement(By.name(“email”)).sendKeys(“user1@test.com”);
driver.findElement(By.name(“password”)).sendKeys(“user1″);
driver.findElement(By.name(“confirmPassword”)).sendKeys(“user1″);
driver.findElement(By.name(“register”)).click();
driver.close();
driver.quit();
}
}
7. After executing the test select the project and press F5 to refresh the project.
A new folder “test-results” will get created which will show you the results for the execution. Right click on index.html->open with->web browser to see the execution report.