2017-11-21 2 views
0

자바 널 포인터 예외가 발생합니다. 나는 그것을 풀 수 없다. 내 testbase 클래스에서 드라이버를 초기화가 내 Testing_TVO 클래스에서 동일한 드라이버를 사용하려면 다음 TestNG 예외 : 클래스를 인스턴스화 할 수 없습니다.

내 시험 기본 클래스 여기

public class testbase { 

public static WebDriver driver; 
String baseURL = "https://mpower.tvo.org/educators/#/login/"; 
String browser = "firefox"; 


public void init() 
{ 
    selectBrowser(browser); 
    getUrl(baseURL); 
} 

public void selectBrowser(String browser) 
{ 
    if(browser.equalsIgnoreCase("firefox")) 
    { 
     System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe "); 
     driver = new FirefoxDriver(); 
    } 
    if (browser.equalsIgnoreCase("chrome")) 
    { 
     System.setProperty("webdriver.chrome.driver", "H:\\geckodriver-v0.17.0-win64\\chromedriver.exe"); 
     driver = new ChromeDriver(); 
    } 
    if(browser.equalsIgnoreCase("edge")) 
    { 
     System.setProperty("webdriver.edge.driver", "H:\\MicrosoftWebDriver.exe"); 
     driver = new EdgeDriver(); 
    } 

} 

public void getUrl(String url) 
{ 
    driver.get(url); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); 
} 

이다 나는 또한 웹 요소를 초기화 할 내 테스트 클래스입니다 스크립트를 작성

public class Testing_TVO_Website extends testbase { 


    @BeforeClass 
    public void beforeClass() { 
     init(); 

    } 

    WebElement register_btn = driver.findElement(By.xpath(".//*[@id='loginPageRegister1']")); 
    WebElement firstName_tbx = driver.findElement(By.xpath(".//*[@id='firstName']")); 
    WebElement lastName_tbx = driver.findElement(By.xpath(".//*[@id='lastName']")); 

    @FindBy(xpath = ".//*[@id='educatorRoleId']") 
    WebElement role; 
    Select role_sel = new Select(role); 

    @FindBy(xpath = ".//*[@id='boardId']") 
    WebElement board; 
    Select board_sel = new Select(board); 

    @FindBy(xpath = ".//*[@id='schoolId']") 
    WebElement school; 
    Select school_sel = new Select(school); 


    WebElement email_tbx = driver.findElement(By.xpath(".//*[@id='email']")); 
    WebElement emailConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userEmailConfirm']")); 
    WebElement password_tbx = driver.findElement(By.xpath(".//*[@id='userPassword']")); 
    WebElement passwordConfirm_tbx = driver.findElement(By.xpath(".//*[@id='userPasswordConfirm']")); 

    @FindBy(xpath = ".//*[@id='source']") 
    WebElement source; 
    Select source_sel = new Select(source); 

    WebElement agreement_chbx = driver.findElement(By.xpath(".//*[@id='agreement']")); 
    WebElement registerSubmit_btn = driver.findElement(By.xpath(".//*[@id='registerSubmit']")); 



    @Test 
    public void TC_001_Verifying_Registeration() { 
     register_btn.click(); 
     firstName_tbx.sendKeys("Shivam"); 
     lastName_tbx.sendKeys("Patel"); 
     role_sel.selectByIndex(1); 
     board_sel.selectByValue("Algoma DSB"); 
     school_sel.selectByVisibleText("Blind River"); 

     email_tbx.sendKeys("[email protected]"); 
     emailConfirm_tbx.sendKeys("[email protected]"); 
     password_tbx.sendKeys("shivampatel"); 
     passwordConfirm_tbx.sendKeys("shivampatel"); 

     source_sel.selectByIndex(2); 
     agreement_chbx.click(); 
     registerSubmit_btn.click(); 

     assertEquals(driver.findElement(By.xpath(".//*[@id='app-content']/div[3]/div/div/div[2]/h2")), "Thank you for registering!"); 




    } 

하지만 자바 null 포인터 예외가 나타납니다. 다음은 로그 로그 당으로

org.testng.TestNGException: 
Cannot instantiate class com.test.TVO_Assignment.Testing_TVO_Website 
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31) 
    at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410) 
    at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323) 
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126) 
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:191) 
    at org.testng.TestClass.getInstances(TestClass.java:99) 
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:85) 
    at org.testng.TestClass.init(TestClass.java:77) 
    at org.testng.TestClass.<init>(TestClass.java:42) 
    at org.testng.TestRunner.initMethods(TestRunner.java:423) 
    at org.testng.TestRunner.init(TestRunner.java:250) 
    at org.testng.TestRunner.init(TestRunner.java:220) 
    at org.testng.TestRunner.<init>(TestRunner.java:169) 
    at org.testng.remote.support.RemoteTestNG6_10$1.newTestRunner(RemoteTestNG6_10.java:28) 
    at org.testng.remote.support.RemoteTestNG6_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_10.java:61) 
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:616) 
    at org.testng.SuiteRunner.init(SuiteRunner.java:185) 
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:131) 
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1383) 
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1363) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1217) 
    at org.testng.TestNG.runSuites(TestNG.java:1144) 
    at org.testng.TestNG.run(TestNG.java:1115) 
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76) 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23) 
    ... 25 more 
Caused by: java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases 
    at com.google.common.base.Preconditions.checkState(Preconditions.java:738) 
    at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124) 
    at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41) 
    at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115) 
    at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330) 
    at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207) 
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108) 
    at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104) 
    at com.test.TVO_Assignment.Testing_TVO_Website.<init>(Testing_TVO_Website.java:34) 
    ... 30 more 
+0

가능한 [NullPointerException이란 무엇이며 어떻게 수정합니까?] (https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix) -it) – Amalius

+0

@ sp4 페이지 개체를 어디에서 초기화합니까? 아래에서 제안한대로 분리를하는 것이 좋습니다. – Grasshopper

답변

1

이며, 오류가 널 포인터 예외가 아니며, 도마뱀 붙이 드라이버가 설정되지 않은 beacuse 실제 오류는 IllegalStateException이있다. 여기서 문제는 도마뱀 드라이버 경로 끝에 여분의 공간이 있다는 것입니다. 오타 일 수 있습니다.

System.setProperty("webdriver.gecko.driver","H:\\geckodriver-v0.19.0-win64\\geckodriver.exe "); 

위의 행 끝 부분을주의해서 제거하십시오. 문제를 해결합니다.

또한 코드에 문제가 있습니다. 페이지 팩토리 코드와 테스트 코드를 단일 클래스로 결합하여 별도로 만들 수는 없습니다.

+0

'페이지 팩토리 코드와 테스트 코드를 단일 클래스로 결합 할 수 없습니다. '는 주요 문제입니다. – DebanjanB

관련 문제