簡體   English   中英

TestNG:@Test方法中的java.lang.NullPointerException

[英]TestNG: java.lang.NullPointerException in the @Test Method

請幫助我,@ @BeforeClass成功運行,但是進入@Test方法,它將引發以下異常。 @Test中的代碼無法正常運行。

我可以在幾個月前成功運行代碼,但現在無法正常工作。 我正在使用selenium-server-standalone-2.39.0.jar

例外:

java.lang.NullPointerException
    at scripts.IRS_TP_3_Contribution.test990TP2(IRS_TP_3_Contribution.java:51)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:335)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:330)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    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)

這是代碼:

@BeforeClass 

public void launchBrowser() throws IOException, InterruptedException 
{
System.setProperty("webdriver.chrome.driver","E://FELIX//Automation Testing//Jar files//chromedriver.exe");  
        WebDriver driver = new ChromeDriver();      
        driver.get("Link Hided");

        driver.findElement(By.id("LEmailAddress")).clear();
        driver.findElement(By.id("LEmailAddress")).sendKeys("felix@gmail.com");
        driver.findElement(By.id("Password")).clear();
        driver.findElement(By.id("Password")).sendKeys("123456");
        driver.findElement(By.id("btnSubmit")).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//body/div[2]/div[3]/div[3]/div/div[1]/table/tbody/tr/td[1]/form/div/div[3]/table/tbody/tr[9]/td[3]/a")).click();
        //driver.findElement(By.xpath("(//a[contains(text(),'Edit')])[5]")).click();
        driver.findElement(By.id("btnContributionRevenueStart")).click();
        driver.findElement(By.id("btnContributionGiftsStart")).click();
        driver.findElement(By.linkText("Add Contributions and Grants")).click();
        //driver.findElement(By.id("ContributorName")).clear();


        // Need Iteration for 992
        //driver.findElement(By.id("Line1_IsGrantsEquivalentYes")).click();
        //driver.findElement(By.xpath("//input[@value='Go to Express990']")).click();
        /*for (int second = 0;; second++) {
            if (second >= 20) fail("timeout");
            try { if (isElementPresent(By.xpath("//input[@value='Go to Express990']"))) break; } catch (Exception e) {}
            Thread.sleep(1000);
        }

        driver.findElement(By.xpath("//input[@value='Go to Express990']")).click();*/
    }

@Test (dataProvider = "DP1")
      public void test990TP2(String contributorName, String contributorType, String add1, String city, String state, String zip, String amount) throws Exception {
        driver.manage().timeouts().implicitlyWait(05, TimeUnit.SECONDS);
        //driver.findElement(By.id("btnAddAnother")).click();
        // Orgnaization Info
        // ERROR: Caught exception [ERROR: Unsupported command [typeKeys | id=EIN | 00-9000004]]
        /*driver.findElement(By.linkText("Add Contributions and Grants")).click();
        //driver.findElement(By.id("ContributorName")).clear();
        driver.findElement(By.id("ContributorName")).sendKeys(contributorName);*/
        driver.findElement(By.id("ContributorName")).sendKeys(contributorName);
        new Select(driver.findElement(By.id("ContributorType"))).selectByVisibleText(contributorType);
        driver.findElement(By.id("Address1")).clear();
        driver.findElement(By.id("Address1")).sendKeys(add1);
        driver.findElement(By.id("City")).clear();
        driver.findElement(By.id("City")).sendKeys(city);
        new Select(driver.findElement(By.id("USStates"))).selectByVisibleText(state);
        driver.findElement(By.id("ZipCode")).clear();
        driver.findElement(By.id("ZipCode")).sendKeys(zip);
        driver.findElement(By.xpath("//div[@id='codeRefer']/table/tbody/tr[16]/td[2]/p/label/span")).click();
        driver.findElement(By.id("IsContributionNonCashYes")).click();
        driver.findElement(By.id("GrantTotalContribution")).clear();
        driver.findElement(By.id("GrantTotalContribution")).sendKeys(amount);
        driver.findElement(By.id("btnAddAnother")).click();
        // Ends
      }

之所以會得到NullPointerException是因為您的指針driver是在方法launchBrowser()創建的。

如果要在兩種方法中都使用指針driver ,則必須將其設置為實例變量。

例:

public class Example {
    Webdriver driver; //instance variable - available for all methods in this class

@BeforeTest
public void launchBrowser() {
    driver = new Chromedriver();
    }    
}

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM