Wednesday, 8 February 2017

SELENIUM: Factory Annotation in TestNG

SELENIUM: Factory Annotation in TestNG:



1.@Factory allows tests to be created at runtime depending on certain data-sets or conditions.
2.Sometimes we may need to run a set of tests with different data values. To achieve this we may define a separate set of tests inside a suite in the testng XML and test the required scenario.
3.The problem with this approach is that, if you get an extra set of data, you will need to redefine the test. TestNG solves this problem by providing the  @Factory annotation feature. Factory in TestNG defines and creates tests dynamically at runtime.

4.A factory method is defined by declaring @Factory above the respective test method. It’s mandatory that a factory method should return an array of Object class (Object []).

5.One of the main advantage of using the factory methods is that you can pass parameters to test classes while initializing them. These parameters can then be used across all the test methods present in the said classes.



package com.rameshsoft.rameshselenium;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

          public  class FactoryDemo {
                    
                    @Factory
                    public Object[] getValues()
                    {
                             Object[] obj=new Object[3];
                              
                              obj[0]=new FactoryAnnotation("rameshsoft.selenium@gmail.com", "*****");
                              obj[1]=new FactoryAnnotation("rameshatbtech@gmail.com","******");

                              obj[2]=new FactoryAnnotation("rameshatbtech @gmail.com","******");
                              
                              return obj;


/*
 return new Object[]{new FactoryAnnotation("rameshsoft.selenium@gmail.com", "*****"),
                    new FactoryAnnotation("rameshatbtech@gmail.com ", "*****"),
          new FactoryAnnotation("rameshatbtech @gmail.com ","******")
};

*/

                             }
                  
                   }
///////////////////////////////////////////////////////////////////////////////////////////////////

package com.rameshsoft.rameshselenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class FactoryAnnotation {
           String uname;
           String pwd;
                  
                   public FactoryAnnotation(String username,String password)
                   {
                            
                              this.uname=username;
                              this.pwd=password;
                             System.out.println(uname);
                             System.out.println(pwd);
                   }
                   @BeforeClass
                   public void beforeClass()
                   {
                             System.out.println("Before class executing");
                   }
                  
                   @BeforeSuite
                   public void beforeSuite()
                   {
                             System.out.println("Before Suite executing");
                   }
                   @BeforeTest
                   public void beforeTest()
                   {
                             System.out.println("Before Test executing");
                   }
                  
                   @BeforeMethod
                   public void beforeMethod()
                   {
                             System.out.println("Before Method executing");
                   }
                   @Test
                   public void execute() throws InterruptedException
                   {
                             System.out.println("user name is: "+uname);

                             System.out.println("password is : "+pwd);

                             System.setProperty("webdriver.gecko.driver", "C:\\Users\\ramesh\\Desktop\\java\\desk\\geckodriver-v0.11.1-win64\\geckodriver.exe");

          WebDriver driver=new FirefoxDriver();

          driver.manage().window().maximize();

          driver.manage().timeouts().implicitlyWait(50,TimeUnit.SECONDS);

          driver.get("https://www.gmail.com");

          driver.findElement(By.id("Email")).sendKeys(uname);

          driver.findElement(By.id("next")).click();

          Thread.sleep(2000);

          driver.findElement(By.id("Passwd")).sendKeys(pwd);

          driver.findElement(By.id("signIn")).click();

          driver.quit();
          }

          }


Note:Run  FactoryDemo.java file


Output:
rameshsoft.selenium@gmail.com
*****
rameshatbtech@gmail.com
******
rameshatbtech @gmail.com
******

Before Suite executing
Before Test executing
Before class executing
Before Method executing
user name is: rameshsoft.selenium@gmail.com
password is : *****


Before class executing
Before Method executing
user name is: rameshatbtech @gmail.com
password is : ******


Before class executing
Before Method executing
user name is: rameshatbtech@gmail.com
password is : ******

No comments:

Post a Comment