1
votes

I've searched and read almost every article about Selenium Grid and running tests in parallel with testNG. However, I still can't figure about what I'm doing wrong.

Using Page Object Pattern I'm testing http://www.gmail.com. I'm using Google Guice dependency injection in order to provide every page with WebDriver, for that I used @Provides annotation in AbstractModule class:

public class GmailModule extends AbstractModule {
@Override
protected void configure() { ... }
String NODE_URL =  "http://localhost:5555/wd/hub";


@Provides
WebDriver getRemoteDriver() throws MalformedURLException {
    ThreadLocal<RemoteWebDriver> threadDriver = new ThreadLocal<RemoteWebDriver>();
    DesiredCapabilities capability = new DesiredCapabilities();
    capability.setBrowserName(DesiredCapabilities.firefox().getBrowserName());
    capability.setCapability(FirefoxDriver.PROFILE, new File(ResourceExaminer.getValueFromExpDataMap("firefoxprofile")));
    capability.setPlatform(Platform.XP);
    RemoteWebDriver webDriver = new RemoteWebDriver(new URL(NODE_URL), capability);
    threadDriver.set(webDriver);
    return threadDriver.get();
}
}

All pages extends Abstract page, where I'm passing the driver and then just use it.

public abstract class AbstractPage extends HTMLElements{
public WebDriver webDriver;
@Inject
public AbstractPage(WebDriver driver){
    this.webDriver = driver;
    PageFactory.initElements(new HtmlElementDecorator(webDriver), this);
}
}

Then all tests extends AbstractTestingClass, which is provided with Google @Guice annotation in order to inject the first page. I use the following cmd lines to run the hub and node with Selenium Grid:

java -jar selenium-server-standalone-2.39.0.jar -role hub -hubConfig DefaultHub.json java -jar selenium-server-standalone-2.39.0.jar -role node -nodeConfig DefaultNode.json

In DefaultNode.json I cut down the number of maxSessions=2 and browserName=firefox

My test suite contains the following

    <suite name="PositiveTestSuite" parallel="classes" thread-count="2" verbose="2">
    <test name="Attaching file">
        <classes>
            <class name="com.epam.seleniumtask.gmail.test.AttachingFilesTest"/>
        </classes>
    </test>
    <test name="2nd">
        <classes>
            <class name="com.epam.seleniumtask.gmail.test.ChangeSignatureTest"/>
        </classes>
    </test>
    <test name="3rd">
        <classes>
            <class name="com.epam.seleniumtask.gmail.test.CreateNewLabelTest"/>
        </classes>
    </test>
<test name="4th">
    <classes>
        <class name="com.epam.seleniumtask.gmail.test.DeletingMessagesTest"/>
    </classes>
     </test>

However, tests are running very strange -->

  • 4 browsers are opened, not 2.
  • 2 tests ARE running in parallel, but the other two are waiting in the other browsers.

My questions are - 1) Why I still have 3 browsers even if I restrict the number of them in Selenium Grid? 2) How can I force my tests to run in only 2 browsers? 2 tests in one, 2 in another?

Please, help me. I will appreciate any answer.

1
To your first question :When you see your grid console, do you see your max sessions set reflected? Can you elaborate on your second question - 2 tests in one browse and 2 in another. Do you have dependent tests? In the above scenario, this is not gauranteed. Whatever browser gets free first gets the next req.niharika_neo
Can you publish a working sample of your project at a GitHub url?djangofan

1 Answers

1
votes

Thanks for the answers. I finally got the idea why I was having those problems. In case someone needs the answer --> I have been injecting module for Google Guice in every test class. So injection has been initialized every time the test started, and as a result new browser has been opened as well.

As a solution I'm using lazy initialization in Guice using @Provides injection. And in every @BeforeClass annotation I'm getting the provider. Something like that:

@Guice(modules = GmailModule.class)
public class ForwardTest extends AbstractTestingClass {

@Inject
Provider<SignInPage> providingSignInPage;

@BeforeClass
public void startUp(){
     signInPage = providingSignInPage.get();
}

Inside the singInPage there is a RemoteWebDriver. Now it works fine to me.