0

I have developed a testing framework using Selenium WebDriver (PageObject Pattern) and TestNG. My page objects are initialized using pageFactory.

While running single instance everything works fine. But, when I increases instances (running in parallel), only one instance detects the web elements.

I don't understand what is the problem with this framework.

My framework is as below:

DifferentBrowsers.java (for instatiating driver)

package com.tcs.rsf.utilities;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

import jxl.read.biff.BiffException;

import org.apache.log4j.Logger;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.opera.core.systems.OperaDriver;
import com.tcs.rsf.excel2bean.ExcelToBeanRepoAddressedOffer;

public class DifferentBrowsers {

    private WebDriver driver;
    static Properties path = new Properties();
    private static Logger log = Logger.getLogger(CrossBrowser.class);

    public WebDriver getBrowser(String browserName, String browserType) throws MalformedURLException {

        try {

        path.load(new FileInputStream("path.properties"));

        if (browserName.equalsIgnoreCase("firefox") && browserType.equalsIgnoreCase("local")) {
            System.out
                    .println("---------------: Inside firefox profile :--------------");

            DesiredCapabilities ff = DesiredCapabilities.firefox();
            ff.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
                    UnexpectedAlertBehaviour.ACCEPT);
            ff.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
            ff.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            ff.setCapability(CapabilityType.ENABLE_PERSISTENT_HOVERING, true);
            ff.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);

            driver = new FirefoxDriver(ff);
            System.out
                    .println("==========:::: got firefox driver ::::===========");

        } else if (browserName.equalsIgnoreCase("chrome") && browserType.equalsIgnoreCase("local")) {
            System.out
                    .println("---------------: Inside chrome profile :--------------");

            DesiredCapabilities chro = DesiredCapabilities.chrome();
            chro.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
                    UnexpectedAlertBehaviour.ACCEPT);
            chro.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
            chro.setCapability(CapabilityType.HAS_NATIVE_EVENTS,true);
            chro.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS,true);
            chro.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT,true);
            chro.setCapability(CapabilityType.SUPPORTS_ALERTS,true);
            chro.setCapability(CapabilityType.ENABLE_PERSISTENT_HOVERING, true);    
            System.setProperty("webdriver.chrome.driver",path.getProperty("chromeDriver2.6"));

            driver = new ChromeDriver(chro);
            System.out
                    .println("==========:::: got Chrome driver ::::===========");
        } else if (browserName.equalsIgnoreCase("opera") && browserType.equalsIgnoreCase("local")) {
            System.out
                    .println("---------------: Inside opera profile :--------------");
            DesiredCapabilities opera = DesiredCapabilities.chrome();
            opera.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
                    UnexpectedAlertBehaviour.IGNORE);
            opera.setCapability("opera.binary","C:\\Program Files\\Opera\\launcher.exe");
            driver = new OperaDriver(opera);
            System.out
                    .println("==========:::: got opera driver ::::===========");
        } else if (browserName.equalsIgnoreCase("firefox") && browserType.equalsIgnoreCase("remote")) {
            System.out.println("---------------: Inside remote firefox profile :--------------");

            DesiredCapabilities ff = DesiredCapabilities.firefox();
            ff.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
                    UnexpectedAlertBehaviour.ACCEPT);
            ff.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            ff.setCapability(CapabilityType.ENABLE_PERSISTENT_HOVERING, true);
            ff.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
            ff.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);

            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), ff);
            System.out.println("==========:::: got remote firefox driver ::::===========");
        }
        else if (browserName.equalsIgnoreCase("chrome") && browserType.equalsIgnoreCase("remote")) {
            System.out.println("---------------: Inside remote chrome profile :--------------");

            DesiredCapabilities chro = DesiredCapabilities.chrome();
            chro.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
                    UnexpectedAlertBehaviour.ACCEPT);
            chro.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
            chro.setCapability(CapabilityType.HAS_NATIVE_EVENTS,true);
            chro.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS,true);
            chro.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT,true);
            chro.setCapability(CapabilityType.SUPPORTS_ALERTS,true);
            chro.setCapability(CapabilityType.ENABLE_PERSISTENT_HOVERING, true);

            System.setProperty("webdriver.chrome.driver",path.getProperty("chromeDriver2.6"));

            chro.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), chro);

            System.out.println("==========:::: got remote Chrome driver ::::===========");
        } else if (browserName.equalsIgnoreCase("opera") && browserType.equalsIgnoreCase("remote")) {
            System.out
            .println("---------------: Inside remote opera profile :--------------");
            DesiredCapabilities opera = DesiredCapabilities.opera();
            opera.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR,
                    UnexpectedAlertBehaviour.IGNORE);
            opera.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
            driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), opera);

            System.out.println("==========:::: got remote opera driver ::::===========");
        } 

        else {

            System.out.println("default firefox driver called");
            driver = new FirefoxDriver();
        }

        } catch (IOException e) {
            log.error("An error occured while instantiating driver.");
            e.printStackTrace();
        }
        return driver;

    }
}

