【问题标题】:java.lang.ExceptionInInitializerError while opening another page through TestNG selenium automationjava.lang.ExceptionInInitializerError 通过TestNG selenium 自动化打开另一个页面
【发布时间】:2021-02-10 17:48:23
【问题描述】:

我使用了 2 页的 POM 模型。登录页面和主页。这 2 个页面有 2 个测试类(LoginTest 和 HomeTest)以及 BaseTest(驱动程序初始化目的和被所有 4 个类继承)。单击 loginpage.verifyLogin(email_id , pswrd) 时,应该打开主页。执行登录测试用例时出现 java.lang.ExceptionInInitializerError。

附言。如果将 verifyLogin() 的返回类型更改为 void,则工作正常。

参考 BaseTest。

public class BaseTest 
{
    public static WebDriver driver ;
    public static String chrome_path ="/home/downloads/chromedriver";
   public static String property_file_path ="/home/CompleteMotorJourneyAutoTest/src/test/java/com/qa/utils/loginDetails.properties";
    
    public BaseTest()
    {
        try {
            FileInputStream fis = new FileInputStream(property_file_path);
            Properties prop = new Properties();
            prop.load(fis);         
            }catch(Exception e) {System.out.println("Exception occured during reading the property file !");}
    }
    
    public static void initialization() 
    {           System.setProperty("webdriver.chrome.driver",chrome_path);
                driver=new ChromeDriver();          
        driver.get(prop.getProperty("URL"));
    }
}

登录页面

public class LoginPage extends BaseTest
{
    @FindBy(xpath="//*[@id=\"loginform\"]/div[1]/div[2]/input")
    WebElement email_id_field;
    @FindBy(xpath="//*[@id=\"loginform\"]/div[2]/div[2]/input")
    WebElement password_field;
    @FindBy(xpath="//*[@id='loginform']/input")
    WebElement login_button_Field;

    public LoginPage()
    {       PageFactory.initElements(driver, this);     }

    public HomePage verifyLogin(String username,String password) 
    {   email_id_field.sendKeys(username);
        password_field.sendKeys(password);
        login_button_Field.click();
        return new HomePage();
    }
}

登录测试

public class LoginTest extends BaseTest 
{   LoginPage loginpage;
    HomePage homepage;  

    public LoginTest() 
    {   super();    }

    //Browser initialization/Environment setup  <----------------------
    @BeforeMethod                               
    public void setup() 
    {   initialization();
        loginpage = new LoginPage();    }

    //login with correct credentials            <----------------------
    @Test(priority=0)
    public void loginWithCorrectCredentials() 
    {       homepage=loginpage.verifyLogin(prop.getProperty("login_id"),prop.getProperty("login_password")); 
    }
    
    //Browser closure                           <----------------------
    @AfterMethod
    public void tearDown()
    {   driver.quit();      }
}

我也需要测试主页,但自从获得后就卡住了

[RemoteTestNG] detected TestNG version 7.0.1
Starting ChromeDriver 86.0.4240.22 (398b0743353ff36fb1b82468f63a3a93b4e2e89e-refs/branch-heads/4240@{#378}) on port 8944
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Oct 28, 2020 3:55:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
FAILED CONFIGURATION: @BeforeMethod setup
java.lang.ExceptionInInitializerError
    at com.qa.testpages.LoginPage.verifyLogin(LoginPage.java:115)
    at com.qa.testcase.HomeTest.setup(HomeTest.java:35)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302)
    at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at org.testng.TestRunner.privateRun(TestRunner.java:766)
    at org.testng.TestRunner.run(TestRunner.java:587)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.NullPointerException
    at com.qa.testpages.HomePage.<clinit>(HomePage.java:32)
    ... 34 more

SKIPPED CONFIGURATION: @AfterMethod tearDown

谁能帮忙。提前致谢!

【问题讨论】:

    标签: java selenium exception pom.xml ui-automation


    【解决方案1】:

    关门了。问题在于 HomePage.java 类,它具有 HomeTest.java 类的 xpath 和方法。 HomePage.java 类的静态 WebElements 具有不正确的 xpaths。现在,更正了 xpath 并发现它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多