无法在TestNG中运行并行执行,因为TestNG框架中有静态WebDriver。

huangapple go评论71阅读模式
英文:

Cannot run parallel execution in testng due to static WebDriver in the testng framework

问题

我使用 testng 创建了一个框架,并将 driver 用作静态变量,但我无法在 testng 中运行并行执行,因为我使用了 static 来创建驱动程序。现在我想将驱动程序更改为非静态,但我不确定如何将驱动程序更改为非静态并使用它。以下是我的框架流程。

public TestBase{
    public static WebDriver driver; // 在此类中将WebDriver声明为静态变量
    // 下面有一些方法(点击、输入等),可以在不同的应用程序中通用使用
    // 在这些方法中有一些使用 driver.findElement 的语句,因此去除静态将影响所有方法
}

public LIC_CommonMethods extends TestBase{
    // 有一些特定于 LIC 应用程序的通用方法
    // 在这些方法中有一些使用 driver.findElement 的语句,因此去除静态将影响所有方法
}

public LIC_AutoMethods extends LIC_CommonMethods{
    // 有一些特定于 LIC 的方法
    // 在这些方法中有一些使用 driver.findElement 的语句,因此去除静态将影响所有方法
}

public LIC_AutoExecution extends TestBase {
    @BeforeTest
    public static void initiateBrowser(){
    // 在这里进行浏览器初始化代码
}

@DataProvider
public Iterator<Object[]>  getData(){
    // 从另一个方法获取数据的语句
}

@Test
public static void Execution(){
    LIC_CommonMethods liccomm = new LIC_CommonMethods();
    LIC_AutoExecution licauto = new LIC_AutoExecution();
    liccomm.method1();
    liccomm.method2();
    licauto.method1();
    licauto.method2();
}

@AfterTest
public static void closedriver(){
    driver.quit();
}

我尝试在 TestBaseLIC_CommonMethodsLIC_AutoMethods 中添加构造函数,但不确定如何在不影响其他类(如 TestBaseLIC_CommonMethodsLIC_AutoMethods)中使用的 driver.findElement 语句的情况下将驱动程序值传递给这些类。

英文:

I created a framework using testng and used driver as a static variable, but I'm unable to run parallel execution in testng, as I used static to create the driver. Now I want to change the driver to non static, but am confused on how to change the driver to non static and use it. Below is my framework flow.

public TestBase{
    public static WebDriver driver; // declared the webdriver as static in this class
    // below there are few methods(click, type etc) which can be used generally across different applications
    // There are statements written as driver.findElement in this class in different methods and hence removing static will effect all the methods
}

public LIC_CommonMethods extends TestBase{
    // have some common methods specific to lic application
    // There are statements written as driver.findElement in this class in different methods and hence removing static will effect all the methods
}

public LIC_AutoMethods extends LIC_CommonMethods{
    // have some methods which are specific to lic 
    // There are statements written as driver.findElement in this class in different methods and hence removing static will effect all the methods
}

public LIC_AutoExecution extends TestBase {
    @before Test
    public static void initiateBrowser(){
    // Browser initiation code will be present here
}

@DataProvider
public Iterator&lt;Object[]&gt;  getData(){
    // statement to get the data from another method
}

@Test
public static void Execution(){
    LIC_CommonMethods liccomm = new LIC_CommonMethods();
    LIC_AutoExecution licauto = new licauto();
    liccomm.method1();
    liccomm.method2();
    licauto.method1();
    licauto.method2();
}

@AfterTest
public static void closedriver(){
    driver.quit();
}

I tried adding constructors in TestBase, LIC_CommonMethods, and LIC_AutoMethods, but am not sure how to pass the driver value to these classes without affecting the driver.findElement statement used in the other classes such as TestBase, LIC_CommonMethods, and LIC_AutoMethods.

答案1

得分: 0

我按照博客中提到的步骤 http://makeseleniumeasy.com/2020/05/27/threadlocal-static-webdriver-for-parallel-execution/ 并解决了这个问题。

英文:

I followed the steps mentioned in the blog http://makeseleniumeasy.com/2020/05/27/threadlocal-static-webdriver-for-parallel-execution/ and solved the issue.

huangapple
  • 本文由 发表于 2023年7月14日 00:09:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76681417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定