0

두 개의 분리 된 Selenium Webdriver 클래스가 있습니다. 내가 처음 만든 클래스는 오류없이 실행되지만 두 번째 클래스는 NullPointerException을 throw합니다.
오류 메시지는 WebDriverWait wait = new WebDriverWait(driver,40);입니다. WebDriverWait뿐만 아니라 driver이 코드 줄에서 인스턴스화 된 경우 NullPointer 예외가 throw됩니다. 예 : Actions action = new Actions(driver);.
NullPointerException도 위의 코드를 throw합니다. 하지만 첫 번째 수업에는 문제가 없습니다.Selenium 드라이버에서 NullPointerException이 발생하는 이유는 무엇입니까?

는 클래스 I 만든 제

package Initiate; 

import dataProvider.CommonClass; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.interactions.Actions; 
import org.openqa.selenium.support.ui.ExpectedConditions; 
import org.openqa.selenium.support.ui.WebDriverWait; 

import java.util.concurrent.TimeUnit; 

public class NewBOM { 

    public WebDriver driver; 
    By newBomButton = By.id("btnUpdate"); 

    public NewBOM(WebDriver driver){ 
    this.driver=driver; 
     this.CreateNewBOM(); 

    } 

    public void CreateNewBOM(){ 
     Actions action = new Actions(driver); 
     WebDriverWait wait = new WebDriverWait(driver, 40); 
     wait.pollingEvery(2, TimeUnit.SECONDS); 

     WebElement newBomBtn = driver.findElement(newBomButton); 
     wait.until(ExpectedConditions.elementToBeClickable(newBomBtn)); 
     action.moveToElement(newBomBtn).click().build().perform(); 
    } 
} 

nullpointer 예외

package Initiate; 

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.support.ui.WebDriverWait; 


public class NewBOO { 
    public WebDriver driver; 

    public NewBOO(WebDriver driver){ 
     this.driver=driver; 
    } 

    public void test() throws InterruptedException { 
     WebDriverWait wait = new WebDriverWait(driver,40); 
     System.out.println("Test print"); 
    } 
} 
을 발생 제 클래스 I를위한 1

import Initiate.NewBOM; 
import org.junit.BeforeClass; 
import org.openqa.selenium.WebDriver; 
import org.testng.annotations.Test; 

public class CreateBOM { 
    public WebDriver driver; 

    private NewBOM objNewBOM; 

    @Test(priority = 1) 
    public void clickOnNewBOMButton(){ 
     objNewBOM = new NewBOM(driver); 
     objNewBOM.CreateNewBOM(); 
    } 

} 

TestNG의 클래스로 이루어지는 클래스

TestNG의 클래스 내가 만든 2 등분

import Initiate.NewBOO; 
import org.openqa.selenium.WebDriver; 
import org.testng.annotations.Test; 

public class CreateBOO { 
    public WebDriver driver; 
    NewBOO objNewBOO; 
    @Test(priority = 1) 
    public void temporyTest() throws InterruptedException { 
     objNewBOO = new NewBOO(driver); 
     objNewBOO.test(); 
    } 
} 

전체 오류 메시지

java.lang.NullPointerException 
    at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:787) 
    at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:96) 
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71) 
    at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45) 
    at Initiate.NewBOO.test(NewBOO.java:17) 
    at CreateBOO.temporyTest(CreateBOO.java:11) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:661) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) 
    at org.testng.TestRunner.privateRun(TestRunner.java:744) 
    at org.testng.TestRunner.run(TestRunner.java:602) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:380) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:289) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1226) 
    at org.testng.TestNG.runSuites(TestNG.java:1144) 
    at org.testng.TestNG.run(TestNG.java:1115) 
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:123) 


=============================================== 
Default Suite`enter code here` 
Total tests run: 1, Failures: 1, Skips: 0 
=============================================== 


Process finished with exit code 0 

나는 개별적으로 모두 TestNG의 클래스를 테스트하고 두 클래스는 독립적이다. 하지만 첫 번째 클래스는 null 포인터 예외없이 실행되고 두 번째 클래스는 nullpointer 예외를 throw합니다.

+3

여기서 '드라이버'는 초기화합니까? – Guy

+0

어떤 드라이버가 있습니까? 두 클래스에서 초기화했습니다. –

+1

제공된 코드 어디에도 초기화가 표시되지 않습니다. – Guy

답변

0

코드가 init WebDriver가 아니므로 사용하려고 시도 했으므로 NullPointerException이 표시됩니다. TestNG에서 @BeforeSuite, @BeforeTest, @BeforeClass를 사용하여 종속성을 초기화 할 수 있습니다. 아래 예제는 'BeforeClass'에서 웹 드라이버를 초기화하는 방법을 보여줍니다.

import Initiate.NewBOM; 
import org.junit.BeforeClass; 
import org.openqa.selenium.WebDriver; 
import org.testng.annotations.Test; 

public class CreateBOM { 
    public WebDriver driver; 

    private NewBOM objNewBOM; 

    @BeforeClass 
    public void setupClass(){ 
     driver = new FirefoxDriver(); 
    } 

    @Test(priority = 1) 
    public void clickOnNewBOMButton(){ 
     objNewBOM = new NewBOM(driver); 
     objNewBOM.CreateNewBOM(); 
    } 

}