打开Chrome浏览器(Selenium)后出现空指针异常

时间:2020-07-26 05:38:59

标签: javascript java selenium selenium-webdriver

我希望你们一切都好。

您能帮我解决此空指针问题吗?我正在公司中建立一个新的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 PMcumber.api.cli。主要运行 警告:您正在使用不推荐使用的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 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO:检测到的方言: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) 在â½位置。用户位于新用户页面上(文件:/// 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()

2 个答案:

答案 0 :(得分:0)

在基类中,驱动程序实例为null,因为未分配它。

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

driver = browser.StartBrowser();

答案 1 :(得分:0)

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;