繁体   English   中英

运行硒时出现NullPointerException

[英]NullPointerException while running selenium

在下面的代码下运行时,正在获取null NullPointerException,我只是尝试从属性文件中读取excel路径,但出现错误,请尝试帮助解决此问题,请帮助我

登录脚本

public class Login1 {
    public static Properties props;
    public static WebDriver driver;
    public InputStream propertiesFilePath = this.getClass().getClassLoader()
    .getResourceAsStream("Config.properties");

    public static void main(String [] arg){
    }

    public  void Readproperties() throws IOException {
        props = new Properties();
        props.load(propertiesFilePath);
    }

    public static void  getDriver(){
        System.setProperty("webdriver.chrome.driver",               "C:\\Users\\aroy\\Desktop\\Sele\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    public static void logintoapp(String acc, String passw){
        driver.findElement(By.xpath("//input[@id='userName']")).sendKeys(acc);
        driver.findElement(By.xpath("//input[@id='password']")).sendKeys(passw);
        driver.findElement(By.xpath("//button[@id='loginBtn']")).click();
        System.out.println("Login SuccessFully");
    }
}

Tesstcase:

package AtExecution;

import java.io.IOException;
import org.testng.annotations.Test;

import BeoreExecution.Login1;

public class Testcase extends Login1 {

    public static ExcelLib exel1;

    @Test
    public void login() throws IOException{
        Login1 x = new Login1();
        x.Readproperties();

        exel1 = new ExcelLib(Login1.props.getProperty("TestDataPath"));
        String siteurl = exel1.getCellData("Login_Credentials",    "TestUrl", 2);
        String acc = exel1.getCellData("Login_Credentials", "account", 2);
        String passw=exel1.getCellData("Login_Credentials", "Password", 2);
        Login1.getDriver();
        driver.get(siteurl);
        driver.manage().window().maximize();
        Login1.logintoapp(acc, passw);
    }
}

这是我的日志

    [RemoteTestNG] detected TestNG version 6.12.0
     FAILED: login
     java.lang.NullPointerException
     at java.util.Properties$LineReader.readLine(Unknown Source)
     at java.util.Properties.load0(Unknown Source)
     at java.util.Properties.load(Unknown Source)
        at BeoreExecution.Login1.Readproperties(Login1.java:30)
     at AtExecution.Testcase.login(Testcase.java:18)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
     at java.lang.reflect.Method.invoke(Unknown Source)
at            org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)

请帮我解决这个问题

我的配置文件数据是

TestDataPath = C:/Users/Testdata.xlsx

以下是我的exelsheet数据

链接到Excel工作表

您没有在测试用例类中实例化WebDriver实例。 您可以按照以下方式进行:

从方法返回WebDriver-

public static WebDriver  getDriver(){
    System.setProperty("webdriver.chrome.driver",               "C:\\Users\\aroy\\Desktop\\Sele\\chromedriver_win32\\chromedriver.exe");
    driver = new ChromeDriver();
    return driver;
}

然后在登录测试方法中调用此方法,例如

WebDriver driver = Login1.getDriver();

最后继续下一个代码。

问题在您的代码的以下片段中

public InputStream propertiesFilePath = this.getClass().getClassLoader()
    .getResourceAsStream("Config.properties");

    public  void Readproperties() throws IOException {
        props = new Properties();
        props.load(propertiesFilePath);
    }

Java在CLASSPATH中找不到名称为Config.properties的资源。 您需要检查并确保该文件在CLASSPATH中可用并且可以被Java读取。 解决此问题后,您的NullPointerException应该消失了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM