GlobalVariables クラスは、フレームワーク全体で使用されるさまざまな変数を保持します。そのうちの 1 つは 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();
}
//他のグローバル変数を初期化する他のメソッド。}
ドライバ インスタンスを使用して UI コントロール要素を取得する多くの GetElement クラスがあります。
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)同期の使用に出くわしました。2)ThreadLocalインスタンスを作成します(注:この解決策を試しましたが、まだ同じ問題です。テストが互いに混ざり合って失敗しています。WebDriver インスタンスを ThreadLocal としてマークし、ThreadLocal の initialValue メソッドをオーバーライドしてドライバー インスタンスを初期化しました。それでも、正しく実装したかどうかはわかりません。) 今、特定のシナリオでこのソリューションのいずれかを実装する最善の方法がわかりません。どんな助けでも大歓迎です。ティア!