Parallel execution of test throws the following exception when parallel=true on DataProvider. Two browser instances will open running on separate threads but only one browser instance will execute successfully and the other one gets stuck on trying to find element and throwing an exception. Please, how do I get the test to execute successfully when parallel=true in dataprovider. I'm I missing something maybe the WebDriver instance in the BasePage. Please help. Thanks.
[PoolService-1] 15:44:06,447 INFO [Default test] Close driver PASSED: loginTest("[email protected]", "123456") FAILED: loginTest("[email protected]", "123456") WARNING: WebDriverException thrown by findElement(By.xpath: //button[contains(text(), 'NO THANKS')]) org.openqa.selenium.WebDriverException: org.apache.http.conn.HttpHostConnectException: Connect to localhost:8478 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect When parallel=false on dataprovider, the test will open one browser instance at a time as the data is fed into the test and executes successfully.
PASSED: loginTest("[email protected]", "123456") PASSED: loginTest("[email protected]", "123456") Here's the code I wrote:
1) BrowserFactory
public class BrowserDriverFactory { private ThreadLocal<WebDriver> driver = new ThreadLocal<WebDriver>(); private String browser; private Logger log; public BrowserDriverFactory(String browser, Logger log) { this.browser = browser.toLowerCase(); this.log = log; } public WebDriver createDriver() { // Create driver log.info("Create driver: " + browser); if(browser.equals("chrome")) { System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe"); driver.set(new ChromeDriver()); } else if(browser.equals("firefox")) { System.setProperty("webdriver.gecko.driver", "src/main/resources/geckodriver.exe"); driver.set(new FirefoxDriver()); } else { System.out.println("Do not know how to start: " + browser + ", starting chrome."); System.setProperty("webdriver.chrome.driver", "src/main/resources/chromedriver.exe"); driver.set(new ChromeDriver()); } return driver.get(); } } 2)BaseTest
public class BaseTest { protected WebDriver driver; public WebDriverWait wait; protected Logger log; protected FirefoxProfile profile; protected String url = "http://www.qaclickacademy.com/"; protected String testSuiteName; protected String testName; protected String testMethodName; @BeforeMethod(alwaysRun = false) public void setUp(Method method, ITestContext ctx) { String testName = ctx.getCurrentXmlTest().getName(); log = LogManager.getLogger(testName); BrowserDriverFactory factory = new BrowserDriverFactory(config.getBrowser(), log); driver = factory.createDriver(); driver.get(url); profile=new FirefoxProfile(); // Set preferences for file type profile.setPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream"); driver.manage().window().maximize(); wait = new WebDriverWait(driver, 5); this.testSuiteName = ctx.getSuite().getName(); this.testName = testName; this.testMethodName = method.getName(); } @AfterMethod(alwaysRun = true) public void tearDown(ITestResult result) { if(result.getStatus()==ITestResult.FAILURE) { //capture screenshot } log.info("Close driver"); // Close browser driver.quit(); } } 3)DataProvider
@DataProvider(name="SearchProvider", parallel=true) public Object[][] getDataFromDataprovider(){ return new Object[][] { { "[email protected]", "123456" }, { "[email protected]", "123456" } }; } 4)BasePage
public class BasePage { protected WebDriver driver; public WebDriverWait wait; protected Logger log; public BasePage(WebDriver driver, Logger log) { this.driver = driver; this.log = log; wait = new WebDriverWait(driver, 5); } protected void sleep(long millis) { try { Thread.sleep(millis); } catch (InterruptedException e) { e.printStackTrace(); } } protected void clickStallElement(By locator) { try { waitForVisibilityOf(locator, 30); findElement(locator).click(); } catch(org.openqa.selenium.StaleElementReferenceException ex) { waitForVisibilityOf(locator, 30); findElement(locator).click(); } } /** Open page with given URL */ protected void openUrl(String url) { driver.get(url); } /** Find element using given locator */ protected WebElement findElement(By locator) { return driver.findElement(locator); } /** Find all elements using given locator */ protected List<WebElement> findAllElements(By locator) { return driver.findElements(locator); } /** Click on element with given locator when its visible */ protected void click(By locator) { waitForVisibilityOf(locator, 30); findElement(locator).click(); log.info("Clicked WebElement"); } ... } 5) BaseTest
public class SignInPageTest extends BaseTest { @Test(dataProvider = "SearchProvider", dataProviderClass = DBDataProvider.class) public void loginTest(String username, String password) { System.err.println("Running Test=> " + this + " -> on thread [" + Thread.currentThread().getId() + "]"); log.info("Starting LogInTest # for " + username); ClickAcadamyLandingPage home = new ClickAcadamyLandingPage(driver, log); home.openPage(); home.closeNewsLetterPopup(); // click login ClickAcadamyLoginPage loginPage = new ClickAcadamyLoginPage(driver, log); loginPage = home.clickLoginBtn(); loginPage.enterEmailAddress(username); loginPage.enterPassword(password); ClickAcadamyDashboard dashboard = loginPage.clickLogin(); dashboard.clickLogout(); } } 6)testng.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> <suite name="Suite" data-provider-thread-count = "2" thread-count="2" verbose="1" parallel="tests"> <listeners> <listener class-name="com.paralleltest.basetest.TestListener"/> </listeners> <test name="loginTest"> <classes> <class name="com.paralleltest.SignInPageTest"/> </classes> </test> </suite>