[英]Getting java.lang.NullPointerException for selenium webdriver class
I am quite new to Selenium WebDriver.我对 Selenium WebDriver 很陌生。 A java.lang.NullPointerException
has been troubling me for sometime now, and I cannot understand why. java.lang.NullPointerException
已经困扰我一段时间了,我不明白为什么。 Following are my classes which are quite simple actually:以下是我的课程,实际上非常简单:
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);
}
}
I run the TestPage1.java file using XML, where I encounter following error:我使用 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at 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. 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) at sun.reflect。 DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80) at org.testng.internal .Invoker.invokeMethod(Invoker.java:714) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng。 internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111) at 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 org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) 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 org.testng.TestNG.runSuitesLocally(TestNG.java:1123) at org.testng.TestNG.run(TestNG.java:1031) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58) runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java) :52) 在 org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 在 org.testng.TestNG.runSuitesSequentially(TestNG.java:1198) 在 org.testng.TestNG.runSuitesLocally(TestNG.java:1123) 在 org .testng.TestNG.run(TestNG.java:1031) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) at org. testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
Also I have browserOpen class which I run before TestPage1, in which I declare the WebDriver我也有我在 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;
}
}
I never used this technology before, but I see not inited elements.我以前从未使用过这项技术,但我没有看到初始化元素。
public WebDriver driver;
WebDriver was not initialized. WebDriver 未初始化。
You need to initialize them before using.您需要在使用前对其进行初始化。 Like:像:
WebDriver driver = new FirefoxDriver();
Or something else.或者别的什么。 Check this out: http://www.seleniumhq.org/docs/03_webdriver.jsp看看这个: http : //www.seleniumhq.org/docs/03_webdriver.jsp
-- UPD: Exception is probably throwing at testCase1() tpa.test(WebDriver). -- UPD:异常可能在 testCase1() tpa.test(WebDriver) 处抛出。 tpa is probably null. tpa 可能为空。
you are using @Parameters({ "browserType", "appURL" }) in your TestPage1 class that will come from testng.xml file .您在来自 testng.xml 文件的 TestPage1 类中使用 @Parameters({ "browserType", "appURL" }) 。 Hope your using this xml file to run your test.希望您使用此 xml 文件来运行您的测试。
When I declared - public static WebDriver driver;当我声明 - 公共静态 WebDriver 驱动程序; before @BeforeSuite在@BeforeSuite 之前
and then inside the method if we again declared as然后在方法内部,如果我们再次声明为
WebDriver driver = new FirefoxDriver();
and run the script then it shows me java.lang.NullPointerException error.并运行脚本然后它显示我 java.lang.NullPointerException 错误。
Please try below code to resolve :-请尝试以下代码来解决:-
driver = new Firefoxdriver();
Example -例子 -
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灰烬绑定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.