Search code examples
javaselenium-webdrivercucumber-jvmpage-factory

When trying to run Selenium Cucumber test in two browsers, test runs in only one browser


I am stuck whilst trying to run a selenium cucumber java script in two browsers (Chrome, Firefox). The test works fine when I use a single browser. I use selenium PageFactory class to initialise the web elements.

The problem is when I run the test for two browsers, the first browser opens, navigates to the URL and then nothing happens. Web elements are not initialised. The test moves on to the second browser, navigates to URL, web elements are initialised, subsequent test methods (testMethod1) run as expected. Why is the test not running on the first browser?

This is the PageFactory BasePage class holding the web elements:

public class BasePage {       
private final WebDriver driver;  

public BasePage(WebDriver driver) {this.driver = driver;} //constructor


 @FindBy(id = "cc-amount")
 public WebElement amountField;

This is the test class and how I have tried to run the test in two browsers:

public class Convert {

  private static WebDriver driver;
  private final BaseUtil baseUtil = new BaseUtil();
  private static BasePage basePage;
  private static int browser;



 public void navigateToUrl(String url) throws InterruptedException {  
for (browser = 1; browser <= 2; browser++) {

  if (browser == 1) {
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();

  } else if (browser == 2) {
    WebDriverManager.firefoxdriver().setup();
    driver = new FirefoxDriver();
  }

  driver.get(baseUtil.getMyUrl()); //Url coming from a utility class
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  basePage = PageFactory.initElements(driver, BasePage.class); //initialisation of the BasePage class conttaining the pagefactory web elements

After the browser initialisation, this method runs ok in the second browser (Firefox) but did not run at all in the first browser (Chrome):

  public void testMetod1(String amount) throws InterruptedException {
  basePage.amountField.click();

My suspicion is that PageFacory could not be initiated for both browsers in a single run but I do not know how to diagnose this further. It could also be a for loop error.


Solution

  • I believe it's a synchronisation issue. Just you need to create a class for each browser, then you can create another base class which contains all the shared code.

    Creating a class for each browser will give you some advantages:

    1. Cleaner code.
    2. Your tests will run in parallel.
    3. Specific test cases for a specific browsers (Specially for IE).
    4. Solve this sync issue.

    So basically instead of using Converter, you can create:

    • ChromeConverter
    • FirefoxConverter

    And inside your TestExecuter class, just create instance for each one and run your tests.

    And to be more detailed, you are using one class Convert and inside it you have two static variables:

      private static WebDriver driver;
      private static BasePage basePage;
    

    Removing the static keyword from BasePage class won't solve the root cause as you are using BasePage which is a static class and can't be instantiated as instance class and it's inside an external library so you need to separate the classes as kind of wrapper.

    Here you are trying to use the same exact initialisation with Chrome then Firefox which won't work in parallel or even in for loop until you can wait to the first test then dispose it and initialise all the stuff related to the second test again.

    So the correct approach is to create a separate Convert for each browser like that:

    ChromeConverter
    FirefoxConverter
    

    And In your TestExecuter you just need to initialise each one and use it, something like that:

     @Given("...")
     public void navigateToUrl(String url) throws InterruptedException {
       chromeConverterApp.navigateToUrl(url);
       firefoxConverterApp.navigateToUrl(url);
     }
     @When("^..")
     public void enterCurrencies(String amount, String from, String to) throws InterruptedException {
       chromeConverterApp.enterCurrencies(amount, from, to);
       firefoxConverterApp.enterCurrencies(amount,from,to);
     }