Earn Money in 10 days

Selenium Videos

Protractor In Selenium

Livechat

Thursday, 11 December 2014

Page Factory(Pom)

Page Factory in Selenium 

In order to provide additional support for the Page Object pattern, the package org.openqa.selenium’ has a PageFactory Class. One can import the PageFactory class in Java using the following statement
import org.openqa.selenium.support.PageFactory;
By importing this package we can use multiple pages just we have to make an object of that class.
Let me take an example
Here If my page name is Login then i will call as 
Login l = PageFactory.initElements(driver, Login.class);
Generally we make the object of that class and use but in page factory we use init method.
You can also  this Page Factory as Reference.
In page factory we define Webelements like this.
Annotations in PageFactory are like this:



We use annotation@FindBy 
If we use locator as xpath then we will write 
@FindBy(how = How.XPATH, using = "//*[@value='First name']")
WebElement firstname;
how = How.XPATH, [We can define locators  as How.ID,How.name,How.css etc]
In using we have to define which locator we are using id,xpath name,css etc.
using = //*[@value='First name']
If we use locator By name Then we will write 
@FindBy(how = How.NAME, using = "firstname")
WebElement firstname;

We don't used driver.findelements();

We simply call these as:
 firstname.sendKeys("Ritika");

How to implement Page factory
  •  Create a ‘New Package file and name it as ‘pageobject’ by right click on the Project and select New > Package.


  •  Create a ‘New Class’ file and refer the name to the actual page from the test object, by right click on the above created Package and select New > Class. In our case it as FacebookRegistration
//Here is code of page factory 
package pageobject;


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;

public class FacebookRegistration {
WebDriver driver;
public FacebookRegistration(WebDriver ldriver){
this.driver= ldriver;
}
 
@FindBy(how = How.XPATH, using = "//*[@value='First name']")
WebElement firstname;
@FindBy(how = How.XPATH, using = "//*[@value='Last name']")
WebElement lastname;
@FindBy(how = How.XPATH, using = "//*[@value='Email or mobile number']")
WebElement email;
@FindBy(how = How.XPATH, using = "//*[@value='Re-enter email or mobile number']")
WebElement reemail;
@FindBy(how = How.XPATH, using = "//*[@value='New password']")
WebElement NewPassword;
@FindBy(how = How.XPATH, using = "//*[@id='month']")
WebElement month;
@FindBy(how = How.XPATH, using = "//*[@id='day']")
WebElement day;
@FindBy(how = How.XPATH, using = "//*[@id='year']")
WebElement year;
@FindBy(how = How.XPATH, using = "//*[@id='u_0_d']")
WebElement female;
@FindBy(how = How.XPATH, using = "//button[@name='websubmit']")
WebElement signup;
 
// Type firstname
public void typefirstname(String fname) {
firstname.sendKeys(fname);
}
// Type lastname
public void typelastname(String lstname) {
lastname.sendKeys(lstname);
}
// Type email
public void typeemail(String emailid) {
email.sendKeys(emailid);
}//Typereemail
public void typeremail(String remailid) {
reemail.sendKeys(remailid);
}
//typenewpassword
public void typenewPassword(String newpassrd) {
NewPassword.sendKeys(newpassrd);
}
//selectmonth
public void selectmonth(String mont){
month.sendKeys(mont);
}
//Select date 
public void selectdate(String date){
day.sendKeys(date);
}
//selecting year
public void selectyear(String yea){
year.sendKeys(yea);
}
//clicking on femaleradiobutton  
public void ClickingGender(){
female.click();
}
//clicking sign up button  
public void clicksignup(){
signup.click();
}
 
}
/In this We have created a Registration page of Facebook Now we need to call this page in our Testcase.Generally we create pages in page object factory and after that we call that pages in Testcase.
  •  Create a ‘New Package file and name it as ‘pageobjectframework’ by right click on the Project and select New > Package.
  •  Create a ‘New Class‘ and name it as TestCase by right click on the ‘pageobjectFramework Package and select New > Class.
package pageobjectframework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import pageobject.FacebookRegistration;


public class TestCase {
WebDriver driver;
@BeforeTest 
    public void openBrowser(){
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.facebook.com");
    }
//This test will fill the sign up page of facebook login
@Test
public void registerDetails(){
//By this init method we can call our pages in Page factory.
FacebookRegistration fo = PageFactory.initElements(driver, FacebookRegistration.class);
fo.typefirstname("Ritika");
fo.typelastname("Gulati");
fo.typenewPassword("seleniumgyan");
fo.typeemail("submityouremailid@gmail.com");
fo.typeremail("submityouremailid@gmail.com");
fo.selectmonth("Jul");
fo.selectdate("22");
fo.selectyear("1991");
fo.ClickingGender();
fo.clicksignup();
}}

Tuesday, 9 December 2014

What is POM(pageobjectmodel)

