If your tests can pass independently then it can be done using cucumber-guice injection to independently open browser for each scenario.
You may have to arrange your tests as scenarios like below
Scenario: set source country
Given source country is setup
Scenario: set source currency
Given source currency is setup
OR you can use example feature of cucumber. In this method you may to do extra parsing in step defs depending on how much different it is to set up each source
Scenario Outline: login registration
Given these <source> are set up with <details>
Examples:
|source|details|
|"country" |"xyz"|
|"currency" |"abc"|
...
...
cucumber-guice will open one browser for each scenario in both above methods ( cucumber consider each example line a scenario)
Now the actual guice implementation. To use guice you will need cucumber-guice dependency
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-guice</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.3</version>
<scope>test</scope>
</dependency>
These are the classes that I use to independently open browser for each scenario. In a chromemanager class you do
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
public class ChromeManager extends DriverManager {
protected ChromeDriver driver;
@Override
protected WebDriver createDriver() {
System.setProperty("webdriver.chrome.driver", "/Users/../chromedriver");
return driver = new ChromeDriver(ChromeDriverService.createDefaultService());
}
@Override
public WebDriver getDriver() {
if (driver == null) {
this.createDriver();
}
return driver;
}
}
Driverfactory class
import org.openqa.selenium.WebDriver;
public class DriverFactory {
public WebDriver getManager() {
return new ChromeManager().getDriver();
}
}
Drivermanager class
import org.openqa.selenium.WebDriver;
public abstract class DriverManager {
protected abstract WebDriver createDriver();
public abstract WebDriver getDriver();
}
and then in a global class
import org.openqa.selenium.WebDriver;
import io.cucumber.guice.ScenarioScoped;
@ScenarioScoped
public class Global {
public WebDriver driver;
public WebDriverWait wait;
public Global() {
driver = new DriverFactory().getManager();
wait = new WebDriverWait(driver, 3000);
//any class you instantiate here will be injected in below initialIT class for each scenario
}
}
and finally your test class
import com.google.inject.Inject;
import com.test.support.Global;
public class InitialIT {
public static ChromeDriver driver ;
@Inject
Global global;
@Test
@Given("source country is setup")
public void setCountry() throws MalformedURLException {
global.driver.get(yourSite);
}
@Test
@Given("source currency is setup")
public void setCurrency() throws MalformedURLException {
global.driver.get(yourSite);
}
@After
public void closeBrowser() {
global.driver.quit();
}
}