新安装时出现nullpointerexception错误

时间:2017-06-26 15:50:13

标签: java selenium selenium-webdriver selenium-firefoxdriver

由于此错误,我们已经创建了一个全新的eclipse安装(我删除了所有文件,但我猜测我错过了一些文件)

这是错误:

java.lang.NullPointerException
at testing.testsrc.testcase1(testsrc.java:16)
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:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

将它放在一个全新的项目上也是如此,相信这远离任何项目特定的配置。

有人可以帮忙吗?

编辑: 作为示例,此代码也将返回相同的错误:

package testing;
package TestsStudies;

import static org.junit.Assert.*;

import java.awt.Desktop.Action;
import java.awt.List;
import java.awt.RenderingHints.Key;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.gargoylesoftware.htmlunit.javascript.host.Iterator;

public class TestingStd {

    WebDriver driver;
    WebElement sidearrow;
    WebElement sidemenu;
    WebDriverWait wait = new WebDriverWait(driver, 3);

    @Before
    public void setUp() throws InterruptedException { //Creating driver and connecting to url
        String baseURL = "url";
        System.setProperty("webdriver.gecko.driver", "/Users/user/Desktop/gecko/geckodriver.exe");
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get(baseURL);
        driver.findElement(By.id("isid")).sendKeys("username");
        WebElement Password = driver.findElement(By.id("password"));
        Password.sendKeys("password");
        Password.sendKeys(Keys.RETURN);     
        Thread.sleep(1500);
        driver.findElement(By.xpath("//img[@ src='/url/images/icons/logo_gif/Br.svg']")).click();;
        Thread.sleep(1500);
        driver.findElement(By.xpath("//*[@id=\"nosaveq\"]/div[3]/div[1]/span/img")).click();;
        Thread.sleep(1500);

    }

    @Test
    public void expDescriptionandSave() throws InterruptedException {

        sidemenu = driver.findElement(By.id("optionstd"));
        sidearrow = driver.findElement(By.id("slideButton_internal"));
        Actions builder = new Actions(driver);
        builder.moveToElement(sidemenu).perform();
        Thread.sleep(1500);
        builder.moveToElement(sidearrow).click().perform();
        driver.findElement(By.xpath("//*[@id=\"unusedFolderProtocols\"]/div[2]")).click();
        Thread.sleep(1500);
        driver.findElement(By.xpath("//*[@id=\"unusedFolderProtocolsContent\"]/div[1]/div[2]/div/img")).click(); //click BioELN
        Thread.sleep(1500);
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
        System.out.println(timeStamp + " experimentDescriptionandSave");
        driver.switchTo().frame("details");
        driver.findElement(By.cssSelector("#experiments > table > tbody > tr:nth-child(3) > td:nth-child(2) > textarea")).sendKeys(timeStamp);
        driver.findElement(By.xpath("//*[@title='create study']")).click();
        Thread.sleep(1500);
        String timeStampSecond = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
        System.out.println(timeStampSecond + " experimentDescriptionandSave");
        WebElement description = driver.findElement(By.name("DESCR"));
        description.clear();
        description.sendKeys(timeStampSecond);
        driver.findElement(By.xpath("//*[@id=\"exp\"]/table/tbody/tr[11]/td/label[1]")).click();
        Thread.sleep(1500);
        String descriptionText = driver.findElement(By.name("DESCR")).getText();
        assertTrue(descriptionText.contains(timeStampSecond));


    }

2 个答案:

答案 0 :(得分:1)

根据您的代码,您没有初始化驱动程序变量。基本代码是 -

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

此外,根据您要使用的浏览器,您还需要下载并设置相应的驱动程序。对于Firefox,它被称为GeckoDriver,对于Chrome,它被称为ChromeDriver。最后,您需要在脚本中提供驱动程序位置。所以代码看起来像这样 -

System.setProperty("webdriver.gecko.driver", "C:\\some_folder\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");

如果您正在寻找详细步骤,那么您可以按照这篇文章进行操作 - Selenium WebDriver Setup

答案 1 :(得分:0)

在您的代码中,您似乎尚未初始化driver

因此,您可能希望在编写driver.get();语句之前添加两个语句。

    System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"\\lib\\chromedriver.exe");  
   //Or any other browser's driver that you have used.
    driver = new ChromeDriver();