Simply Selenium!!

Day 1 : Selenium is free-open source automation tool to test web-applications.

This is just one-liner definition. Being curious is my hobby. Before I even started learning and understanding the definition I started searching “Why was this testing tool named Selenium by Jason Huggins(Creator)”?

Selenium is the name of a mineral. Selenium is an essential trace mineral that functions as an antioxidant and promotes a healthy immune system. Selenium has strong anti-cancer effects and is known to help detoxify the body and remove heavy metals including Mercury.

Does Selenium mineral kills anyone..!!! Yes it does.. It KILLS Mercury!! The creator of Selenium wanted to name it so as he wanted to nullify the licensed automation softwares like QTP and winrunner from MERCURY Interactive company which is now owned by HP.

Jason made it Open-source and for the matter of fun, he named the Test Automation Tool to be Selenium as it kills or nullifies the effect of Mercury.

Inspired by his thought I feel its important to have an imagination more than having knowledge. Share knowledge and give it to the world..Let the world learn and explore your ideas.. Its open source!! Their will always be great ideas behind simple words.. Be a learner.. be a creator :)

Now some practicals :

Step 1: Create Maven Project in Eclipse IDE.

POM : A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects.

Add dependencies in POM as below:

  • Click on POM XML
  • Click on Dependencies
  • Click on add. New window will pop up. Fill below details:
  • These are libraries for the Maven project.
  • Click Manage to add above jars in the project:

After build is completed find the jars under “Maven Dependencies”

All set to create Test files now :)

Day 2:

Task- Create Login_test.java file to test facebook login page as below:

Login_test.java

Program:

