变量在Java中显示空值
我有3门Java课程- 1) TestSetup(Baseclass)–我在这里定义了两个变量和一个方法:变量在Java中显示空值,java,selenium,automation,cucumber,Java,Selenium,Automation,Cucumber,我有3门Java课程- 1) TestSetup(Baseclass)–我在这里定义了两个变量和一个方法: 公共静态Webdriver; 公共字符串executionbrowsername; /** *调用BeforeTest方法(TestRunner.java)的函数 */ 测试前公共无效(ITestContext ctx,字符串浏览器){ executionbrowsername=浏览器; } TestRunner类–该类扩展TestSetup类。 在这里,我使用下面的逻辑来获取浏览器变量
公共静态Webdriver;
公共字符串executionbrowsername;
/**
*调用BeforeTest方法(TestRunner.java)的函数
*/
测试前公共无效(ITestContext ctx,字符串浏览器){
executionbrowsername=浏览器;
}
@参数(“浏览器”)
@试验前
public void beforetest(ITestContext ctx,@可选(“firefox”)字符串浏览器){
bc.测试前(ctx,浏览器);
System.out.println(“测试前执行浏览器-”+bc.executionbrowsername);
}
@之前
挂钩前的公共无效(场景){
System.out.println(“内部Beforehooks----”+super.executionbrowsername);
super.before方法(场景);
}
问题–此处executionbrowsername的变量值变为null
在这里,我不能将变量用作静态变量。它为空,因为您从未将它设置为同一对象中的任何其他值。只需使用
super.beforetest
而不是bc.beforetest
。非静态变量仅在同一对象中共享。您好,谢谢您的回复。在这里的testrunner类2中,我能够使用bc.executionbrowsername打印变量中的值。但在类3,即钩子类中,我不能打印变量的值,正如我所说的,非静态变量只在同一个对象中共享。