问题
I have setup the grid as mentioned in this post. Also, I have the test classes setup as in this post.
Since I wanted each driverInstance
to use its own someController
and someData
, I changed the BaseTest
as follows:
public abstract class BaseTest {
String testFolder;
String testName;
protected String envName;
protected Configuration config;
protected String host;
protected RemoteWebDriver driver;
protected String proxy;
protected SomeData someData;
protected SomeController someController;
public BaseTest() {
}
public BaseTest( String testFolder, String testName)
{
this.testFolder = testFolder;
this.testName = testName;
this.envName = System.getProperty("config");
this.proxy = System.getProperty("proxy");
config = this.envName;
}
@BeforeMethod
public void startTest(Method testMethod) {
try {
this.someData = SomeDataSetup.getSafeSomeData(new SomeData());
this.driver = WebDriverSetup.getDriver();
this.someController = SomeController.getSafeSomeController(new someController(this.driver, this.someData));
driver.navigate().to("https://" + this.host);
} catch (MalformedURLException e) {
System.out.println("MalformedURLException");
}
}
@AfterMethod
public void closeWindow() {
driver.close();
driver.quit();
}
}
In order to get each individual instance, I created the following class
public class SomeDataSetup {
public static SomeData getSafeSomeData(SomeData someDataSet ) {
ThreadLocal<SomeData> someData = null;
someData = new ThreadLocal<SomeData>();
someData.set(someDataSet);
return someData.get();
}
}
Similar thing for the someController
. The following tests will be using its own instance of someData
, someController
and driver
as below in TestClass
.
public class Tests extends BaseTest {
//This is used to do some setup in the super class
@Parameters({"testName", "env" })
public Tests( @Optional String testName, @Optional String env ) {
super( null, testName, null, env );
}
@BeforeMethod
public void setup() throws Exception {
try {
SomeData someData;
SomeController someController;
RemoteWebDriver driver;
this.someData = SomeDataSetup.getSafeSomeData(new SomeData());
this.driver = super.driver;
this.someController = SomeControllerSetup
.getSomeController(new SomeController(
this.driver, this.someData));
this.driver.navigate().to("https://" + this.host);
}
public void test1() throws Exception {
use driver from super
use someData from super
use someController is using the driver from super
}
public void test2() throws Exception {
use driver from super
use someData from super
use someController is using the driver from super
}
Is it a good way to get a threadsafe instances of each someData, someController or driver, or there is some that can be done in the BaseTest itself to return the threadlocal instance of each of these?
来源:https://stackoverflow.com/questions/27531619/selenium-grid-driversafe-tests-distribution