0

我試圖執行下面的程序,但得到一個空指針錯誤。 @test無法訪問對象驅動程序。我可能犯的錯誤是什麼?錯誤 -Selenium TestNG框架無法在多個測試中調用驅動程序對象

JavaScript警告:https://www.google.com/xjs/_/js/k=xjs.s.en_US.MHBUsB8Me90.O/m=sx,c,sb,cdos,cr,elog,hsm,jsa,r,qsm,d,csi/am=wCJGjhccAPk_IRQStxAWyAImDiA/rt=j/d=1/t=zcms/rs=ACT90oGwMPBWhsFBKoM1svJAFUVoVQRQug,第7行:改變對象的[[Prototype]]將導致您的代碼運行速度非常緩慢;而是創建使用的Object.create https://www.google.com/?gws_rd=ssl正確的初始[[原型]]值 失敗的對象:websiteTitle

java.lang.NullPointerException 
    at myPackage.TestNGforHDFC.websiteTitle(TestNGforHDFC.java:24) 
    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:108) 

package myPackage; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 

public class TestNGforHDFC { 
    public WebDriver driver; 
    public String urlUnderTest = "http://www.google.com"; 
    public String projLocation = "C:\\Users\\Nikita Agrawal\\Selenium\\geckodriver.exe"; 
    @BeforeTest 
    public void login() 
    { 
     System.setProperty("webdriver.gecko.driver", projLocation); 
     WebDriver driver = new FirefoxDriver(); 
     driver.get(urlUnderTest); 
     System.out.println(driver.getCurrentUrl()); 
    } 

    @Test 
public void websiteTitle() 
    { 
     System.out.println(driver.getTitle()); 
    } 
} 

回答

2

類變量driver從未初始化,你定義一個名爲driver內的新變量login方法。

用途:

public void login(){ 
    driver = new FirefoxDriver(); 
    .... 
} 

相反的:

public void login(){ 
    WebDriver driver = new FirefoxDriver(); 
    .... 
} 
+0

太謝謝你了。它工作 –

+0

@NileshKhoriya我很高興我能幫上忙。如果這解決了您的問題,請不要忘記標記答案。 – Titus

相關問題