Sunday, 7 February 2016

Optional annotation in TestNG Frameork:

Optional annotation in TestNG Frameork:
=================================
we can pass parameter values to the test methods during run time from testng xml file by specifying Parameters annotation to test method.

To do this, we need to declare parameters tag in xml file using 'name' and 'value' attribute.Where the name attribute of the tag defines name of the parameter and the value attribute defines the value of the parameter.

If defined parameter is not found in your testng.xml file, The test method will receive the default value which is specified inside the @Optional annotation.

Syntax to define @Optional annotation:
=======================================
@Parameters("browser")
@Test
public void openBrowser(@Optional("firefox") String value) 

... 

}

CrossBrowserTestingDemo:
==========================
package testngdemo;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossBrowserTestingDemo {

WebDriver webDriver;

@BeforeTest
 
@Parameters("browser")
 
    public void setup(@Optional("firefox")String browser) throws Exception{
        if(browser.equalsIgnoreCase("firefox")){
 
        webDriver = new FirefoxDriver();
            webDriver.manage().window().maximize();
    webDriver.manage().deleteAllCookies();
    webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
 
        }
 
 
        else if(browser.equalsIgnoreCase("chrome")){
 
            System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
            webDriver = new ChromeDriver();
            webDriver.manage().window().maximize();
    webDriver.manage().deleteAllCookies();
    webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
 
        }
 
else if(browser.equalsIgnoreCase("ie")){

            System.setProperty("webdriver.ie.driver","C:\\IEdriver.exe");
            webDriver = new InternetExplorerDriver();
            webDriver.manage().window().maximize();
    webDriver.manage().deleteAllCookies();
    webDriver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
 
        }
 
        else{
            throw new Exception("Browser is not correct");
 
        }
    }
 
    @Test
 
    public void testParameter() throws InterruptedException{
 
    webDriver.get("https://google.com");
webDriver.findElement(By.name("q")).sendKeys("selenium by ramesh");
webDriver.findElement(By.name("btnG")).click();
Thread.sleep(3000);
webDriver.findElement(By.linkText("Selenium By Ramesh Anapati")).click();
 
    }
}


testng.xml;
===========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">

    <classes>
      <class name="testngdemo.CrossBrowserTestingDemo"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

No comments:

Post a Comment