全局变量在第一次测试后设置为 null 执行 Testng 线程本地 WebDriver 对象
Global variable set to null after first test Execution Testng Thread Local WebDriver Object
大家好我有两个像下面这样的class我可以运行顺序没有任何问题但是如果我运行这个样本两个测试用例在第二种测试方法中并行对象设置为 null.for 由于某些原因我使用了线程局部变量请在 below.In 浏览器设置中找到我的代码 class 我正在创建驱动程序实例
public class SeleniumMethodsClass {
private static ThreadLocal<RemoteWebDriver> driver = new ThreadLocal<RemoteWebDriver>();
private static ThreadLocal<ExtentTest> logger = new ThreadLocal<ExtentTest>();
private static ThreadLocal<ExtentReports> report= new ThreadLocal<ExtentReports>();
private static int time=20;
public static RemoteWebDriver getDriver() {
return driver.get();
}
public static void setDriver(RemoteWebDriver setdriver) {
driver.set(setdriver);
}
public static ExtentTest getLogger() {
return logger.get();
}
public static void setlogger(ExtentTest setlogger) {
logger.set(setlogger);
}
public static ExtentReports getreport() {
return report.get();
}
public static void setreport(ExtentReports setlogger) {
report.set(setlogger);
}
public static void initilization(String testCaseDescription) throws Exception{
RemoteWebDriver driver=BrowserSetup.createdriverInstance();
String url=JsonReader.readJson("envconfig","ApplicationUrl");
SeleniumMethods.setDriver(driver);
driver.get(url);
driver.manage().window().maximize();
ExtentReports report=ReportTemplate.instance();
SeleniumMethods.setreport(report);
ExtentTest logger=ReportTemplate.loggerinstance(testCaseDescription);
SeleniumMethods.setlogger(logger);
}
}
ClassOne
和 ClassTwo
如下所示:
public class ClassOne {
@BeforeClass
public void setUp() throws Exception{
SeleniumMethods.initilization("Producer_CRUDIndividualNo");
}
@Test
public void create_producer() {
try{
//Some test code
} catch(Exception e){
//Exception handling
}
}
@Test
public void create_producer2() {
try{
//Some test code
} catch(Exception e){
//Exception handling
}
}
@AfterClass
public void tear_down(){
ReportTemplate.close_intialization();
}
}
TestNG 套件 xml 文件如下所示:
<suite name="ProducerPro_Automation" verbose="2" parallel="classes" thread-count="5">
<test name="Producer HealthCheck1" verbose="2">
<classes>
<class name="class1"/>
<class name="class2"/>
</classes>
</test>
</suite>
你能帮我吗this.I有很多基于这个框架的测试脚本,我不想在脚本中修改level.Please建议我。
问题可能是由于您正在从 @BeforeClass
注释方法中初始化 ThreadLocal
变量。 TestNG 不保证您所有的 @Test
方法和 @BeforeClass
方法都将 运行 在同一个线程中。
由于您在不同线程中初始化 ThreadLocal
变量并尝试在不同线程中访问它们,因此您可能会看到 null
值。
当您 运行 顺序执行时不会发生这种情况,因为没有并发执行。
要解决此问题,您有两种选择:
- 用
@BeforeMethod
注释您的 setUp()
方法,而不是使用 @BeforeClass
- 将您的
SeleniumMethods.initilization("Producer_CRUDIndividualNo");
调用移动到实现 IInvokedMethodListener
的侦听器中,在 beforeInvocation()
中。这在技术上等同于在 @BeforeMethod
方法中执行此操作,但您不必在每个 class 中都去更改它,并且它有助于保持您的 class 清洁。
您可以在 this blog post of mine, and also refer to this 博文中阅读更多关于一般监听器的信息,了解如何使用监听器的概念来实例化和清理网络驱动程序。
大家好我有两个像下面这样的class我可以运行顺序没有任何问题但是如果我运行这个样本两个测试用例在第二种测试方法中并行对象设置为 null.for 由于某些原因我使用了线程局部变量请在 below.In 浏览器设置中找到我的代码 class 我正在创建驱动程序实例
public class SeleniumMethodsClass {
private static ThreadLocal<RemoteWebDriver> driver = new ThreadLocal<RemoteWebDriver>();
private static ThreadLocal<ExtentTest> logger = new ThreadLocal<ExtentTest>();
private static ThreadLocal<ExtentReports> report= new ThreadLocal<ExtentReports>();
private static int time=20;
public static RemoteWebDriver getDriver() {
return driver.get();
}
public static void setDriver(RemoteWebDriver setdriver) {
driver.set(setdriver);
}
public static ExtentTest getLogger() {
return logger.get();
}
public static void setlogger(ExtentTest setlogger) {
logger.set(setlogger);
}
public static ExtentReports getreport() {
return report.get();
}
public static void setreport(ExtentReports setlogger) {
report.set(setlogger);
}
public static void initilization(String testCaseDescription) throws Exception{
RemoteWebDriver driver=BrowserSetup.createdriverInstance();
String url=JsonReader.readJson("envconfig","ApplicationUrl");
SeleniumMethods.setDriver(driver);
driver.get(url);
driver.manage().window().maximize();
ExtentReports report=ReportTemplate.instance();
SeleniumMethods.setreport(report);
ExtentTest logger=ReportTemplate.loggerinstance(testCaseDescription);
SeleniumMethods.setlogger(logger);
}
}
ClassOne
和 ClassTwo
如下所示:
public class ClassOne {
@BeforeClass
public void setUp() throws Exception{
SeleniumMethods.initilization("Producer_CRUDIndividualNo");
}
@Test
public void create_producer() {
try{
//Some test code
} catch(Exception e){
//Exception handling
}
}
@Test
public void create_producer2() {
try{
//Some test code
} catch(Exception e){
//Exception handling
}
}
@AfterClass
public void tear_down(){
ReportTemplate.close_intialization();
}
}
TestNG 套件 xml 文件如下所示:
<suite name="ProducerPro_Automation" verbose="2" parallel="classes" thread-count="5">
<test name="Producer HealthCheck1" verbose="2">
<classes>
<class name="class1"/>
<class name="class2"/>
</classes>
</test>
</suite>
你能帮我吗this.I有很多基于这个框架的测试脚本,我不想在脚本中修改level.Please建议我。
问题可能是由于您正在从 @BeforeClass
注释方法中初始化 ThreadLocal
变量。 TestNG 不保证您所有的 @Test
方法和 @BeforeClass
方法都将 运行 在同一个线程中。
由于您在不同线程中初始化 ThreadLocal
变量并尝试在不同线程中访问它们,因此您可能会看到 null
值。
当您 运行 顺序执行时不会发生这种情况,因为没有并发执行。
要解决此问题,您有两种选择:
- 用
@BeforeMethod
注释您的setUp()
方法,而不是使用@BeforeClass
- 将您的
SeleniumMethods.initilization("Producer_CRUDIndividualNo");
调用移动到实现IInvokedMethodListener
的侦听器中,在beforeInvocation()
中。这在技术上等同于在@BeforeMethod
方法中执行此操作,但您不必在每个 class 中都去更改它,并且它有助于保持您的 class 清洁。
您可以在 this blog post of mine, and also refer to this 博文中阅读更多关于一般监听器的信息,了解如何使用监听器的概念来实例化和清理网络驱动程序。