无法实例化类

时间:2014-01-30 15:20:58

标签: java selenium-webdriver testng

iam面临一个问题我已经编写了一个方法navback,我需要定期使用导航回来。当我运行它时会抛出一个错误。

下面是代码。

    package Examples;

import java.util.concurrent.TimeUnit;

//import org.junit.BeforeClass;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Flipkart {
    public static WebDriver driver;
    Actions action = new Actions(driver);
//String ddd;
//    public Example2() {
//       super();
//    }

    @BeforeClass
    public void beforeClass()
    {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);


    }

    @Test

    public void mailSend() throws InterruptedException
    {

        driver.get("https://www.flipkart.com/");
        driver.manage().window().maximize();


        navback();
        driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[2]/a")).click();
        driver.navigate().back();


        navback();
        driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[3]/a")).click();
        driver.navigate().back();

        driver.navigate().refresh();
    }

    public void navback()
    {
          WebElement we = driver.findElement(By.xpath("//html/body/div/div/div[2]/div/div/ul/li/a/span"));
          action.moveToElement(we).build().perform();
    }



    @AfterClass
    public void tear()
    {
       // driver.quit();
   }
}

以下是错误。

org.testng.TestNGException: 
Cannot instantiate class Examples.Flipkart
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
    at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
    at org.testng.TestRunner.initMethods(TestRunner.java:409)
    at org.testng.TestRunner.init(TestRunner.java:235)
    at org.testng.TestRunner.init(TestRunner.java:205)
    at org.testng.TestRunner.<init>(TestRunner.java:160)
    at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
    at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
    at org.testng.SuiteRunner.init(SuiteRunner.java:157)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
    ... 21 more
Caused by: java.lang.NullPointerException
    at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)
    at Examples.Flipkart.<init>(Flipkart.java:18)
    ... 26 more

请有人帮助我,我无法继续下去。

由于

1 个答案:

答案 0 :(得分:3)

您没有初始化driver,因此它是null,但您将其传递给Actions

public static WebDriver driver;
Actions action = new Actions(driver);

抛出NullPointerException

Caused by: java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)

初始化driver


请注意生命周期。在JUnit运行@BeforeClass@Before方法之前,必须创建Flipkart实例。实例字段初始化表达式在此时运行。

重新考虑你的设计。在action初始化后初始化driver