【问题标题】:org.testng.TestNGException: Cannot instantiate classorg.testng.TestNGException:无法实例化类
【发布时间】:2017-02-02 21:21:26
【问题描述】:

我在尝试运行脚本时收到以下错误

org.testng.TestNGException:无法实例化类原因: java.lang.reflect.InvocationTargetException 原因: java.lang.IllegalStateException:驱动程序可执行文件的路径 必须由 webdriver.ie.driver 系统属性设置;

package EDRTermsPackge;

import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;

public class ContactInformationTesting {

 //For use of IE only; Please enable for IE Browser
 WebDriver driver = new InternetExplorerDriver();

 @BeforeMethod
 public void beforeMethod() {
  //Using or Launching Internet Explorer
  String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";

  //For use of IE only; Please enable for IE Browser
  System.setProperty("webdriver.ie.driver", exePath);
 }

 @Test
 public void OpenPage_Login() {
  driver.get("http://cp-qa.harriscomputer.com/");
 }
}

【问题讨论】:

  • @JJWhispers - 我相信您应该使用源代码更新您的问题,而不是将代码作为您自己问题的答案之一发布。我已经编辑了您的问题以包含您的源代码共享(我清理了注释掉的代码)

标签: java selenium testng


【解决方案1】:

你应该先设置你的驱动路径然后实例化IEDriver,你不能在System.setProperty("webdriver.ie.driver", exePath);之前使用new InternetExplorerDriver();

在你的情况下,你可以做这样的事情(不需要@BeforeMethod 只做这个简单的属性设置):

public class ContactInformationTesting {

  //Using or Launching Internet Explorer
  String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe";

  //For use of IE only; Please enable for IE Browser
  System.setProperty("webdriver.ie.driver", exePath);

  //For use of IE only; Please enable for IE Browser
  WebDriver driver = new InternetExplorerDriver();

 @Test
 public void OpenPage_Login() {
  driver.get("http://cp-qa.harriscomputer.com/");
 }

【讨论】:

  • 由于令牌上的更改语法错误,我收到此错误消息,请删除这些令牌
  • public class ContactInformationTesting { //使用或启动 Internet Explorer String exePath = "\\Users\\jj85274\\Desktop\\IEDriverServer.exe"; System.setProperty("webdriver.ie.driver", exePath); //仅供IE使用;请启用 IE 浏览器 WebDriver driver = new InternetExplorerDriver(); //仅供IE使用;请为 IE 浏览器启用 @Test public void OpenPage_Login() { driver.get("cp-qa.harriscomputer.com/"); }
【解决方案2】:

String exePath = "\Users\jj85274\Desktop\IEDriverServer.exe";

这行提示就像您尝试从网络驱动器设置 IEDriverServer 二进制文件一样。是这样吗 ?我不确定Java代码是否可以直接访问网络路径。

我建议不要尝试在源代码中为每个测试添加IEDriverServer.exe 路径,还不如将这个二进制文件包含在PATH 变量中。您可以通过将此 exe 放入以下命令输出中列出的目录之一在 Windows 上执行此操作

在 Windows 上 echo %PATH%

在非 Windows 上 echo $PATH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 2021-07-27
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多