当我尝试在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()类中对驱动程序的引用有关。如果可能,这些方法必须保持相同。我试图寻找其他解决方案来解决此错误,但其他解决方案均未真正遇到我的相同情况。

非常感谢您的帮助!

最佳答案

快速浏览后,我发现此行似乎引发了错误:

driver.get(url);


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

我看到您正在WebDriverRun类中创建setUp()的新实例。但是,在StartQuote类中,您没有为此驱动程序实例分配WebDriver driver,并且StartQuote类字段WebDriver driver保持为空。您可以在StartQuote类中执行以下操作:

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

08-26 01:03