geckodriver is working fine in java but not working on testng

20 views Asked by At

WebDriver driver;

@BeforeMethod
public void setup() {
    System.setProperty("webdriver.gecko.driver", "C:\\SOFTWARE\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    
    driver.get("http/www.google.com");
    
    driver.manage().window().maximize();
    
    driver.manage().deleteAllCookies();
    driver.get("http/www.google.com")
    

Not getting up only in testng but working on java main method then it working fine

@BeforeMethod should be run.

1

There are 1 answers

0
sashkins On

If the same code works in the main method, but doesn't work in the TestNG @BeforeMethod, then most likely the @BeforeMethod wasn't executed.

You can try to set a breakpoint to any line of the setup() method and run your code in the debug mode to see if the method gets called.
Alternatively, you can add some logger or a simple System.out.println("") statement somewhere in the method.

If the @BeforeMethod didn't run, check the following things:

  • Make sure that at least one @Test exists and gets executed by the TestNG
  • In case the setup() method is located in a separate class (e.g. some BaseTest class), check that that class is added to your testNG .XML file. Or check that your actual 'Test' class extends the 'BaseTest' class.
  • Ensure that no other @Before* hooks were failed. You can add 'alwaysRun = true' (@BeforeMethod(alwaysRun = true)) to make sure that your @BeforeMethod will be executed even if some other hooks were skipped or failed.