Skip to main content

Launch browser using Selenium Cucumber

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();
}
	

Submitted by tgoswami on August 29, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.