PageBase.java (Parent class of all page objects..All utitlity methods go here)

package com.tcs.rsf.pages;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.UnexpectedAlertBehaviour;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import org.testng.Assert;

import com.opera.core.systems.OperaDriver;
import com.tcs.rsf.exception.ElementNotVisibleException;
import com.tcs.rsf.utilities.CrossBrowser;
import com.tcs.rsf.utilities.Utility;
import com.tcs.rsf.utilities.Verification;

public class PageBase {

    Utility util = new Utility();

    protected WebDriver driver;
    protected WebElement element;

    private static Logger log = Logger.getLogger(PageBase.class);
    public static final int IMPLICIT_TIMEOUT_VALUE = 30; // seconds
    public static final int EXPLICIT_TIMEOUT_VALUE = 30; // seconds 
    public static final int PAGELOAD_TIMEOUT_VALUE = 10; // seconds 
    static Properties path = new Properties();
    Verification verification=new Verification();

    public void sleeper(int timeInMilliSeconds) {

        try {
            Thread.sleep(timeInMilliSeconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void open(String url) {

        Assert.assertNotNull(this.driver);
        this.driver.get(url);

    }

    public void close() {

        this.driver.quit();
    }

    public String getTitle() {

        return this.driver.getTitle();
    }

    public void implicitTimeout() {

        this.driver.manage().timeouts()
                .implicitlyWait(IMPLICIT_TIMEOUT_VALUE, TimeUnit.SECONDS);
    }

    public void logOut() {

        this.driver.findElement(By.id("forLogout")).click();

    }

PageObject.java (my page Object...extends PageBase)

package com.tcs.rsf.pages;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Locale;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import com.tcs.rsf.dao.SeclendOverlayDao;
import com.tcs.rsf.dao.SeclendTradingDAO;
import com.tcs.rsf.dao.SeclendVerificationDAO;
import com.tcs.rsf.utilities.Utility;


public class PageObject extends PageBase {

    public PageObject(WebDriver driver) {
        super(driver);
        super.driver = driver; //gets driver from parent class
    }


    WebDriverWait wait = new WebDriverWait(driver, EXPLICIT_TIMEOUT_VALUE);
    Utility utilities = new Utility();

    @FindBy(id = "abc")
    private WebElement findAbc;

    @FindBy(xpath= "//input[@id='xyz']")
    private WebElement findXyz;

    @FindBy(id = "sample")
    private WebElement findSample;

    public void clickABCButton() {

        findAbc.click();

    }

    public void isXyzLabelPresent() {

        Assert.assertEquals(findXyz.getText(), "");

    }

TestBase.java (parent class of test script...all before,after etc methods go here..also page object is iniitalized here via pagefactory)

package com.tcs.rsf.testbases;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;

import com.tcs.rsf.pages.MarketWatchPage;
import com.tcs.rsf.utilities.CrossBrowser;
import com.tcs.rsf.utilities.Zip;

public class TestBase {

    protected PageObject page;

    @Parameters(value={"browser","browserType"}) 
    @BeforeClass 
    public void startUp(String browser,String browserType) {
        try {
            page = PageFactory.initElements(DifferentBrowsers.getBrowser(browser,browserType), PageObject.class);   //driver is obtained from DifferentBrowsers.java
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    @BeforeMethod
    public void callBrowser() {

        try {
            path.load(new FileInputStream("path.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        page.open(//URL to navigate)); //open() is defined in PageBase.java
    }

    @AfterClass
    public void closeBrowser() {
        page.close();  // close() is defined in PageBase.java
    }

TestScript.java (script which will be executed...child of TestBase.java)

import com.tcs.rsf.exception.CustomSeleniumException;
import com.tcs.rsf.exception.PageLoadException;
import com.tcs.rsf.testbases.TestBase;
import com.tcs.rsf.utilities.Utility;

import java.io.PrintWriter;
import java.io.StringWriter;
import org.apache.log4j.Logger;
import org.testng.annotations.Test;

public class TestScript extends TestBase{

        private static Logger log = Logger.getLogger(TestScript.class);

        CustomSeleniumException customSeleniumException=new CustomSeleniumException();
        PageLoadException pageLoadExceptionCheck =new PageLoadException();

        Utility utilities =new Utility();

        @Test
        public void testCase() throws Exception {

            try{

                page.maximizeBrowser(); //defined in PageBase.java 
                page.implicitTimeout();  //defined in PageBase.java
                pageLoadExceptionCheck.validatePageLoad(super.getBaseURL(),super.getTestDriver());


                //**********Step 1**********//
                //User  logs into system with valid credentials.
                page.login("username","password");
                log.info("step 1 complete");

                //**********Step 2**********//
                //User check label Xyz
                page.isXyzLabelPresent();

                log.info("step 2 completed");

                //**********Step 3**********//
                //User clicks on Abc button
                page.clickAbcButton();

                log.info("step 3 complete");

            }
                catch(Exception e)
                {                   
                    customSeleniumException.seleniumThrownException(e,super.getTestDriver());

                    log.error("An exception occured in test case");

             } 

        } 
     } 

testNG.xml (this controls the flow of execution and number of instances and browser)

<?xml version="1.0" encoding="UTF-8"?>

<suite name="TestSuite" parallel="tests" thread-count="3">

    <parameter name="browser" value="firefox"></parameter>
    <parameter name="browserType" value="local"></parameter>

    <test name="Script_1">
        <classes>
            <class name="com.test.sample.testcases.TestScript"></class>
        </classes>
    </test>

    <test name="Script_2">
        <classes>
            <class name="com.test.sample.testcases.TestScript2"></class>
        </classes>
    </test>
    <test name="Script_3">
        <classes>
            <class name="com.test.sample.testcases.TestScript3"></class>
        </classes>
    </test>
    <test name="Script_4">
        <classes>
            <class name="com.test.sample.testcases.TestScript4"></class>
        </classes>
    </test>
    <test name="Script_5">
        <classes>
            <class name="com.test.sample.testcases.TestScript5"></class>
        </classes>
    </test>

</suite>

As test script above, I have 500 such scripts which I want to execute in parallel. But, when executing in parallel scripts are instantiated but only one script executes properly at a time and other doesn't receive elements.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
star95
  • 111
  • 3
  • 16

1 Answers1

0

after a very quick look, you at least should get rid of static WebDriver field in very first class.

user1058106
  • 530
  • 3
  • 12
  • even without `static `, it gives same result :( – star95 May 29 '14 at 15:45
  • What have you done after you have removed static? Can you edit your question and post the new code without static? – niharika_neo May 30 '14 at 14:04
  • @niharika_neo I removed `static` `WebDriver` initialization and `static` modifier of `getBrowser()` , but still am not able to run it in parallel because only one object gets the elements. – star95 May 30 '14 at 15:31
  • Post updated code... One more thing is that there is no pageMarketWatch definition in TestBase class... So, the code you posted won't even be compiled. am I wrong? super(driver); super.driver = driver; //gets driver from parent class - are you sure? – user1058106 May 30 '14 at 18:01
  • @user1058106..I have updated the code as above. still same issue. Anyways, I have defined utility methods in PageBase so I have made it my parent page object class. Is anything wrong in passing parent driver to sub classes? – star95 Jun 02 '14 at 06:15
  • @star95 My concern is about the constructor in PageObject class. It has super(driver); call while there is no such constructor in parent class. So, it won't be event built. I'm not clear how it can work at all; not even in parallel context. – user1058106 Jun 02 '14 at 07:55