Introduction to Selenium and some Examples

|
| By Webner

Introduction to Selenium and some Examples

History of Selenium

  1. Selenium was launched in 2004 by Jason Huggins (engineer at ThoughtWorks in Chicago) and he named it “Javascript TestRunner” but at that time it was stuck with the “same origin policy” issue. Paul Hammant (engineer at ThoughtWorks in Chicago)  solved this problem and made it open source at the end of the year.
  1. In 2006 the enhanced Selenium RC took over the Selenium core and released Selenium 1. In same year Shinya Kasatani created Firefox extension that records and replays browser action. Kasatani donated it to the Selenium project and named it as Selenium IDE.
  1. In 2007 Simon Stewart (engineer at ThoughtWorks in Chicago) was working on another web testing tool named WebDriver. Selenium RC and Webdriver merged and released Selenium 2.

Components of Selenium:

  1. Selenium IDE (integrated development environment): It is a Mozilla Firefox plugin that works only in the Firefox browser. It supports record/playback mechanism similar to some other commercial tools like QTP, Winrunner etc. It gives the options to edit, record and debugs test cases. Selenium IDE introduced the concept of the user interface and it is having its own language called selenese (set of commands). It is easy to use and test cases written in IDE can be exported to many programming languages like Ruby, Java, C#, etc.
  2. Selenium RC (Remote Control): It is also known as the Selenium 1. It was the first tool of the Selenium suit that made the Selenium famous in the market. Selenium RC supports multiple programming languages (JAVA, Ruby, Perl, PHP, Python, and C#) and all browsers such as Mozilla Firefox, Google Chrome, Internet Explorer ,etc. Earlier it was known as the Javascriptexecutor. We can use this for all browser that support javascript.

Selenium RC was having a drawback that it had to communicate with the server each time to run a test. Server used to work as the intermediate between the code and browser.

1. Selenium Webdriver – It is also known as Selenium 2. It has direct communication with the browser hence overcomes Selenium 1’s limitations. It has parallel testing capabilities.

2. Selenium Grid – It is the last component of Selenium. It supports parallel testing used to run a test script on different machines at the same time. it follows Hub and nodes system – one machine is created as the hub that controls the communication and node is where actual execution happens. it supports all of the major operating systems and main browsers to run the script.

Some examples:

1. How to get Title of current page and compare it:

In this example we will follow these steps:

  1. Open the browser
  2. Enter Url of website
  3. Get the Title of page
  4. Compare Title with the Expected Title:
package Selenium;
import org.openqa.Selenium.WebDriver;
import org.openqa.Selenium.chrome.ChromeDriver;
public class Seleniumexample
{
public WebDriver driver;
public void testCase()
{
//launch the chrome browser
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
driver = new ChromeDriver();
//open the url in the browser
driver.get("https://webnersolutions.com/");
//expected Title
String expectedTitle ="Webner | Insurance, eLearning and Salesforce Software Development";
// get the actual title of the page
String actualTitle = driver.getTitle();
System.out.println("Actual Title Of Page is" + " : " + actualTitle);
//compare the expected title with actual title
if (actualTitle.equals(expectedTitle))
{
System.out.println("TEST CASE PASSED");
}
else
{
System.out.println("TEST CASE FAILED");
}
//close the browser
driver.close();
}
public static void main(String[] args) 
{
Seleniumexample call = new Seleniumexample();
call.testCase();
}
}

Screenshot of Result:

2. How to test the Yahoo register page:

In this example we will follow these steps:

  1. Open the browser
  2. Enter the Url of Yahoo
  3. Fill the registration page
  4. Click on continue button:
package selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.Select;
public class registration 
{
public WebDriver driver;
public void browser() throws InterruptedException
{
//launch the Chrome browser
System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe");
driver = new ChromeDriver();
}
public void regform() throws InterruptedException
{
//open the url in the browser
driver.get("https://login.yahoo.com/account/create?specId=yidReg");
//maximize the Window Size
driver.manage().window().maximize();
//enter the firstname
driver.findElement(By.id("usernamereg-firstName")).sendKeys("Manpreet");
//enter the lastname
driver.findElement(By.id("usernamereg-lastName")).sendKeys("Singh");
//enter the email address
driver.findElement(By.id("usernamereg-yid")).sendKeys("manpreetwebners123");
//enter the password
driver.findElement(By.id("usernamereg-password")).sendKeys("manpreet@123");
//enter the phone number
driver.findElement(By.id("usernamereg-phone")).sendKeys("7307296138");
//enter the month in DOB field
WebElement month = driver.findElement(By.id("usernamereg-month"));
Select mnth = new Select(month);
mnth.selectByIndex(2);
//enter the date and year in DOB field
driver.findElement(By.id("usernamereg-day")).sendKeys("09");
driver.findElement(By.id("usernamereg-year")).sendKeys("1993 ");
//enter the gender
WebElement Gender = driver.findElement(By.xpath(".//*[@id='usernamereg-freeformGender']"));
Gender.sendKeys("Male");
//click on the continue button
driver.findElement(By.id("reg-submit-button")).click();
}
public static void main(String arg[]) throws InterruptedException
{
registration cal = new registration();
cal.browser();
cal.regform();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *