【问题标题】:Null pointer exception after opening chrome browser (Selenium)打开 chrome 浏览器 (Selenium) 后出现空指针异常
【发布时间】:2020-07-26 05:38:59
【问题描述】:

希望你们一切都好。

您能帮我解决这个空指针问题吗?我正在我的公司构建一个新的 Selenium 框架。 如下所述,我从浏览器类调用基类中的方法“StartBrowser()”。我的代码在“StartBrowser()”之前一直正常执行,但在那之后,它会抛出空指针异常。

非常感谢您。

{
public WebDriver driver;

public WebDriver StartBrowser() throws IOException
{
    Properties prop=new Properties();
    FileInputStream fis=new FileInputStream(System.getProperty("user.dir")+"\\src\\main\\java\\config\\config.properties");
    prop.load(fis);
    String browserName=prop.getProperty("browser");
    String LADSurl=prop.getProperty("LADSurl");
    String GADSurl=prop.getProperty("GADSurl");
    String chromeDriverPath=prop.getProperty("ChromeDriverPath");
    System.out.println(browserName);
    
    if (browserName.equalsIgnoreCase("chrome"))
    {
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        driver=new ChromeDriver();
    }
    else if(browserName.equalsIgnoreCase("firefox"))
    {   
    }
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
    return driver;
}

}

public class Base {
public static WebDriver driver;
BrowserFactory browser;
public static Properties prop;
public static String LADSurl;
public static String GADSurl;


enter code here

public static void setupPropertiesFile() throws IOException {
    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream(
            System.getProperty("user.dir") + "\\src\\main\\java\\config\\config.properties");
    prop.load(fis);
    String browserName = prop.getProperty("browser");
    String LADSurl = prop.getProperty("LADSurl");
    String GADSurl = prop.getProperty("GADSurl");
    String chromeDriverPath = prop.getProperty("ChromeDriverPath");
}
public static void OpenApplication(String environment) throws IOException {
    BrowserFactory browser=new BrowserFactory();
    browser.StartBrowser();
    if (environment.equalsIgnoreCase("LADS")) {
        driver.get("www.google.com"); // Local ADS URL
    } else if (environment.equalsIgnoreCase("GADS")) {
        driver.get(GADSurl); // Global ADS URL
    } 

}

控制台错误: 2020 年 7 月 26 日 1:49:05 PM cucumber.api.cli.Main 运行 警告:您正在使用已弃用的 Main 类。请使用 io.cucumber.core.cli.Main

场景:创建学生 # src/test/resources/Features/CreateUsers.feature:3 铬合金 在端口 47410 上启动 ChromeDriver 84.0.4147.30 (48b3e868b4cc0aa7e8149519690b6f6949e110a8-refs/branch-heads/4147@{#310}) 只允许本地连接。 请参阅https://chromedriver.chromium.org/security-considerations 获取有关确保 ChromeDriver 安全的建议。 ChromeDriver 已成功启动。 2020 年 7 月 26 日下午 1:49:09 org.openqa.selenium.remote.ProtocolHandshake createSession 信息:检测到的方言:W3C 给定用户在新用户页面 # stepDefinitions.CreateUserSteps.user_is_on_new_user_page() java.lang.NullPointerException 在 utils.Base.OpenApplication(Base.java:39) 在 stepDefinitions.CreateUserSteps.user_is_on_new_user_page(CreateUserSteps.java:38) 在 ✽.User 在新用户页面上(file:///C:/Users/rgorilla/eclipse-workspace/ESA/src/test/resources/Features/CreateUsers.feature:4)

当用户提供学生信息时,点击保存用户# stepDefinitions.CreateUserSteps.user_provide_student_information_click_save_user() 然后验证用户创建成功# stepDefinitions.CreateUserSteps.validate_user_is_created_successfully()

【问题讨论】:

  • 您需要显示空指针异常的完整堆栈跟踪。
  • 嗨!感谢您的答复。我刚刚添加了控制台错误。这是你所期待的吗?对不起,如果不是,这是我在这个网站上的第一个问题。请指教。
  • Base 中的字段 driver 似乎没有被分配到。

标签: javascript java selenium selenium-webdriver


【解决方案1】:

在您的基类中,驱动程序实例为空,因为您没有分配它。

在您的 openApplication() 方法中添加以下内容

driver = browser.StartBrowser();

【讨论】:

    【解决方案2】:
    Null point exception happening because of driver instance .In your automation framework have more pages so every pages should handled by same driver instance otherwise you will get null point exception 
    
    Plese change your code to  **static WebDriver driver**; from public WebDriver driver;
    

    【讨论】:

      猜你喜欢
      • 2018-04-11
      • 1970-01-01
      • 2021-06-10
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-25
      • 2017-07-26
      • 2017-01-13
      相关资源
      最近更新 更多