java.lang.NullPointerException Webdriver失败

时间:2018-01-16 18:56:42

标签: java maven selenium-webdriver nullpointerexception

当我尝试在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)

到目前为止,我总共只有两个班级(RunStartQuote):

Run班级

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班级

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();
}

}

我认为此问题与StartQuoteGoToIAQ()方法下CheckOfferings()课程中对该驱动程序的引用有关。如果可能,这些方法必须保持不变。我试图查看其他解决方案是否存在此错误,但其他任何解决方案都没有真正分享我的相同情况。

非常感谢你的帮助!

2 个答案:

答案 0 :(得分:2)

在快速浏览之后,我发现错误似乎是由这一行抛出的:

driver.get(url);

这让我觉得driver实例没有正确初始化。

我发现您正在WebDriver的{​​{1}}班级中创建Run的新实例。但是,在setUp()类中,您没有使用此驱动程序实例分配StartQuote,并且WebDriver driver类字段StartQuote保持为空。您可以在WebDriver driver课程中执行以下操作:

StartQuote

答案 1 :(得分:-1)

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

出错时显示: 失败:跑 显示java.lang.NullPointerException     at Automation.IAQ.pages.StartQuote.GoToIAQ(StartQuote.java:38)

你需要初始化你的对象,driver.get()为此抛出null。在Run类或StartQuote类上。