Skip to content

Instantly share code, notes, and snippets.

@rahulrathore44
Last active June 26, 2021 18:54
Show Gist options
  • Save rahulrathore44/e2aeff9258d9f63cfd73c6f4dc9f5266 to your computer and use it in GitHub Desktop.
Save rahulrathore44/e2aeff9258d9f63cfd73c6f4dc9f5266 to your computer and use it in GitHub Desktop.
Dependency Injection for Selenium Webdriver
Feature: To setup the browser only once
Scenario: Open the browser
Given BrowserOne_I open the URL
And BrowserOne_I wait for 5 sec
package v6.cucumber6.featurefile.injection;
import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;
@CucumberOptions(features = "classpath:v6/cucumber6/featurefile/injection", glue = {
"v6.cucumber6.featurefile.injection",
"com.v6.cucumber6.hooks" }, dryRun = false, monochrome = true, plugin = "pretty")
public class BrowserInjectionRunner extends AbstractTestNGCucumberTests {
}
package v6.cucumber6.featurefile.injection;
import com.v6.cucumber6.hooks.TestSettingImpl;
import io.cucumber.java.en.Given;
public class BrowserInjectionStepDfn {
private TestSettingImpl testSetting;
public BrowserInjectionStepDfn(TestSettingImpl testSetting) {
this.testSetting = testSetting;
}
@Given("BrowserOne_I open the URL")
public void browser_one_i_open_the_url() {
testSetting.getaDriver().get("https://www.google.com");
}
@Given("BrowserOne_I wait for {int} sec")
public void browser_one_i_wait_for_sec(Integer sec) throws InterruptedException {
Thread.sleep((sec * 1000));
}
}
Feature: To setup the browser only once
Scenario: Open the browser
Given BrowserTwo_I open the URL
And BrowserTwo_I wait for 5 sec
package v6.cucumber6.featurefile.injection;
import com.v6.cucumber6.hooks.TestSettingImpl;
import io.cucumber.java.en.Given;
public class BrowserInjectionTwoStepDfn {
private TestSettingImpl testSetting;
public BrowserInjectionTwoStepDfn(TestSettingImpl testSetting) {
this.testSetting = testSetting;
}
@Given("BrowserTwo_I open the URL")
public void browser_two_i_open_the_url() {
testSetting.getaDriver().get("https://github.com/");
}
@Given("BrowserTwo_I wait for {int} sec")
public void browser_two_i_wait_for_sec(Integer int1) throws InterruptedException {
Thread.sleep(int1 * 1000);
}
}
package com.v6.cucumber6.hooks;
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class HookHelper {
public TestSettingImpl testSetting;
public HookHelper(TestSettingImpl testSetting) {
this.testSetting = testSetting;
}
@Before
public void before() {
System.out.println(testSetting.toString());
}
@After
public void after() {
testSetting.getaDriver().quit();
}
}
package com.v6.cucumber6.hooks;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class TestSettingImpl {
private WebDriver aDriver;
public WebDriver getaDriver() {
return aDriver;
}
private void initWebDriver() {
WebDriverManager.chromedriver().setup();
aDriver = new ChromeDriver();
}
public TestSettingImpl() {
initWebDriver();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment