我们可以在 selenium web 驱动程序中使用静态变量来初始化驱动程序吗 [英] Can we have static variable for initilizing driver in selenium web driver

查看:25
本文介绍了我们可以在 selenium web 驱动程序中使用静态变量来初始化驱动程序吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个静态变量来初始化 Selenium WebDriver 中的驱动程序.

We have a static variable to initialize driver in Selenium WebDriver.

公共静态WebDriver驱动;

这个变量在一个类(DriverInit)中声明,并在各种测试计划(Test Classes)的@BeforeClass"中初始化.初始化变量(驱动程序)将在项目中的各种可重用函数和@test 方法中使用.驱动程序将在@QAfterClass 处关闭/退出

This variable is declared in a class (DriverInit) and initialized in "@BeforeClass" of various Test Plans(Test Classes). The initialized variable (driver) will be used across the project in various re-usable functions and @test methods. Driver will be closed/quit at the @QAfterClass

这适用于非并行执行脚本.使用静态变量来初始化驱动程序是否会以任何方式影响并行执行?

This works fine with non-paralled execution of scripts. Does having a static variable to initialize the driver affect parallel execution by any means?

例如:

public class DriverInit { (using remote webdriver, browser name will be read from XML)
  public static WebDriver driver;    
  public DriverInit() {
    switch (browser) {
    case "IE" : driver = ....
    case "Firefox" : driver = ....
    }
  }
}

public class TestClass {
  @BeforeClass
  public void BeforeClass() {
   DriverInit driver = new DriverInit();
  }

  @Test
  public void Test1() {
   DriverInit.driver.findElementBy();
   Reusable.func1();
  }
}

Public class Reusable {

  public WebElement r1;
  public Reusable() {
    r1 = DriverInit.driver.findElementBy(..);
  }
  public void func1() {
     r1.findElementBy(..);
  }
}

推荐答案

正如您所说,您可以使用静态驱动程序,但是,是的,如果您直接在测试代码级别进行任何并行化,则会面临共享内存问题的风险.

As you stated, you can use a static driver, but, yes, you are risking a shared memory issue if you do any parallelization directly at the test code level.

正是因为这个原因,我避免使用静态驱动程序,对于可能发展为并行化的测试套件来说,这是一个糟糕的设计.在 selenium 网格中的多台服务器上运行单个进程启动测试是行不通的.

I've avoided using a static driver for just this reason, it's bad design for a test suite that may grow into parallelization. Running a single process launching tests on multiple servers in a selenium grid would not work.

我围绕这种情况设计的方法是在初始化每个页面对象时将驱动程序(在@Before 中创建)传递给它们.

The way I've designed around this situation is to pass the driver (created in the @Before) to each page object when I initialize them.

如果套件不会增长,您仍然可以使用静态驱动程序,并将多个测试作为单独的作业启动(即单独的 Jenkins 作业,或从命令行单独运行等)

If the suite WILL NOT grow, you can still use the static driver, and launch multiple tests as separate jobs (i.e. separate Jenkins jobs, or separate runs from the command line, etc)

这篇关于我们可以在 selenium web 驱动程序中使用静态变量来初始化驱动程序吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