我是Selenium WebDriver的新手。一段java.lang.NullPointerException
一直困扰着我,我无法理解为什么。以下是我的课程,实际上非常简单:
package utilities.suiteBase;
import org.openqa.selenium.WebDriver;
import actions.testPage1.testPage1Actions;
import ui_map.testPage1.TestPage1UI;
public class suiteBase {
public WebDriver driver;
protected static TestPage1UI tpui = new TestPage1UI();
protected static testPage1Actions tpa = new testPage1Actions();
}
package actions.testPage1;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Timeouts;
import org.openqa.selenium.WebElement;
import utilities.suiteBase.suiteBase;
public class testPage1Actions extends suiteBase {
public WebDriver driver;
public void test(WebDriver driver){
WebElement loc1 = driver.findElement(By.xpath("id('email')"));
loc1.sendKeys("testing");
System.out.println("done...");
}
}
package testPage1;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utilities.suiteBase.BrowserOpen;
import utilities.suiteBase.suiteBase;
public class TestPage1 extends suiteBase{
public WebDriver driver;
BrowserOpen browse = new BrowserOpen();
@Parameters({ "browserType", "appURL" })
@Test(priority = 1)
public void openBrowser(String browserType, String appURL){
browse.setUp(browserType, appURL);
System.out.println("Done....");
}
@Test(priority = 2)
public void testCase1() throws InterruptedException{
driver.wait(1000);
tpa.test(driver);
}
}
我使用XML运行TestPage1.java文件,遇到以下错误:
java.lang.NullPointerException at testPage1.TestPage1.testCase1(TestPage1.java:28)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:497)at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) 在org.testng.internal.Invoker.invokeMethod(Invoker.java:714)at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)at at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)at at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) 在org.testng.TestRunner.privateRun(TestRunner.java:767)at org.testng.TestRunner.run(TestRunner.java:617)at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)at at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)at at org.testng.SuiteRunner.run(SuiteRunner.java:240)at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at org.testng.TestNG.runSuitesSequentially(TestNG.java:1198)at at org.testng.TestNG.runSuitesLocally(TestNG.java:1123)at at org.testng.TestNG.run(TestNG.java:1031)at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
我还有一个在TestPage1之前运行的browserOpen类,其中我声明了WebDriver
package utilities.suiteBase;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utilities.suiteBase.BrowserOpen;
public class BrowserOpen {
public WebDriver driver;
static String driverPath = "E:\\Selenium\\";
public void setUp(String browserType, String appURL) {
try {
setDriver(browserType, appURL);
} catch (Exception e) {
System.out.println("Error....." + e.getStackTrace());
}
}
@AfterClass
public void tearDown() {
driver.quit();
}
private void setDriver(String browserType, String appURL) {
switch (browserType) {
case "chrome":
driver = initChromeDriver(appURL);
break;
case "firefox":
driver = initFirefoxDriver(appURL);
break;
default:
System.out.println("browser : " + browserType
+ " is invalid, Launching Firefox as browser of choice..");
driver = initFirefoxDriver(appURL);
}
}
private static WebDriver initChromeDriver(String appURL) {
System.out.println("Launching google chrome with new profile..");
System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to(appURL);
System.out.println("URL inserted");
// driver.get(appURL);
return driver;
}
private static WebDriver initFirefoxDriver(String appURL) {
System.out.println("Launching Firefox browser..");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to(appURL);
return driver;
}
}
答案 0 :(得分:2)
我之前从未使用过这项技术,但我看不到这些技术。
WebDriver driver = new FirefoxDriver();
WebDriver未初始化 您需要在使用之前初始化它们。 像:
$app->get('/myownurl', MyController::class);
或者别的什么。看看这个:http://www.seleniumhq.org/docs/03_webdriver.jsp
- UPD:异常可能抛出testCase1()tpa.test(WebDriver)。 tpa可能为空。
答案 1 :(得分:0)
您正在使用来自testng.xml文件的TestPage1类中的@Parameters({" browserType"," appURL"})。希望您使用此xml文件来运行测试。
答案 2 :(得分:0)
当我声明-公共静态WebDriver驱动程序时; @BeforeSuite之前
,然后在方法中,如果我们再次声明为
WebDriver driver = new FirefoxDriver();
并运行脚本,然后向我显示java.lang.NullPointerException错误。
请尝试以下代码解决:-
driver = new Firefoxdriver();
示例-
public class LearnCheckBox {
public static WebDriver driver;
@BeforeSuite
public static void verify_SetupBrowser() throws InterruptedException
{
driver = new FirefoxDriver(); //
driver.get("https://learn.letskodeit.com/p/practice");
driver.manage().window().maximize();
Thread.sleep(2000);
}
Ashish Bind