如何从我的班级开始构造函数?

时间:2018-02-22 04:23:52

标签: java selenium selenium-webdriver selenium-firefoxdriver

我遇到了这个问题,这让我发疯,我有这个课程

    package Test;

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

public class Firefox {

    String keyFirefox = "webdriver.gecko.driver";
    String valueFirefox = "...path/geckodriver.exe";

    public Firefox(){
        System.setProperty(keyFirefox, valueFirefox);
    }

    WebDriver Firefox = new FirefoxDriver();
}

在我的主类中,我实例化了上一个类,可以毫无问题地访问属性

package Test;


public class EntryPoint {

    public static void main(String[] args) {


        Firefox firefoxBrowser = new Firefox();
        firefoxBrowser.Firefox.get("https://www.amazon.com.mx/");


    }

}

但是当它编译时它会给我一个错误:

线程“main”中的异常java.lang.IllegalStateException:驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置;有关更多信息,请参阅https://github.com/mozilla/geckodriver。最新版本可以从https://github.com/mozilla/geckodriver/releases

下载

谢谢!

1 个答案:

答案 0 :(得分:6)

If you want to create the driver after setting the properties, try

public class Firefox {

    static final String FIREFOX_DRIVER = "webdriver.gecko.driver";
    static final String FIREFOX_DRIVER_PATH = "...path/geckodriver.exe";

    WebDriver driver;

    public Firefox(){
        System.setProperty(FIREFOX_DRIVER, FIREFOX_DRIVER_PATH);
        driver = new FirefoxDriver();
    }


}