代码之家  ›  专栏  ›  技术社区  ›  Reelbigcohen

Java语言lang.NullPointerException Webdriver失败

  •  1
  • Reelbigcohen  · 技术社区  · 7 年前

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

    到目前为止,我总共只有两门课( Run StartQuote ):

    :

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

    }

    StartQuaote公司

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

    }

    我相信这个问题与 StartQuaote公司 下的类 GoToIAQ() CheckOfferings() 方法。如果可能,这些方法必须保持不变。我曾试图寻找其他解决此错误的方法,但其他方法都没有与我的情况相同。

    非常感谢您的帮助!

    2 回复  |  直到 7 年前
        1
  •  2
  •   Eugene S    7 年前

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

    driver.get(url);
    

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

    我看到您正在创建的新实例 WebDriver 在您的 Run 中的类 setUp() . 但是,在 StartQuote 您未分配的类 WebDriver driver 使用此驱动程序实例和 StartQuaote公司 “类别”字段 WebDriver驱动程序 保持为空。你能做的就是这样 StartQuaote公司 类别:

    //Constructor
    public StartQuote(WebDriver driver) {
        PageFactory.initElements(driver, this);
        this.driver = driver; //add this
    }
    
        2
  •  -1
  •   ccamol    7 年前
    //Navigates to the IAQ website
    public void GoToIAQ() {
        driver.get(url);
    }
    

    出现错误时显示: 失败:运行 Java语言lang.NullPointerException at自动化。室内空气品质。页。开始计时。GoToIAQ(StartQuote.java:38)

    你需要初始化你的对象,司机。get()为此抛出null。在跑步课或StartKuote课上。