简体   繁体   中英

java.lang.NullPointerException Webdriver Failure

I am currently receiving the following errors when I try to run on TestNG:

Only local connections are allowed.
Jan 16, 2018 1:31:15 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
FAILED: run
java.lang.NullPointerException
    at Automation.IAQ.pages.StartQuote.GoToIAQ(StartQuote.java:38)
    at Automation.IAQ.startQuote.Run.run(Run.java:28)
    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:124)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:571)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
    at org.testng.TestRunner.privateRun(TestRunner.java:648)
    at org.testng.TestRunner.run(TestRunner.java:505)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
    at org.testng.TestNG.runSuites(TestNG.java:1028)
    at org.testng.TestNG.run(TestNG.java:996)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

I only have a total of two classes so far ( Run and StartQuote ):

Run Class :

package Automation.IAQ.startQuote;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;
import Automation.IAQ.pages.StartQuote;

public class Run {

WebDriver driver;
StartQuote sq;


@BeforeTest
public void setUp() {
    System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/drivers/chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
    driver.manage().window().maximize();
}

@Test
public void run() {
    sq = new StartQuote(driver);
    sq.GoToIAQ();
    sq.ClickNewQuote();
    sq.UncheckOpenNewWindow();
    sq.SelectState("MO");
    sq.CheckOfferings("HO");
    sq.SelectAgentCode("56079");        
}

@AfterTest
public void endTest() {
    driver.close();
}

}

StartQuote Class

package Automation.IAQ.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;

public class StartQuote {

WebDriver driver;
String offeringstate;   
String url = "Some website (let me know if you'll need it, don't want to give it out)";

@FindBy(xpath="//input[@id='FormModel_QuoteType' and @value='new']")//Radio Button
WebElement newqtype;

@FindBy(id="open-quote-in-new-window")//Check Box
WebElement openwindow;

@FindBy(id="FormModel_State")//Dropdown
WebElement state;

@FindBy(id="FormModel_AgentCode")//Dropdown
WebElement agentcode;

@FindBy(xpath="//button[@type='submit'")//Form Submission Button
WebElement submit;

//Constructor
public StartQuote(WebDriver driver) {
    PageFactory.initElements(driver, this); 
}

//Navigates to the IAQ website
public void GoToIAQ() {
    driver.get(url);
}

//Clicks "new" Quote Type radio button
public void ClickNewQuote() {
    newqtype.click();
}

//Unchecks the "open new window" check box
public void UncheckOpenNewWindow() {
    openwindow.click();
}

//Selects the appropriate state from drop down
public void SelectState(String selectedstate) {
    new Select(state).selectByVisibleText(selectedstate);
    offeringstate = selectedstate; //Sets variable equal to state chosen for use in CheckOfferings()    
}

//Checks the appropriate offering check box
public void CheckOfferings(String selectoffering)  {
    switch(selectoffering) {
        case "AA": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_AA') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "BT": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_BT') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "CO": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_CO') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "HO": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_HO') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "MC": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_MC') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "MT": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_MT') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "RT": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_RT') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "SM": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_SM') and contains(@id, '" + offeringstate + "')]")).click();break;
        case "TT": driver.findElement(By.xpath("//input[contains(@id,'FormModel_Offerings_TT') and contains(@id, '" + offeringstate + "')]")).click();break;
    }   
}

//Selects the appropriate Agent Code from drop down
public void SelectAgentCode(String selectedagentcode) {
    new Select(agentcode).selectByVisibleText(selectedagentcode);
}

//Clicks the "Start Quote" submission button
public void ClickStartQuote() {
    submit.submit();
}

}

I believe the issue is related to the reference to the driver in the StartQuote class under the GoToIAQ() and CheckOfferings() methods. Those methods will have to remain the same if possible. I've tried to look at other solutions for this error but none of the other ones really shared my same situation.

Thank you very much for your help!

Just after a quick look I see that the error seem to be thrown by this line:

driver.get(url);

that makes me think that the driver instance was not initialized properly.

I see that you are creating a new instance of WebDriver in your Run class in setUp() . But then, in StartQuote class you are not assigning the WebDriver driver with this driver instance and the StartQuote class field WebDriver driver remains null. What you can do is something like this in you StartQuote class:

//Constructor
public StartQuote(WebDriver driver) {
    PageFactory.initElements(driver, this);
    this.driver = driver; //add this
}
//Navigates to the IAQ website
public void GoToIAQ() {
    driver.get(url);
}

on error show this: FAILED: run java.lang.NullPointerException at Automation.IAQ.pages.StartQuote.GoToIAQ(StartQuote.java:38)

you need to initialize your object, driver.get() throw null for that. Either on your Run class or StartQuote class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM