0

I'm using selenium to test a single page app. I'm loading the page in a @beforeClass method. Then running all the tests in the class. Then tearing down the driver in the @afterClass method.

I've annotated my constructor as a @factory method using a dataprovider from a base class. The dataprovider is used to setup a collection of different browsers.

I'm using crossbrowsertesting.com to run the tests, and I'm struggling to control the parallelization. I need to be able to control the maximum number of concurrent threads running at any one time (our current plan only allows for 2 simultaneous tests). And I want to ensure that parallelisation occurs at the dataprovider level. (i.e. I want a thread for each browser up to a maximum of 2 threads).

I'm using a testng.xml file.

<suite name="functional_tests" parallel="classes" thread-count="2">
    <test name="com.mapov.functional_tests suite">
        <classes>
           <class name="com.mapov.functional_tests.KiteTests" />
        </classes>

    </test>
</suite>

With a base class -

public class BaseTest {

    public BaseTest (String browser, String platform, String resolution, String browserDescription) {  
        userName = "myUsername";
        accessKey = "myKey"; 
        pageUrl = "http://test.mapov.com/hotels/benidorm";
        this.browserDescription = browserDescription;

        caps = new DesiredCapabilities();
        caps.setCapability("browser_api_name", browser);
        caps.setCapability("os_api_name", platform);
        caps.setCapability("screen_resolution", resolution);
        caps.setCapability("name", this.browserDescription);
        caps.setCapability("record_video", "false");
        caps.setCapability("record_network", "false");
        caps.setCapability("record_snapshot", "false");      
    }

    @DataProvider(parallel=true)
    public static Object[][] getBrowsers() {
        return new Object[][]{
            new Object[]{"MblSafari8.0", "iPadAir-iOS8Sim", "1024x768", "Ipad air, with safari 8"}, 
            new Object[]{"IE11", "Win10", "1280x1024", "IE11 on windows 10"},
            new Object[]{"Safari9", "Mac10.11", "1280x1024", "Safari 9 on Mac osx10.11"}, 
        };
    }


    @BeforeClass
    public void SetupDriver() {
        long id = Thread.currentThread().getId();
        try {
            URL remoteDriverURL = new URL("http://" + userName + ":" + accessKey + "@hub.crossbrowsertesting.com:80/wd/hub");
            driver = new RemoteWebDriver(remoteDriverURL, caps);
            driver.get(pageUrl);
            WebElement pager = (new WebDriverWait(driver, 30)).until(ExpectedConditions.visibilityOfElementLocated(By.id("pager")));
        } catch (MalformedURLException ex) {
            System.out.println(ex.getMessage());
        }
    }

    @AfterClass
    public void disposeOfDriver() {
        // quit the driver
    }
}

And then an inheriting class with tests.

public class KiteTests extends BaseTest {

    @Factory(dataProvider = "getBrowsers")
    public KiteTests(String browser, String platform, String resolution, String description) {
        super(browser, platform, resolution, "Kite tests on " + description);
    }   

    @Test 
    public void testKitesAppear() {
        // test.
    }

    @Test
    public void testHotelNamePresent() {
       // test
    }

}

If I remove the parallel="classes" thread-count="2" attributes from the suite node of the xml, then everything runs fine (just not in parallel).

If I keep the parameters then I get the following error -

    FAILED CONFIGURATION: @BeforeClass SetupDriver
org.openqa.selenium.WebDriverException: This account has reached it maxiumum number of concurrent selenium tests - please wait for a test to finish before starting a new test.
Command duration or timeout: 668 milliseconds
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'Ewen-Macbook.lan', ip: '192.168.1.65', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.11', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:247)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:129)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:156)
    at com.mapov.functional_tests.BaseTest.SetupDriver(BaseTest.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:175)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:107)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
  • Could you provide the stacktrace if available? The code of constructor and the code of beforeclass method may help too. – juherr Nov 04 '15 at 09:48

2 Answers2

1

I think using the parameters concept of TestNG is a better method to get the desired results.

TestNG.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="functional_tests Suite" parallel="tests"
thread-count="2">
<test name="functional_tests suite - Ipad air, with safari 8">
    <parameter name="browser" value="MblSafari8.0" />
    <parameter name="os" value="iPadAir-iOS8Sim" />
    <parameter name="screen" value="1024x768" />
    <parameter name="desc" value="Ipad air, with safari 8" />
    <classes>
        <class name="com.mapov.functional_tests.KiteTests" />
    </classes>
</test>

<test name="functional_tests suite - IE11 on windows 10">
    <parameter name="browser" value="IE11" />
    <parameter name="os" value="Win10" />
    <parameter name="screen" value="1024x768" />
    <parameter name="desc" value="IE11 on windows 10" />
    <classes>
        <class name="com.mapov.functional_tests.KiteTests" />
    </classes>
</test>

<test name="functional_tests suite - Safari 9 on Mac osx10.11">
    <parameter name="browser" value="Safari9" />
    <parameter name="os" value="Mac10.11" />
    <parameter name="screen" value="1024x768" />
    <parameter name="desc" value="Safari 9 on Mac osx10.11" />
    <classes>
        <class name="com.mapov.functional_tests.KiteTests" />
    </classes>
</test>

BaseClass

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

public class BaseTest {

    @Parameters({ "browser", "os", "screen", "desc" })
    @BeforeClass
    public void SetupDriver(String browser, String os, String screen, String desc) {
        // connect to the remote server and load the page.
        System.out.println(browser + os + screen + desc);
    }

    @AfterClass
    public void disposeOfDriver() {
        // quit the driver
    }
}

Test Class

import org.testng.annotations.Test;

public class KiteTests {

    @Test
    public void testKitesAppear() {
        // test.
    }

    @Test
    public void testHotelNamePresent() {
        // test
    }
}

This structure gives control to configure the test flow from the XML file without changing the JAVA code.

Hope this helps you!!

StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
0

Try using: data-provider-thread-count="2" instead of thread-count="2"

tomerpacific
  • 4,704
  • 13
  • 34
  • 52