I am using selenium cucumber maven framework with Junit. I need to run the tests in multiple browsers. How can i achieve this? Can anyone help me? my TestRunnerTest.java file is as below:
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/1login.feature"
, glue= {"stepDefinition"}
, plugin = { "com.vimalselvam.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"},
monochrome = true
)
public class TestRunnerTest {
public static WebDriver driver;
public static String timeStamp = new
SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
private static TestRunnerTest sharedInstance = new TestRunnerTest();
private TestRunnerTest() { }
public static TestRunnerTest getInstance() {
return sharedInstance;
}
@BeforeClass
public static void before() {
System.setProperty("webdriver.chrome.driver", "E:\\ChromeDriverNew\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@AfterClass
public static void after() {
Runtime.getRuntime().addShutdownHook(new Thread()
{
public void run()
{
try {
Reporter.loadXMLConfig(new File("config/report.xml"));
Files.move(Paths.get("target/cucumber-reports"), Paths.get("target/cucumber-reports_ "+
LocalDateTime.now().format(DateTimeFormatter.ofPattern("L-d-YYYY H-m-s"))),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
});
driver.quit();
}
And my baseDefinition is as below:
public class baseDefinition {
public Boolean beforsuit=true;
public String baseurl = "https://click2rail-dev.cloudiumedge.com";
private static TestRunnerTest runner_TestObj = TestRunnerTest.getInstance();
public WebDriver driver = runner_TestObj.driver;
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new
Date());
}
Now I need to run the test in multiple browsers (Chrome,Firefox,IE). What are the changes i need to do in the TestRunner.java file & baseDefinition file ? It would be very helpful if you guys help me with a solution since I am a beginner. I have searched so many times ..But i am getting the solutions with TestNG .
One approach of doing this would be to have an environment variable for browser name. Since you are using maven, you can simply inject the browser name environment variable while initiating the test from command line. Example: mvn test -DbrowserName=Firefox
You may then modify your @BeforeClass method to have a conditional check based on the browser name.
@BeforeClass
public static void before() {
String browserName=System.getenv("browserName");
if (browserName
.equalsIgnoreCase("Chrome")) {
System.setProperty("webdriver.chrome.driver", "E:\\ChromeDriverNew\\chromedriver.exe");
driver=new ChromeDriver();
} else if (browserName
.equalsIgnoreCase("Firefox")) {
//Set gecko driver path and initiate firefox
} else if (browserName
.equalsIgnoreCase("IE")) {
//Set IEDriver path and initiate IE
} else {
logger.debug("Error Message----> "
+ "browser name not mentioned properly");
System.exit(0);
}
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}