Sharing same selenium WebDriver between step definition files

前端 未结 2 1580
轻奢々
轻奢々 2020-12-03 02:08

Right now we\'re working on adopting Cucumber to run functional tests on our Java8/Spring app. We want our step definition files to remain as DRY as possible and as such pla

2条回答
  •  一个人的身影
    2020-12-03 02:51

    I reccomend you to use pico-container as a dependency injection framework to use with cucumber-jvm.

    With PicoContainer, you can have a 'base' class with the instance of WebDriver, and then pass this base class automactically to any other class. Or even you could pass directly the web driver if you prefer.

    
        info.cukes
        cucumber-picocontainer
        1.2.3
        test
    
    

    Example:

    Base class with the instance of WebDriver:

    public class ContextSteps {
    
       private static boolean initialized = false;
    
       private WebDriver driver;
    
       @Before
       public void setUp() throws Exception {
          if (!initialized) {
             // initialize the driver
             driver = = new FirefoxDriver();
    
             initialized = true;
          }
       }
    
       public WebDriver getDriver() {
          return driver;
       }
    }
    

    Other class who access webDriver through pico-container DI.

    public class OtherClassSteps {
    
       private ContextSteps contextSteps;
    
       // PicoContainer injects class ContextSteps
       public OtherClassSteps (ContextSteps contextSteps) {
          this.contextSteps = contextSteps;
       }
    
    
       @Given("^Foo step$")
       public void fooStep() throws Throwable {
          // Access WebDriver instance
          WebDriver driver = contextSteps.getDriver();
       }
    }
    

    Hope it helps.

提交回复
热议问题