2

to all. Curently writing a little BDD Test automation framework, using Java11+Junit5+Cucumber+Selenium, build tool: Graddle. Created a little test for validating Google title. When starting test, using Test task in Graddle or running CucumberRunner class, in both cases getting the same result: two times @Before method is executed, once @After method is executed and one browser windows is staying open. After added one more test, the same situation, only 4 browsers are opened, 2 of them are closing. Can anyone help with this situation?

Link to repository

After some watching of logs saw, that, seems, @Before is not executed twice, but Driver class is initialized twice, but why it happens no idea for now...

My code for now: CucumberRunner.java:

@RunWith(Cucumber.class )
@CucumberOptions(
        features = "src\\test\\java\\features",
        glue = {"steps", "utils"},
        tags = "@smoke")
public class CucumberRunner {
}

Driver.java:

public class Driver {
    private WebDriver driver;

    public Driver(){
        driverInitialization();
    }

    private void driverInitialization(){
        System.setProperty("webdriver.chrome.driver", "D:\\Soft\\selenium-drivers\\chromedriver.exe");
        System.out.println("Starting driver.");
        var browserName = "chrome";
        switch (browserName.toLowerCase()){
            case "chrome":
                System.out.println("Starting chrome");
                driver = new ChromeDriver();
                System.out.println("Before break.");
                break;
            case "firefox":
                driver = new FirefoxDriver();
                break;
            default:
                throw new NotFoundException("Browser not found: " + browserName);
        }
    }

    public WebDriver getDriver(){
        return driver;
    }

    public WebDriverWait getWebDriverWait(){
        return new WebDriverWait(driver, 120);
    }

    public void terminateDriver(){
        System.out.println("Terminating driver.");
        if (driver != null) {
            driver.close();
            driver.quit();
        }
    }
}

Hooks.java:

public class Hooks {
    private Driver driver;

    @Before
    public void setup(){
        System.out.println("In the Setup method.");
        driver = new Driver();
    }

    @After
    public void tearDown(){
        System.out.println("In the TearDown method.");
        driver.terminateDriver();
    }
}
Ukrainis
  • 534
  • 2
  • 16
  • I'm also interested in this. The same thing happens to me, and since I'm a beginner in Cucumber, I suspect I might be doing something wrong. – Mate Mrše Feb 22 '19 at 08:19
  • Guys, you should share your project on any Git platform(Github) so that others could see the complete code that will help them to understand and resolve your issue. Once done, please add the link of your project with the question above. – kwishna Feb 22 '19 at 17:08
  • @ABD, added link to repository. – Ukrainis Feb 22 '19 at 20:17

1 Answers1

3

I think your Hook Class should be like this As You Are Using selenium-picocontainer DI.

public class Hooks {

private Driver driver;

public Hooks(Driver driver) {
    this.driver = driver;
   }

@Before
public void setup(){
    System.out.println("In the Setup method.");
   }

@After
public void tearDown(){
    System.out.println("In the TearDown method.");
    driver.terminateDriver();
   }
}
kwishna
  • 412
  • 7
  • 14