Quality Testing

Quality is delighting customers

org.testng.TestNGException: Can inject only one of <ITestContext, XmlTest> into a @BeforeTest

BrowserSelection

public class BrowserSelection
{
    public static WebDriver driver;

   @Parameters("Browser")
   public static WebDriver ChooseBrowser(String browser)
   {
   if (browser.equalsIgnoreCase("Chrome"))
   {
   System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  }
  else if(browser.equalsIgnoreCase("Firefox"))
  {
  System.setProperty("webdriver.gecko.driver", "D:\\Softwares\\geckodriver-v0.23.0-win64\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
 driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}

return driver;

}

}

QT Class:

public class QT

{

 WebDriver driver;

 @BeforeTest

  public void OpenBrowser(String browser)

  {

  driver = BrowserSelection.ChooseBrowser(browser);

  }

 @AfterTest public void CloseBrowser()

  {

    driver.quit(); }

   } 

}

While running QT Class, throwing " org.testng.TestNGException:
Can inject only one of <ITestContext, XmlTest> into a @BeforeTest " exception. Can anyone help me please.

Views: 470

Reply to This

Replies to This Discussion

Because When you run QT class is looking for a browser(any browser which you pass in POM) but not getting any browser. 

@BeforeTest

 public void OpenBrowser(String browser)

 {

 driver = BrowserSelection.ChooseBrowser(browser);

 }

In the above code OpenBrowser not getting browser as you provide it @Parameters("Browser") in BrowserSelection class and not in QT Class. 

In software testing solution is If you Pass @Parameters("Browser") in QT class you not getting any error and browser launch successfully as it works for me. Please refer to the below code.

POM:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <test thread-count="5" name="Test">
<parameter name="Browser" value="Chrome" />

 <classes>
 <class name="com.xyz.tests.QT"/>
</classes>
</test> <!-- Test -->
</suite>

BrowserSelection Class:

public class BrowserSelection {

    public static WebDriver driver;    

    public static WebDriver ChooseBrowser(String browser) {

        if (browser.equalsIgnoreCase("Chrome")) {

            System.setProperty("webdriver.chrome.driver", "/Drivers/chromedriver.exe");

            driver = new ChromeDriver();

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

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

        } else if (browser.equalsIgnoreCase("Firefox")) {

            System.setProperty("webdriver.gecko.driver", "D:\\Softwares\\geckodriver-v0.23.0-win64\\geckodriver.exe");

            driver = new FirefoxDriver();

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

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

        }

        return driver;

    }

}

QT Class:

public class QT

{

    WebDriver driver;

    @BeforeTest

    @Parameters("Browser")

    public void OpenBrowser(String browser)

    {

        driver = BrowserSelection.ChooseBrowser(browser);

    }


    @AfterTest

    public void CloseBrowser()

    {

        driver.quit();

    }

}

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2019   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service