gpt4 book ai didi

java - 获取 selenium webdriver 类的 java.lang.NullPointerException

转载 作者:行者123 更新时间:2023-12-02 03:31:19 25 4
gpt4 key购买 nike

我对 Selenium WebDriver 还很陌生。 java.lang.NullPointerException 已经困扰我一段时间了,我不明白为什么。以下是我的类(class),实际上非常简单:

suiteBase.java

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

testPage1Actions.java

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...");
}
}

TestPage1.java

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) 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)

我还有在 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;
}
}

最佳答案

我以前从未使用过这项技术,但我没有看到初始化元素。

public WebDriver driver;

WebDriver 未初始化。
使用前需要对其进行初始化。喜欢:

WebDriver driver = new FirefoxDriver();

或者别的什么。看看这个:http://www.seleniumhq.org/docs/03_webdriver.jsp

-- UPD:可能在 testCase1() tpa.test(WebDriver) 处抛出异常。 tpa 可能为空。

关于java - 获取 selenium webdriver 类的 java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38042906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com