1
votes

I am using 2 classes for my basic Maven and Selenium project-openGmail. I am using Selenium 3.5 with Firefox 47.0.1 and Gecodriver 0.18.

My Main class is:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MainClass {

    WebDriver driver = new FirefoxDriver();

    public void setup() {
        String Path_GecoDriver="C:/Personal/Selenium/setup/geckodriver-v0.18.0-win64";
        System.setProperty("webdriver.firefox.marionette", Path_GecoDriver+"/geckodriver.exe");
        System.setProperty("webdriver.gecko.driver", Path_GecoDriver+"/geckodriver.exe");

            }
    public void OpenBrowser() {
        String url="http://google.co.in";
        driver.get(url);
    }
    public void LoginGmail() throws InterruptedException {
        String username ="username";
        String passwd = "passwd";
        driver.findElement(By.linkText("Gmail")).click();
        driver.findElement(By.id("identifierId")).sendKeys(username);
        WebElement cli=driver.findElement(By.xpath("//*[text()='Next']"));
        cli.click();
        Thread.sleep(1000);
        driver.findElement(By.xpath("//input[@name='password']")).sendKeys(passwd);
        driver.findElement(By.xpath("//*[text()='Next']")).click();
    }
    public void CloseBrowser() {
                driver.close();
    }
}

My other class is:-

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestngClass {

MainClass mc=new MainClass();

@Before
public void sp() {
mc.setup();
mc.OpenBrowser();
}

@Test
public void LG() throws InterruptedException {
            mc.LoginGmail();
}
@After
public void CB() {
    mc.CloseBrowser();
}
}

I am getting the error

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information

I have tried to execute it the test by putting my System.setProperty (prop, path/to/driver) in my @Before, and @ test also but no change, I am still getting the error.

If I use a single class then everything works fine so I think I am placing my system.setProperty at the wrong place.

I have started with Java and Selenium very recently. I have even tried to put my Gecko driver exe in src/main/resources of my Maven project as mentioned in In System.setProperty("webdriver.gecko.driver", "<Path to your WebDriver>"), what is meant by "Path to your WebDriver"?

1
It should be "webdriver.gecko.driver" and not "webdriver.firefox.marionette". Change that and you should be fine.Andrew Nolan
updated my answerShubham Jain
Let me know if you are able to get rid of IllegalStateExceptionDebanjanB
@SaurabhJain: please do not use code formatting for software names. They are proper nouns, not code.halfer

1 Answers

0
votes

You have set the property two times as below :-

  System.setProperty("webdriver.firefox.marionette", Path_GecoDriver+"/geckodriver.exe");
  System.setProperty("webdriver.gecko.driver", Path_GecoDriver+"/geckodriver.exe");

Remove :-

System.setProperty("webdriver.firefox.marionette", Path_GecoDriver+"/geckodriver.exe");

Another thing is you have to set it before creating the firefox instance. You have define the instance first and then you are setting up the preference .

Try something like below :-

 WebDriver driver =null;

  public void setup() {
      String Path_GecoDriver="C:/Personal/Selenium/setup/geckodriver-v0.18.0-win64";
      System.setProperty("webdriver.gecko.driver", Path_GecoDriver+"/geckodriver.exe");
      driver= new FirefoxDriver();

}

Hope it will help you :)