Menu

1 / 2

Zero-config Cucumber + Selenium in Java: no @Before, no driver setup, no cleanup

DEV Community·Mohammad Saddam Hossain·7 months ago
#aWMcsYl3
#java#selenium#cucumber#scenario#article
Reading 0:00
15s threshold

The Cucumber + Selenium boilerplate problem

Every Cucumber + Selenium project in Java starts the same way. Before you write a single step, you write the infrastructure:

public class Hooks {

private WebDriver driver ;

@Before public void setUp () { WebDriverManager . chromedriver (). setup (); ChromeOptions options = new ChromeOptions (); driver . manage (). timeouts (). implicitlyWait ( Duration . ofSeconds ( 10 )); driver = new ChromeDriver ( options ); DriverHolder . set ( driver ); }

@After public void tearDown ( Scenario scenario ) { if ( scenario . isFailed ()) { byte [] screenshot = (( TakesScreenshot ) driver ). getScreenshotAs ( OutputType . BYTES ); scenario . attach ( screenshot , "image/png" , "Failure screenshot" ); } if ( driver != null ) driver . quit (); DriverHolder . remove (); } }

Enter fullscreen mode

Exit fullscreen mode

Then you wire a ThreadLocal for parallel safety. Then you figure out how to get it into step definitions. Then you add a static helper class.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.