Shripal Purohit
Shripal Purohit

Reputation: 43

Testng ignores @Test, when parameters are used

I am trying to execute a simple test wherein firefox browser and chrome browser open at once and they both fetch the same URL. But my @Test is being ignored everytime. The @BeforeClass seems to work fine. Could anyone please help me with this? Thanking in advance.

Here is my code:

public class main {
WebDriver driver;

@BeforeClass  // this will perform before your test script
@Parameters({"browser"}) // Here it will pickup the parameters given in XML file
public void beforeTest(String browser){
    if(browser.equalsIgnoreCase("chrome")) {
        System.out.println("Chrome");
        System.setProperty("webdriver.chrome.driver", "D:/chromedriver_32bit_forChrome76/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    } else if(browser.equalsIgnoreCase("Firefox")){
        System.out.println("Firefox");
        System.setProperty("webdriver.gecko.driver","D:/geckodriver_64/geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
    }
}

@Test (alwaysRun = true)
public void setBaseUrl(WebDriver driver) throws InterruptedException {
    //For both the browsers
    System.out.println("Inside the Test");
    driver.get("https://google.com");
}

  @AfterClass // this will quit your browser after execution
public void afterTest() throws Exception{
    Thread.sleep(10);
    driver.quit();
}}

The testng.xml file corresponding to it is:

<suite name="SmokeTest">

   <test name="setBaseUrlFirefox">

      <parameter name="browser" value="firefox"/>

      <classes>

         <class name="Products.main"/>

      </classes>

   </test> <!-- Test -->

   <test name="setBaseUrlChrome">

       <parameter name="browser" value="chrome"/>

       <classes>

          <class name="Products.main"/>

       </classes>

   </test> <!-- Test -->

Upvotes: 0

Views: 311

Answers (2)

e1che
e1che

Reputation: 1251

Because you are using a local variable, which is not instanced. I renamed your variables to make it clear. (driverglobaland driverlocal)

WebDriver driverglobal;  

@Parameters({"browser"}) // Here it will pickup the parameters given in XML file public void beforeTest(String browser){
    if(browser.equalsIgnoreCase("chrome")) {
        //code ...
        driverglobal = new ChromeDriver();
        driverglobal.manage().window().maximize();
    } else if(browser.equalsIgnoreCase("Firefox")){
       //code ...
        driverglobal = new FirefoxDriver();
        driverglobal.manage().window().maximize();
    } 
}


@Test (alwaysRun = true)  
public void setBaseUrl(WebDriver driverlocal) throws InterruptedException {
        //For both the browsers
        System.out.println("Inside the Test");
        driverlocal.get("https://google.com"); }

Basically, you have to delete the entering condition from the procedure setBaseUrl.

Upvotes: 1

Vault23
Vault23

Reputation: 753

Why are you passing driver in public void setBaseUrl(WebDriver driver)? try using @BeforeTest annotation instead of @BeforeClass and your setBaseUrl without parameters, so each test will be with different WebDriver

Upvotes: 0

Related Questions