Launch browser using Selenium Cucumber

Profile picture for user devraj

To launch browser in Selenium with Cucumber is same as launching it in Selenium itself.

Steps to Launch a Chrome Browser in Selenium Cucumber

Step 1: Add below code to your Base File or Steps Class File. Here Path for Chrome driver is defined in my Constants.Java file. I have also added code for implicit wait and to maximize browser windows here.

public static WebDriver driver;
	
public void setDriver()
{
    System.setProperty("webdriver.chrome.driver", Constants.MAC_CHROME_DRIVER);  
    driver = new ChromeDriver();  
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();
}

Step 2: Call setDriver() method in @Before hook if you want to open browser before every scenario or you can also call it in open home page step definition directly.

@Before
public void bf()
{
    TestBase tb = new TestBase();
    tb.setDriver();
}

Step 3: Use driver inside steps definitions

@Given("^(?:I am|User is) on the home page$")
public void i_am_on_the_homepage()
{
	driver.get("http://automationpractice.com");
}

Step 4: Quit browser in @after hook, to close browser after every scenario.

@After
public void af()
{
	driver.quit();
}