package com.facebook.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class Login_test {

public WebDriver driver;

@BeforeClass
 public void setUp()
 {//Setup Driver
 driver=new FirefoxDriver();
 }
 @AfterClass
 public void tearDown()
 {//Close Driver
 this.driver.quit();}

@Test
 public void loadPage()
 {//Load page
 driver.get(“https://www.facebook.com");
 //Assert.assertEquals(actual, expected);
 Assert.assertEquals(driver.getTitle(), “Facebook — Log In or Sign Up”);
 }
 @Test(dependsOnMethods=”loadPage”)
 public void filloutEmailField()
 {//Fill email field
 String email=”shalinibn185@gmail.com”;
 WebElement emailFld=driver.findElement(By.cssSelector(“#email”));
 emailFld.sendKeys(email);
 Assert.assertEquals(emailFld.getAttribute(“value”), email);

}

@Test(dependsOnMethods=”loadPage”)
 public void filloutPassField()
 {//Fill pass field
 String password=”123456";
 WebElement passwordFld=driver.findElement(By.cssSelector(“#pass”));
 passwordFld.sendKeys(password);
 Assert.assertEquals(passwordFld.getAttribute(“value”), password);

}

}

Run As TestNg Test:

Result:Console

[TestNG] Running:

C:\Users\Shalini.B.N\AppData\Local\Temp\testng-eclipse-1294519961\testng-customsuite.xml

PASSED: loadPage

PASSED: filloutEmailField

PASSED: filloutPassField

===============================================

Default test

Tests run: 3, Failures: 0, Skips: 0

===============================================

===============================================

Default suite

Total tests run: 3, Failures: 0, Skips: 0

===============================================

[TestNG] Time taken by org.testng.reporters.XMLReporter@6fadae5d: 39 ms

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@4411d970: 19 ms

[TestNG] Time taken by org.testng.reporters.EmailableReporter2@96532d6: 12 ms

[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 14 ms

[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@7f63425a: 95 ms

[TestNG] Time taken by org.testng.reporters.jq.Main@511baa65: 101 ms

Task: Create Testng.xml and Run

Execution of tests can be grouped in Testng.xml and executed based on priorities

Program:

package com.facebook.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class Login_test {

public WebDriver driver;

@BeforeClass(alwaysRun=true)
 public void setUp()
 {
 driver=new FirefoxDriver();
 }
 @AfterClass(alwaysRun=true)
 public void tearDown()
 {//Close Driver
 this.driver.quit();
 }

@Test(groups={“p1”,”pageLoads”})
 public void loadPage()
 {//Load page
 driver.get(“https://www.facebook.com");
 //Assert.assertEquals(actual, expected);
 Assert.assertEquals(driver.getTitle(), “Facebook — Log In or Sign Up”);
 }
 @Test(groups={“p2”,”field”},dependsOnMethods=”loadPage”)
 public void filloutEmailField()
 {//Fill email field
 String email=”shalinibn185@gmail.com”;
 WebElement emailFld=driver.findElement(By.cssSelector(“#email”));
 emailFld.sendKeys(email);
 Assert.assertEquals(emailFld.getAttribute(“value”), email);

}

@Test(groups={“p2”,”field”},dependsOnMethods=”filloutEmailField”)
 public void filloutPassField()
 {//Fill email field
 String password=”123456";
 WebElement passwordFld=driver.findElement(By.cssSelector(“#pass”));
 passwordFld.sendKeys(password);
 Assert.assertEquals(passwordFld.getAttribute(“value”), password);

}

}

testng.xml:

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd" >
 
<suite name=”Login Tests” verbose=”1" >
 <test name=”P1 Test” >
 <groups>
 <run>
 <include name=”p1"/>
 </run>
 </groups>
 <classes>
 <class name=”com.facebook.tests.Login_test” />
 </classes>
 </test>
 
 <test name=”P2 Test” >
 <groups>
 <run>
 <include name=”p2"/>
 </run>
 </groups>
 <classes>
 <class name=”com.facebook.tests.Login_test” />
 </classes>
 </test>
</suite>

Result:

Day 3: DataProviders

Isn’t is easier if we can run multiple instance from same code. Yes it is possible to run multiple instances for same piece of code. “dataproviders” can be used to store data and it can be fetched by same program any number of times for any number of instances.

Step1 : Create data file as below:

dataProvider code

Step 2:

Facebookdata.java

package com.data;

import org.testng.annotations.DataProvider;

public class Facebookdata {
 @DataProvider(name=”pages”)
 public static Object [][] pages(){
 return new Object[][]{
 {“https://www.facebook.com”,”Facebook — Log In or Sign Up”},
 {“https://www.google.com","Google"},
 {“https://www.yahoo.com","Yahoo"},
 };
 }}

Object[x][y]:

x- How many times tests are run

y-Arguments passed

Step 3: Changes in Login_test.java:

package com.facebook.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

import com.data.Facebookdata;

public class Login_test {

public WebDriver driver;

@BeforeClass(alwaysRun=true)
 public void setUp()
 {
 driver=new FirefoxDriver();
 }
 @AfterClass(alwaysRun=true)
 public void tearDown()
 {//Close Driver
 this.driver.quit();

}

@Test(groups={“p1”,”pageLoads”}, dataProvider=”pages”,dataProviderClass=Facebookdata.class)
 public void loadPage(String url,String title)
 {//Load page
 driver.get(url);
 //Assert.assertEquals(actual, expected);
 Assert.assertEquals(driver.getTitle(),title);
 }
 @Test(groups={“p2”,”field”},dependsOnMethods=”loadPage”)
 public void filloutEmailField()
 {//Fill email field
 String email=”shalinibn185@gmail.com”;
 WebElement emailFld=driver.findElement(By.cssSelector(“#email”));
 emailFld.sendKeys(email);
 Assert.assertEquals(emailFld.getAttribute(“value”), email);

}

@Test(groups={“p2”,”field”},dependsOnMethods=”filloutEmailField”)
 public void filloutPassField()
 {//Fill email field
 String password=”123456";
 WebElement passwordFld=driver.findElement(By.cssSelector(“#pass”));
 passwordFld.sendKeys(password);
 Assert.assertEquals(passwordFld.getAttribute(“value”), password);

}

}

Step 4: Run testng.xml

Day 4: Page Object classes

All actions and elements saved in page Object classes

Any changes can be updated only in page object class

Easy use method and easy to write scripts

Step 1: Create new java class: FacebookMainPage.java as shown below

Code: FacebookMainPage.java

package com.data;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

public class FacebookMainPage {

public static final String PAGE_URL=”https://www.facebook.com";
 public static final String PAGE_TITLE=”Facebook — Log In or Sign Up”;
 String field_EmailLogin=”#email”;
 String field_passwordLogin=”#pass”;
 WebDriver driver;

//Constructor 
 public FacebookMainPage(WebDriver driver){
 this.driver=driver;
 }

public void setText_EmailLogin(String text){
 WebElement element=driver.findElement(By.cssSelector(field_EmailLogin));
 element.sendKeys(text);
 Assert.assertEquals(element.getAttribute(“value”),text);
 }
 
 public void setText_passwordLogin(String text){
 WebElement element=driver.findElement(By.cssSelector(field_passwordLogin));
 element.sendKeys(text);
 Assert.assertEquals(element.getAttribute(“value”),text);
 }
}

Code:Login_test.java

package com.facebook.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

import com.data.FacebookMainPage;
import com.data.Facebookdata;

public class Login_test {

public WebDriver driver;
 FacebookMainPage fbMainPage;

@BeforeClass(alwaysRun=true)
 public void setUp()
 {
 this.driver=new FirefoxDriver();
 fbMainPage=new FacebookMainPage(driver);
 }
 @AfterClass(alwaysRun=true)
 public void tearDown()
 {//Close Driver
 this.driver.quit();
 }
 
 @Test(groups={“p1”,”pageLoads”})
 public void loadPage()
 {//Load page
 driver.get(fbMainPage.PAGE_URL);
 Assert.assertEquals(driver.getTitle(),fbMainPage.PAGE_TITLE);
 }
 
 @Test(groups={“p2”,”field”},dependsOnMethods=”loadPage”)
 public void filloutEmailField()
 {
 fbMainPage.setText_EmailLogin(“shalinibn185@gmail.com”);

}

@Test(groups={“p2”,”field”},dependsOnMethods=”filloutEmailField”)
 public void filloutPassField()
 {
 fbMainPage.setText_passwordLogin(“123456”);
 }

}

Testng.xml:

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd" >
 
<suite name=”Login Tests” verbose=”1" >
 <test name=”P1 Test” >
 <groups>
 <run>
 <include name=”p1"/>
 <include name=”p2"/>
 </run>
 </groups>
 <classes>
 <class name=”com.facebook.tests.Login_test” />
 </classes>
 </test>
 </suite>

Step 2: Run the testng.xml

Output: Facebook.com is loaded emailid and password is entered.

Day 5: PageFactory

It is another way to initialize page objects

Speed up tests and efficient

CODE:

FacebookMainPage.java

package com.data;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.testng.Assert;

public class FacebookMainPage {

public static final String PAGE_URL=”https://www.facebook.com";
 public static final String PAGE_TITLE=”Facebook — Log In or Sign Up”;

@FindBy(id=”email”) WebElement field_EmailLogin;
 @FindBy(id=”pass”) WebElement field_passwordLogin;

 WebDriver driver;

//Constructor 
 public FacebookMainPage(WebDriver driver){
 this.driver=driver;
 }

public void setText_EmailLogin(String text){
 field_EmailLogin.sendKeys(text);
 Assert.assertEquals(field_EmailLogin.getAttribute(“value”),text);
 }

public void setText_passwordLogin(String text){
 field_passwordLogin.sendKeys(text);
 Assert.assertEquals(field_passwordLogin.getAttribute(“value”),text);
 }
}

Login.test.java

package com.facebook.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.Assert;
import org.testng.annotations.*;

import com.data.FacebookMainPage;
import com.data.Facebookdata;

public class Login_test {

public WebDriver driver;
 FacebookMainPage fbMainPage;

@BeforeClass(alwaysRun=true)
 public void setUp()
 {
 this.driver=new FirefoxDriver();
fbMainPage=PageFactory.initElements(driver, FacebookMainPage.class);
 }
 @AfterClass(alwaysRun=true)
 public void tearDown()
 {//Close Driver
 this.driver.quit();
 }
 
 @Test(groups={“p1”,”pageLoads”})
 public void loadPage()
 {//Load page
 driver.get(fbMainPage.PAGE_URL);
 Assert.assertEquals(driver.getTitle(),fbMainPage.PAGE_TITLE);
 }
 
 @Test(groups={“p2”,”field”},dependsOnMethods=”loadPage”)
 public void filloutEmailField()
 {
 fbMainPage.setText_EmailLogin(“shalinibn185@gmail.com”);

}

@Test(groups={“p2”,”field”},dependsOnMethods=”filloutEmailField”)
 public void filloutPassField()
 {
 fbMainPage.setText_passwordLogin(“123456”);
 }

}

Output: Same as above