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控件元素E.g:
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是在巨大的多线程环境中运行测试的最佳解决方案.
在多线程环境中使用WebDriver的代码段:
public static ThreadLocal<WebDriver> driver;
driver=new ThreadLocal<WebDriver>()
{
@Override
protected WebDriver initialValue()
{
return new FirefoxDriver(); //You can use other driver based on your requirement.
}
};
现在,每次创建测试线程时,都会打开一个新的浏览器. ThreadLocal将确保每个线程只有一个静态webdriver实例的副本. [注意:确保您的其他全局变量太过ThreadLocals.在我的情况下,他们不是那就是为什么我遇到测试混乱的问题].我想分享一些额外的知识,以便其他人可以发现它提供信息.在ThreadLocal中,每当调用ThreadLocal.get()方法时,您必须确保有一个初始化本地线程的规定,如上所示在initialValue()方法中,或者您可能遇到空指针异常.感谢大家.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。