Page object model

  • Page Object is a Design Pattern   which has become popular in test automation for enhancing test maintenance and reducing code duplication.
  •  Page object is an object-oriented class that serves as an interface to a page of your Application under test. The tests then use the methods/WebElements of this page object class whenever they need to interact with that page of the UI. The benefit is that if any UI element  changes of that the page we don't need to change in our Test just we need to change only the code within the page object.
  • Page Objects Model is best suited for applications which have multiple pages like similar registration form/or menu bar or states. Each of which have fields which can be uniquely referenced with respect to the page.

Pom Advantages:

  1. Object Repository: We  can create an Object Repository of the fields segmented page-wise. This as a result provides a Page Repository of the application as well. 
  2. Functional Encapsulation: All possible functionality or operations that can be performed on a page can be defined and contained within the same class created for each page.
  3. Low Redundancy:Helps reduce duplication of code. If the architecture is correctly and sufficiently defined, the POM gets more done in less code.
  4. Efficient & Scalable: Faster than other keyword-driven/data-driven approaches where Excel sheets are to be read/written.

How to implement Page object Model

  •  Create a ‘New Package file and name it as ‘pageobjectpages’ by right click on the Project and select New > Package.

  • 2. Create a ‘New Class’ file and refer the name to the actual page from the test object, by right click on the above created Package and select New > Class. In our case it as FacebookRegistrationpage
  • Now write code for Registration page.
  •  Make Webdriver driver as null
  • You need to define all elements according to ui whether it is radiobutton/textboxes.Just you need to define the all webelemets on which you want to perform action.
  •  Create a Constuctor of Your class and Pass WebDriver as Parameter 
  • You need to write methods/Function for all WebElements whether you want to click/write/Select from dropdown.

package pageobjectpages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;

public class FacebookRegistrationpage {
//Instancing webdriver
public  WebDriver driver;
//Defining webelements 
By firstname = By.xpath("//*[@value='First name']");
By lastname =  By.xpath("//*[@value='Last name']");
By email = By.xpath("//*[@value='Email or mobile number']");
    By reemail =  By.xpath("//*[@value='Re-enter email or mobile number']");
    By newPassword = By.xpath("//*[@value='New password']");
    By month = By.xpath("//*[@id='month']");
    By day = By.xpath("//*[@id='day']");
    By year = By.xpath("//*[@id='year']");
    By femaleradiobutton = By.xpath("//*[@id='u_0_d']");
    By signup = By.xpath("//button[@name='websubmit']");

 // Instantiate class
  public FacebookRegistrationpage(WebDriver driver) {
  this.driver = driver;
  }
    
 // Type firstname
  public void typefirstname(String uname) {
  //Create a method which will enter firstname
  driver.findElement(firstname).sendKeys(uname);
  }
 // Type lastname
  public void typelastname(String lstname) {
  //Create a method which will enter lastname
  driver.findElement(lastname).sendKeys(lstname);
  }
  // Type email
  public void typeemail(String emailid) {
  //Create a method which will enter emailid
  driver.findElement(email).sendKeys(emailid);
  }//Typereemail
  public void typeremail(String remailid) {
  //Create a method which should type email
  driver.findElement(reemail).sendKeys(remailid);
  }
  //typenewpassword
  public void typenewPassword(String newpassrd) {
  //Create method will enter password
  driver.findElement(newPassword).sendKeys(newpassrd);
  }
  //selectmonth
  public void selectmonth(String mont){
  Select mon = new Select(driver.findElement(month));
  mon.selectByVisibleText(mont);
  }
  //Select date 
  public void selectdate(String date){
Select datebirth = new Select(driver.findElement(day));
datebirth .selectByVisibleText(date);
}
  //selecting year
  public void selectyear(String yea){
Select yearsel = new Select(driver.findElement(year));
yearsel .selectByVisibleText(yea);
}
  //clicking on femaleradiobutton  
  public void ClickingGender(){
  driver.findElement(femaleradiobutton).click();
  }
 
//clicking sign up button  
  public void clicksignup(){
  driver.findElement(signup).click();
  }
}
//In this We have created a Registration page of Facebook Now we need to call this page in our Testcase.Generally we create pages in page object model and after that we call that pages in Testcase.
  •  Create a ‘New Package file and name it as ‘pageobjectframework’ by right click on the Project and select New > Package.
  •  Create a ‘New Class‘ and name it as TestCase by right click on the ‘pageobjectFramework Package and select New > Class.

package pageobjectframework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import pageobjectpages.FacebookRegistrationpage;

public class TestCase {
WebDriver driver;
FacebookRegistrationpage page;
@BeforeTest 
    public void openBrowser(){
        driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://www.facebook.com");
    }
//This test will fill the sign up page of facebook login
@Test
public void registerDetails(){
page = new FacebookRegistrationpage(driver);
page.typefirstname("ritika");
page.typelastname("Gulati");
page.typeemail("submityourmailid@gmail.com");
page.typeremail("submityourmailid@gmail.com");
page.typenewPassword("seleniumgyan");
page.selectdate("22");
page.selectyear("1991");
page.selectmonth("Jan");
page.ClickingGender();
page.clicksignup();
}
}


