通过TestNG硒自动化打开另一个页面时,java.lang.ExceptionInInitializerError

如何解决通过TestNG硒自动化打开另一个页面时,java.lang.ExceptionInInitializerError

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

PS。如果将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"));
    }
}

LoginPage

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

LoginTest

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

任何人都可以帮忙。预先感谢!

解决方法

已关闭。问题是HomePage.java类,该类具有HomeTest.java类的xpaths和方法。 HomePage.java类具有静态WebElements,该WebElements具有错误的xpath。现在,更正了xpaths并发现它工作正常。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res