-1

Passing value from testNG.xml to hooks file gives error When a hook declares an argument it must be of type cucumber.api.Scenario. public void Step_Defination.Hooks.openBrowser(java.lang.String) throws java.io.IOException hooks.java class TestNG.xml

Abhishek Gaur
  • 300
  • 1
  • 5
  • 20
  • 2
    Please share your code to understand us what have you implemented so far. – TheSociety May 07 '19 at 14:22
  • Testng.xml: hooks.java @Before @Parameters("browser") public void openBrowser(String browser) throws IOException { if(browser.equalsIgnoreCase("chrome")){ System.setProperty("webdriver.chrome.driver","driver//chromedriver.exe"); driver = new ChromeDriver(); – Abhishek Gaur May 07 '19 at 14:25
  • Cucumber Before hook can only be injected a Scenario object and will not work here. Simplest would be to have a properties file in the classpath with the browser type. Access this file in the before hook – Grasshopper May 07 '19 at 14:46
  • 1
    Please [edit] your question and add your code. Don't use comments, code is mostly unreadable in comments, and not all comments may be shown initially. Check also [ask]. – Robert May 07 '19 at 15:32
  • 1
    Images of code are not searchable, or accessible. When including code in a question please do so using text and formatting that text appropriately either using the format as code button in the editor, or by indenting it with four spaces. If the code can be executed (HTML and JavaScript) including it using a snippet. – Jason Aller May 07 '19 at 18:40
  • @Grasshopper I want to do cross-browser testing by Property file I have no idea how to achieve it. therefore I was using TestNG.xml to invoke cucumber Page object model framework – Abhishek Gaur May 07 '19 at 19:26

1 Answers1

1

When using TestNG & Cucumber together, we shall not use @Before (cucumber.api.java.Before) to read values from testng.xml rather we shall use @BeforeTest (org.testng.annotations.BeforeTest) Below is an example of Hooks.java

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import cucumber.api.testng.AbstractTestNGCucumberTests;

public class Hooks extends AbstractTestNGCucumberTests {

    @Parameters({ "browser" })
    @BeforeTest
    public void setUpScenario(String browser){
        //BaseSteps.getInstance().getBrowserInstantiation(browser);
    }
}
TheSociety
  • 1,936
  • 2
  • 8
  • 20
  • 1
    BaseSteps.getInstance().getBrowserInstantiation(browser); Gives error- The method getInstance() is undefined for the type String – Abhishek Gaur May 07 '19 at 19:27
  • I showed BaseSteps.getInstance().getBrowserInstantiation(browser) just for reference. Please use your own method here. – TheSociety May 08 '19 at 02:12