In Pageobject framework just we create pages and call these pages into our Testcases.In these pages we just write all the Webelements and functions. This is only registration page but it Depends on application How many pages your application contains.If your application have 20 ui pages Then you can call all 20 pages in One Testcase.

What are Listners in Testng

@Listner in Testng 

This is one very good feature in Testng. if you choose to have some default behavior for your test case when it fails, passes etc. For Eg : if you extend TestListenerAdapter class and write your implementation in a custom listner to override onTestFailure or onTestSuccess methods you will be able to do  anything which you want when a test fails or passes, like taking screenshots, writing something to database, sending emails(Report generation) etc. 

Lets say your test fails and you want to rerun the test case automatically one more time when it fails. In that case you will be writing a custom listener to achieve what you need and plug the listener to your test case via @Listener annotation. 

Lets say your test fails and you want to take snapshot of fail  test case automatically In that case you will be writing a custom listener to achieve what you need and plug the listener to your test case via @Listener annotation. 


//Above is the description but now i will explain what exactly they do.

In Testng TestListenerAdapter is a class You can override  any of the methods in which are given in TestListemerAdaptor class.
Suppose your test case is passed and you want to take snapshot at that case just you need to override a method onTestSuccess(ITestResult tr)  and just you need to write your snapshot code here.By this whenever your test case is passed this function will execute and you can see snapshot.

Suppose your test case is failed and you want to generate reports at that case just you need to override a method onTestFailure(ITestResult tr) and just you need to write (How to genrate reports)code here.By this whenever your test case is failed this function will execute and you can see snapshot.


Same activity will be done by all of the functions according to their function name



How to implement TestNG Listener



  • Create a simple class named as CustomListner and extends TestListenerAdapter



After extending this class you can override some  methods.Here i am using three methods one pass,skipped and fail.
//Here is the code 

package listner;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class Customlistner extends TestListenerAdapter{
//Override some methods 
public void onTestFailure(ITestResult tr) 
{
System.out.println("When testcase is failed generate reports ");
}
public void onTestSuccess(ITestResult tr)  {
System.out.println("When testcase is pass Take snapshot");
}
public void onTestSkipped(ITestResult tr) {
System.out.println("Testcase is skipped do some activity");
}

}

//After using some of these methods just you need to Create a simple class named as LoginTest

 Make a new class named as LoginTest in which your testcases are running and  write the @Testng annotations and create two Testcases in this class

package listner;

import org.testng.Assert;
import org.testng.annotations.Test;

public class LoginTest {
@Test
public void loginwithvalidusername(){
System.out.println("Login with valid username");
}
@Test
public void loginwithinvalidusername(){
System.out.println("Login with invalid username");
//Use assertion and delibrately failed this
Assert.assertEquals("Equal", "NotEqual");
}

}
// Run this class run this class with help of xml.
//Copy this xml into your project and named as Testng.xml
//This is the xml file for this

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="My Sample Suite">
  <listeners>
    <listener class-name="listner.Customlistner" />
  </listeners>
<test name="LoginTest">
    <classes>
        <class name="listner.LoginTest" ></class>  
    </classes>
</test>
</suite>
Now run this class>Go to testng.xml

Whenever Testcase is failed then failedfunction called By listner or Whenever testcase passed this passed function will called. 
  

Friday, 5 December 2014

How to upload file in Selenium

               Upload file in Selenium

  1. Upload file using Autoit  Click Here
  2. Upload File using Robot class Click here

Wednesday, 3 December 2014

TestNG Video Tutorials For Beginners

        Testng Tutorials  for Beginners from Basic to Advance    


  1. TestNG DEMO  Click here
  2. Testng part 1 for Beginners Click here
  3. Priority in TestNG Click here
  4. Dependencies in Testng Click Here
  5. Xslt Reports by Testng Click here
  6. Xslt Reports Part 2 Click here

Monday, 1 December 2014

How to click on Button in Selenium Webdriver

 The click() method is used to simulate the clicking of any element.              


In Selenium we have click () method of Webdriver Interface for clicking on Signup/Login/Sumit Button/Radio Button/Checkboxes

It does not take any parameter/argument.

//Here is the code
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FacebookLogin {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://facebook.com");
//Enter the text into emailaddress
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("ritu7180@gmail.com");
//Enter the text into password Filled
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password");
//Clicking on signInButton
driver.findElement(By.xpath("//*[@id='signIn']")).click();
}
}

How to enter text into Textbox in Selenium Webdriver

Enter text intoTextbox in WebDriver

In Selenium we have sendkeys() method of Webdriver Interface for Typing in textbox of username and password.


sendKeys()- This method will accept string as an argument whatever text you will specify it will type into that textbox.
//Here is the code for textbox
package test;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Facebook {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://facebook.com");
//Enter the text into emailaddress
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("ritu7180@gmail.com");
//Enter the text into password Filled
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password");
        
}

}

Popular Posts

 
subscribe
Subscribe Us
emailSubscribe to our mailing list to get the updates to your email inbox... We can't wait more to have your email in our subscribers email list. Just put your nice email in below box: