我是Java的新手,所以请耐心等待。我正在使用Java和Selenium来测试Web应用程序。
基本上,我要做的是从Class Main中的cvs文件中读取数据,然后在获得数据后,在另一个类中调用login方法,通过从cvs文件读取的用户名和密码变量。
也许我错误地调用了这个函数,我不确定但是我得到NPE的行是
driver.findElement(By.name("j_username")).clear();
在LiveProcess类的Login函数中。如果我将Login方法移动到Main类中,我没有任何问题。我很感激你能给我的任何帮助 - 提前谢谢!
主类
public void ReadTestData() throws Exception
{
//Open CVS File and extract the contents
try {
CsvReader testData = new CsvReader("C:\\Selenium\\TestData.csv");
testData.readHeaders();
while (testData.readRecord())
{
String testType = testData.get("TestType");
String testName = testData.get("TestName");
String testDescription = testData.get("Description");
String userName = testData.get("UserName");
String password = testData.get("Password");
String firstName = testData.get("FirstName");
String lastName = testData.get("LastName");
int testTypeInt = Integer.parseInt(testType);
RunTests();
}
testData.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
public void RunTests() throws Exception
{
switch(testTypeInt)
{
case 1:
LiveProcessTests method = new LiveProcessTests();
method.Login(userName, password);
break;
default:
break;
}
}
LiveProcess类
public void Login(String uName, String pWord) throws Exception
{
System.out.println("USER NAME: " + uName);
System.out.println("USER NAME: " + pWord);
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).click();
driver.findElement(By.name("j_username")).sendKeys(uName);
}
实际错误:
java.lang.NullPointerException
at com.testscripts.LiveProcessTests.Login(LiveProcessTests.java:31)
at com.testscripts.Main.RunTests(Main.java:97)
at com.testscripts.Main.ReadTestData(Main.java:75)
at com.testscripts.Main.InitializeTests(Main.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
答案 0 :(得分:6)
很可能driver.findElement(By.name("j_username"))
为空,这意味着它无法找到您正在搜索的元素。
确保名称正确且元素存在。此外,您必须初始化驱动程序,但在代码中看不到它
示例:
if (driver.findElement(By.name("j_username")) != null ) {
driver.findElement(By.name("j_username")).clear();
driver.findElement(By.name("j_username")).click();
driver.findElement(By.name("j_username")).sendKeys(uName);
}
<强>更新强>
您应该初始化您的驱动程序。类似的东西:
driver = new FirefoxDriver();