Here's problem. I have some UI tests for one website. I use NavigationService to do some navigation on the page, such as open some page etc.
Here's NavigationModule
public class NavigationModule extends AbstractModule {
@Override
protected void configure() {
bind(NavigationService.class).in(Singleton.class);
}
}
Here's NavigationService
public class NavigationService {
@Inject
private LoginPage loginPage;
@Inject
private TemplateManagementPage templateManagementPage;
@Inject
private CreateTemplatePage createTemplatePage;
@Inject
private NavigationMenu navigationMenu;
public TemplateManagementPage logIn(String username, String password) {
return loginPage.login(username, password);
}
public TemplateManagementPage openTemplateManagementPage() {
navigationMenu.openMenu()
return templateManagementPage;
}}
And all above I use it in BaseTest class with @Guice annotation, here's code
@Guice(modules = NavigationModule.class)
public abstract class BaseTest extends BDDTest {
protected String login = "login";
protected String password = "pass";
@Inject
protected NavigationService navigationService;
protected TemplateManagementPage templateManagementPage;
@BeforeSuite
public void login() {
templateManagementPage = navigationService.logIn(login, password);
}}
The last thing is I call this method in @BeforeMethod
public class TemplateWithLevelTest extends BaseTest {
private CreateTemplatePage templatePage;
@BeforeMethod(description = "Open create template page")
public void openCreateTemplatePage() {
templatePage = navigationService.openCreateTemplatePage();
}
@Test()
public void someTest() {
...
}
Everything works fine, till I want run it in parallel from TestNg.xml, it opens one browser logging in and then opens another one with no page opened, then it throws
Element not found {By.xpath: //div[@id='toast-container']}
NavigationModule is binded in Singleton, and as I get we should create different singletons for each thread, is there any way to do that?
Ohhh, after few weeks, we finally did it. So here's the solution. Enjoy;)