user7836878
user7836878

Reputation: 271

How to pass parameters to @BeforeClass & @AfterClass methods through testNG.xml file

I am trying to pass parameter to @BeforeClass method through testNG.xml file, but when I run my script, neither the script is executed nor any error is shown in the console, just the script is terminated. Not sure where am I going wrong. Kindly help me on this.

P.S: Script is working good when I use @BeforeSuite annotation instead of @BeforeClass

Thanks in advance

Below is my code snippet:

BaseSetup.java

public class BaseSetup {

private  WebDriver driver;
private String userdir = System.getProperty("user.dir");
private  void SelectBrowser(String browsername){

    switch(browsername){

    case "GoogleChrome": 

        //initChromeBrowser();

        break;

    case "FireFox":

        initFireFoxBrowser();

        break;

    default: 
        System.out.println("Wrong input");      
    }

}

private  void initChromeBrowser(){

    System.setProperty("webdriver.chrome.driver", userdir +"\\chromedriver.exe");

    driver = new ChromeDriver();

    launchApp();

}   
private void initFireFoxBrowser(){

    System.setProperty("webdriver.gecko.driver", userdir +"\\geckodriver.exe");

    driver = new FirefoxDriver();

    launchApp();

}

private  void launchApp(){

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

    driver.get("https://www.google.co.in");
}

@Parameters({"browsername"})

@BeforeClass

public  void LaunchBrowser(String browsername){

        SelectBrowser(browsername);     
}
}

Below is my testNG.xml code

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="SampleTestSuite">
<test name="SampleTest">

<classes>

  <class name="com.pac.base.BaseSetup">
  <parameter name="browsername" value="FireFox"> </parameter>     
   </class>

   </classes>

   </test> <!-- Test -->
   </suite> <!-- Suite -->

Upvotes: 1

Views: 9483

Answers (1)

Krishnan Mahadevan
Krishnan Mahadevan

Reputation: 14746

I dont think there's any issue here. TestNG is actually not executing the BeforeClassmethod because the class doesn't contain any @Test methods in it. BeforeSuite case is different because I guess in that case, its a suite level execution and there can be chances that some other class that is part of the execution might have a Test method in it. So TestNG executes it.

Here's a sample which demonstrates that parameters are passed properly

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class ParameterizedClass {
    @Parameters({"browsername"})
    @BeforeClass
    public void setup(String browsername) {
        System.err.println("Browser name in @BeforeClass is " + browsername);
    }

    @Parameters({"browsername"})
    @Test
    public void testMethod(String browsername) {
        System.err.println("Browser name in @Test is " + browsername);
    }

}

Suite file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="1265_Suite" parallel="false" verbose="2">
    <test name="92" parallel="false" preserve-order="true">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.ParameterizedClass"/>
            <parameter name="browsername" value="firefox"/>
        </classes>
    </test>
</suite>

Output

...
... TestNG 6.11 by Cédric Beust ([email protected])
...
Browser name in @BeforeClass is firefox
Browser name in @Test is firefox

===============================================
1265_Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================

Upvotes: 2

Related Questions