0
package firsttestngpackage;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.*;

public class FirstTextNGFile {

   public String baseUrl = "http://newtours.demoaut.com/";
   public WebDriver driver = new FirefoxDriver();

   @Test
   public void verifyHomepageTitle() {
      System.setProperty("webdriver.gecko.driver","c:\\geckodriver-v0.24.0-win64\\geckodriver.exe");
      driver.get(baseUrl);
      String expectedTitle = "Welcome: Mercury Tours";
      String actualTitle = driver.getTitle();
      Assert.assertEquals(actualTitle,  expectedTitle);
      driver.quit();
   }
}

What am I doing wrong here?

14
  • You have an escape character in your driver path. Try: ` "c:\\\\geckodriver-v0.24.0-win64\\geckodriver.exe"` instead. If this fixes the problem, I will make it into an answer.
    – Brian
    Commented Feb 26, 2019 at 21:02
  • no, this did not help
    – Samantha
    Commented Feb 26, 2019 at 21:05
  • Check the versions of both Firefox and Gecko.
    – Brian
    Commented Feb 26, 2019 at 21:06
  • How do I do that?
    – Samantha
    Commented Feb 26, 2019 at 21:06
  • Second post here. Also, if there is an exception being thrown, please add that to this post (the stack trace).
    – Brian
    Commented Feb 26, 2019 at 21:08

1 Answer 1

1

Your code cannot work, you need to call

System.setProperty("webdriver.gecko.driver","c:\\geckodriver-v0.24.0-win64\\geckodriver.exe");

Before

public WebDriver driver = new FirefoxDriver();

Because the new FirefoxDriver creates a new instance and tries to find the geckodriver somewhere in your path, unless give it a specific path first. After this it will start the test code.

I would use the TestNG [setUp()][1] method to configure the driver before the test:

public class FirstTextNGFile {

 public WebDriver driver;

 @BeforeClass
 public void setUp() {
   System.setProperty("webdriver.gecko.driver","c:\\geckodriver-v0.24.0-win64\\geckodriver.exe");
   driver = new FirefoxDriver()
 }

 @Test
 public void verifyHomepageTitle() {
   // test code
 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.