java中的静态Webdriver实例同步

GlobalVariables类包含在我的框架中使用的不同变量,其中一个是WebDriver实例:

public class GlobalVariables { public static WebDriver driver; //Some other static global variables required across my framework public GlobalVariables(String propertiesFile) { initializeVariables(propertiesFile); } public void initializeVariables(String propertiesFile) { GlobalInitializer obj=new GlobalInitializer(); obj.initialize(String propertiesFile); } } 

GlobalInitializer包含初始化所有GlobalVariables的方法:

 public class GlobalInitializer extends GlobalVariables { public void initialize(String propertiesFile) { //Some logic to read properties file and based on the properties set in it, call other initialization methods to set the global variables. } public void initializeDriverInstance(String Browser) { driver=new FireFoxDriver(); } 

//初始化其他全局变量的其他一些方法。 }

我有许多GetElement类,它们使用驱动程序实例来获取UI控件元素,例如:

 public class GetLabelElement extends GlobaleVariables { public static WebElement getLabel(String someID) { return driver.findElement(By.id(someId)); } //Similar methods to get other types of label elements. } public class GetTextBoxElement extends GlobaleVariables { public static WebElement getTextBox(String someXpath) { return driver.findElement(By.xpath(someXpath)); } //Similar methods to get other types of text box elements. } 

我有其他类在UI控件上执行一些操作(这个类也使用全局变量)例如:

 public class GetLabelProperties extends GlobalVariables { public static String getLabelText(WebElement element) { return element.getText(); } } public class PerformAction extends GlobalVariables { public static void setText(String textBoxName,String someText) { driver.findElement(someLocator(textBoxName)).setText("someText"); } //Some other methods which may or may not use the global variables to perform some action } 

我在testng中的测试类看起来像这样:

 public class TestClass { GlobalVariables globalObj=new GlobalVariables(String propertiesFile); @Test(priority=0) { GlobalVariables.driver.get(someURL); //Some assertion. } @Test(priority=1) { WebElement element=GetLabelElement.getLabel(someID); String labelName=GetLabelProperties.getLabelText(element); //Some assertion. } @Test(priority=2) { WebElement element=GetTextBoxElement.getTextBox(someXpath); PerformAction.setText(element.getText(),someText); //Some assertion. } } 

我有基于场景的类似的多个测试类。 现在这个测试运行正常,如果我单独运行它们。 但是当我尝试并行运行它们时,这些测试以某种奇怪的方式失败了。 在分析时,我发现它的静态全局变量正在被每个测试初始化​​,从而使其他测试失败。 现在我应该如何实现我的目标,即在框架设计中进行最小的更改,并行运行多个测试? 我试过搜索选项,我遇到了一些选项,即1)使用synchronized。 2)创建ThreadLocal实例(注意:我已尝试过这个解决方案,但仍然存在同样的问题。测试相互混淆导致失败。我将WebDriver实例标记为ThreadLocal并覆盖ThreadLocal的initialValue方法以初始化驱动程序实例。我仍然不确定我是否正确实施了它。) 现在我不确定在给定方案中如何最好地实现这个解决方案中的任何一个。 任何帮助表示赞赏。 TIA!

我找到了解决方案:使用ThreadLocal是在巨大的multithreading环境中运行测试的最佳解决方案。 在multithreading环境中使用WebDriver的代码段:

 public static ThreadLocal driver; driver=new ThreadLocal() { @Override protected WebDriver initialValue() { return new FirefoxDriver(); //You can use other driver based on your requirement. } }; 

现在,每次创建测试线程时,都会打开一个新的浏览器。 ThreadLocal将确保每个线程只有一个静态webdriver实例的副本。 [注意:确保您的其他全局变量太过ThreadLocals。 在我的情况下,他们不是那就是为什么我遇到测试搞砸问题]。 我想分享一些额外的知识,以便其他人可以发现它提供信息。 在ThreadLocal中,每当调用ThreadLocal.get()方法时,您必须确保有一个初始化本地线程的规定,如上所示在initialValue()方法中,或者您可能遇到空指针exception。 感谢大家。

如果您要运行非并行,那么使用静态webdriver成员(或通过引用传递测试类之间共享的实例)是很好的,因为这是一种不必在测试类之间关闭webdriver实例的好方法。 如果你想并行运行,你需要为EACH线程提供一个webdriver实例,因此在这种情况下使用静态成员是错误的方法。 相反,您需要在调用测试用例类时创建或传递webdriver实例。

此外,您正在测试每个测试步骤的单独测试。 这是非常不寻常的 ,我没有看到你这样做的原因。 您可以通过将所有测试步骤保持在人们通常所做的一个单一测试案例中来真正简化测试。

你得到这个是因为JVM如何处理静态成员和方法。

如果要并行运行,则不能拥有静态webdriver对象。

资料来源:我在我工作的地方实施的自动回归系统 – 我们遇到了这个问题。

你可以尝试这样的事情

公共类DriverManager {

 private static final ThreadLocal threadLocal = new ThreadLocal(); public static WebDriver getDriver() { return threadLocal.get(); } public static void setDriver(WebDriver driver) { threadLocal.set(driver); } public static void closeDriver() { if (getDriver() != null) { try { getDriver().close(); } catch (Exception e) { e.printStackTrace(); } try { getDriver().quit(); } catch (Exception e) { e.printStackTrace(); } } threadLocal.remove(); } 

}