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.