eclipse 线程“main”中的异常 java.lang.IllegalStateException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39309669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:16:40  来源:igfitidea点击:

Exception in thread "main" java.lang.IllegalStateException

javaeclipseseleniumtestingautomation

提问by Aly Magdy

I am studying automation testing tutorial by selenium and was writing my first script in java language and have got that message in "Console" at Eclipse.

我正在学习 selenium 的自动化测试教程,并且正在用 java 语言编写我的第一个脚本,并且在 Eclipse 的“控制台”中收到了该消息。

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
at org.openqa.selenium.firefox.GeckoDriverService.access0(GeckoDriverService.java:38)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:91)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:245)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:220)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:215)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:211)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:124)
at automationFramework.FirstTestCase.main(FirstTestCase.java:12)

My Code :

我的代码:

package automationFramework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstTestCase {

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    // Create a new instance of the Firefox driver
    WebDriver driver = new FirefoxDriver();

    //Launch the Online Store Website
    driver.get("http://www.store.demoqa.com");

    // Print a Log In message to the screen
    System.out.println("Successfully opened the website www.Store.Demoqa.com");

    //Wait for 5 Sec
    Thread.sleep(5);

    // Close the driver
    driver.quit();
}

}

The Tutorial Link : http://toolsqa.wpengine.com/selenium-webdriver/first-test-case/

教程链接:http: //toolsqa.wpengine.com/selenium-webdriver/first-test-case/

回答by Daniel

This is because Selenium 3 uses a seperate driver to interact with the Firefox browser.

这是因为 Selenium 3 使用单独的驱动程序与 Firefox 浏览器交互。

Check out this link.

查看此链接

Add the path of geckodriver.exewith System.setProperty. Assuming path C:\Selenium\Firefox driver\geckodriver.exe:

添加geckodriver.exewith的路径System.setProperty。假设路径C:\Selenium\Firefox driver\geckodriver.exe

System.setProperty("webdriver.gecko.driver","C:\Selenium\Firefox driver\geckodriver.exe");
WebDriver driver = new FirefoxDriver();