// Class1 Code
int TestCaseID;
public WebDriver driver =null;
@Parameters({ "browser" })
@BeforeMethod
public void BrowserOpen(String browser) throws Exception {
BaseClass Base = new BaseClass();
this.driver = Base.OpenBrowser(browser);
}
//基类
public WebDriver driver = null;
public WebDriver OpenBrowser(String browser) throws Exception
{
if(browser.equalsIgnoreCase("Chrome"))
{
System.setProperty("webdriver.chrome.driver", ReadPropertyFile.ReadProperty("ChromePath"));
driver=new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(ReadPropertyFile.ReadProperty("URL"));
}
if(browser.equalsIgnoreCase("Firefox"))
{
driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(ReadPropertyFile.ReadProperty("URL"));
driver.manage().window().maximize();
}
if(browser.equalsIgnoreCase("IE"))
{
System.setProperty("webdriver.ie.driver", ReadPropertyFile.ReadProperty("IEPath"));
driver=new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(ReadPropertyFile.ReadProperty("URL"));
driver.manage().window().maximize();
}
return driver;
}
我在另一个类中调用web驱动程序并从该类返回驱动程序。
但是问题发生在代码超出@BeforeMethod
时它会将异常作为空指针异常发生,如下所示。我已经添加了基类代码和实际代码。
java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:212)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:102)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at commonLibrary.BaseClass.SafeInput(BaseClass.java:83)
at pageFactoryMethods.LoginPageMethod.fnLogin(LoginPageMethod.java:32)
at pageFactoryTestCases.LoginPageTestCases.TC1(LoginPageTestCases.java:53)
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:100)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:811)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1137)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:753)
at org.testng.TestRunner.run(TestRunner.java:607)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:368)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:363)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:321)
at org.testng.SuiteRunner.run(SuiteRunner.java:270)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1284)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1209)
at org.testng.TestNG.runSuites(TestNG.java:1124)
at org.testng.TestNG.run(TestNG.java:1096)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)