Java 我们可以在 selenium web 驱动程序中使用静态变量来初始化驱动程序吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23735885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we have static variable for initilizing driver in selenium web driver
提问by Deva
We have a static variable to initialize driver in Selenium WebDriver.
我们有一个静态变量来初始化 Selenium WebDriver 中的驱动程序。
public static WebDriver driver;
public static WebDriver driver;
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
该变量在类(DriverInit)中声明,并在各种测试计划(Test Classes)的“@BeforeClass”中初始化。初始化变量(驱动程序)将在项目中的各种可重用函数和@test 方法中使用。驱动程序将在@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?
这适用于非并行执行脚本。使用静态变量来初始化驱动程序是否会以任何方式影响并行执行?
Eg :
例如:
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(..);
}
}
回答by QArea
It's not correct to use the static for driver instance. It's better to create get_method to return the driver to another class.
将静态用于驱动程序实例是不正确的。最好创建 get_method 将驱动程序返回到另一个类。
public class DriverInit { (using remote webdriver, browser name will be read from XML)
private WebDriver driver;
public WebDriver getDriver() {
switch (browser) {
case "IE" : driver = ....
case "Firefox" : driver = ....
return driver;
}
}
public class TestClass {
private Webdriver driver;
@BeforeClass
public void BeforeClass() {
DriverInit driveInit= new DriverInit();
driver=driveInit.getDriver();
}
@Test
public void Test1() {
Reusable reusable = new Reusable();
reusable.func1Click(driver);
}
}
public class Reusable {
public void func1Click(WebDriver driver) {
driver.findElementBy(..).click();
}
}
回答by olyv
When you define your field as static
then every thread has access to it and every thread can modify that memory space. That is is why I have doubts about possibility to use webdriver instance defined as static
. But you can consider using Selenium Gridin order to run multiple tests across different browsers.
当您将字段定义为时,static
每个线程都可以访问它,并且每个线程都可以修改该内存空间。这就是为什么我怀疑使用定义为static
. 但是您可以考虑使用Selenium Grid来跨不同浏览器运行多个测试。
回答by Nirmal Keshvani
Use Singleton class in which you define your webDriver, then use the same instance in your other classes
使用定义 webDriver 的 Singleton 类,然后在其他类中使用相同的实例
回答by robert arles
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.
正如您所说,您可以使用静态驱动程序,但是,是的,如果您直接在测试代码级别进行任何并行化,则会面临共享内存问题的风险。
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.
正是因为这个原因,我避免使用静态驱动程序,对于可能发展为并行化的测试套件来说,这是一个糟糕的设计。在 selenium 网格中的多台服务器上运行单个进程启动测试是行不通的。
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.
我围绕这种情况设计的方法是在初始化它们时将驱动程序(在@Before 中创建)传递给每个页面对象。
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)
如果套件不会增长,您仍然可以使用静态驱动程序,并将多个测试作为单独的作业启动(即单独的 Jenkins 作业,或从命令行单独运行等)