org.testng.TestNGException:无法实例化类

时间:2017-02-02 21:21:26

标签: java selenium testng

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

  

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/");
 }
}

2 个答案:

答案 0 :(得分:2)

您应首先设置路径到驱动程序,然后实例化IED驱动程序,在new InternetExplorerDriver();之前不能使用System.setProperty("webdriver.ie.driver", exePath);

在你的情况下你可以做这样的事情(不需要@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/");
 }

答案 1 :(得分:0)

  

String exePath =“\ Users \ jj85274 \ Desktop \ IEDriverServer.exe”;

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

我建议您不要尝试在每个测试的源代码中添加IEDriverServer.exe路径,也可以在PATH变量中包含此二进制文件。您可以通过将此exe放入以下命令

的输出中列出的目录之一来在Windows上执行此操作

在Windows上 echo %PATH%

在非Windows上 echo $PATH