1

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.

3 Answers 3

1

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); } 
Sign up to request clarification or add additional context in comments.

1 Comment

I did not think about this approach but I have tried it and it works like a charm. Although creating separate classes added boilerplate codes, however, for now, the approach gets the job done and the system is behaving the way I was expecting it.
0

The for loop opens Chrome on the first run (browser=1), then opens Firefox on the second run (browser=2) and then continues with the test on the last opened browser (Firefox).

If you wish to run the complete test on both browsers, you need to put the rest of the test inside of the for loop.

Basically, you have something like:

public void navigateToUrl(String url) throws InterruptedException { for (browser = 1; browser <= 2; browser++) { //body of the for loop } //some more code } //end of navigateToUrl 

You need to change it to:

public void navigateToUrl(String url) throws InterruptedException { for (browser = 1; browser <= 2; browser++) { //body of the for loop } //some more code testMethod1() } //end of navigateToUrl 

6 Comments

I tried doing this but got red squiggly errors everywhere. Are you able to demonstrate how I can achieve your suggestion by using the source code provided in the question?
What do red squiggly errors say? Check the updated answer.
On hover, some of the errors say Cannot resolve symbol. Others say Expression expected
I tried your improved answer, but I am getting the red squiggly lines with the error Cannot resolve symbol 'amount'.
Is the testMethod1() in the Convert class? Also, post full error trace.
|
0

Your approach is not correct, if you need to run your tests in parallel you should go for either Selenium Grid or consider implementing other multi-threading option, i.e.:

In any case you will need to refactor your test to follow Parallel Tests - Best Practices at least:

  1. Remove static modifier from the WebDriver declaration
  2. Consider putting your WebDriver instance into the Thread Local Storage otherwise you will have clashes

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.