java.lang.NullPointerException Webdriver 失败

标签 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):

运行:

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

}

我认为该问题与 GoToIAQ()CheckOfferings() 方法下的 StartQuote 类中对驱动程序的引用有关。如果可能的话,这些方法必须保持不变。我尝试寻找此错误的其他解决方案,但其他解决方案都没有真正与我有相同的情况。

非常感谢您的帮助!

最佳答案

快速查看后,我发现错误似乎是由这一行引发的:

driver.get(url);

这让我认为 driver实例未正确初始化。

我看到您正在创建 WebDriver 的新实例在你的Run类(class) setUp() 。但随后,在 StartQuote您没有分配 WebDriver driver 的类使用此驱动程序实例和 StartQuote类字段WebDriver driver保持为空。你能做的就是你自己 StartQuote类:

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

关于java.lang.NullPointerException Webdriver 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48288057/

相关文章:

java - 忽略属性占位符 - Spring Boot

java - 通过长连接实现与POST(HTTP)的推送技术连接

java - Rxjava - 我可以从不同线程调用 `Subject.onNext` 方法吗?

Python Selenium Safari,禁用日志记录

java - 同时使用@DataProvider 和@Parameters

python - 无法单击下拉列表 Selenium 中的选项

java - 如何将C语言结构定义转换成Java代码?

java - mvn 原型(prototype) :generate which version selected

java - 如何将 Maven 生成的 GAE 框架转变为 2 模块应用程序?

java - VSCode 中的自动 ActiveJDBC 检测