Sharing same selenium WebDriver between step definition files

前端 未结 2 1540
轻奢々
轻奢々 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:48

    This question is old, and I left the project shortly after asking this question, but I went back and looked at the code we put in place (using the singleton pattern) and this is what we ended up with. I forget exactly why we couldn't use pico-container (it was possibly an organizational constraint) but if you can use extra libraries I remember that solution worked well.

    I will leave that as the accepted answer but hopefully this solution is useful for those who find themselves in a similar position that I was in a few years ago.

    public class TestingBase {
    
        private static TestingBase instance;
        private static WebDriver driver;
        private static Thread CLOSE_DRIVER = new Thread() {
            @Override
            public void run() {
                driver.close();
            }
    
        };
    
        static {
            Runtime.getRuntime().addShutdownHook(CLOSE_DRIVER);
        }
    
        private TestingBase() {
            DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
            desiredCapabilities.setJavascriptEnabled(true);
            desiredCapabilities.setCapability("takesScreenshot", false);
            desiredCapabilities.setCapability("handlesAlerts", true);
    
            desiredCapabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[]{
                    "--web-security=false",
                    "--ssl-protocol=TLSv1",
                    "--ignore-ssl-errors=true",
                    "--webdriver-loglevel=ERROR",
                    "--webdriver-logfile=/var/log/phantomjs/ghostrdriver.log"
            });
            desiredCapabilities.setCapability("elementScrollBehavior",true);
            driver = new PhantomJSDriver(desiredCapabilities);
        }
    
        public static TestingBase getTestingBase() {
            if (instance == null) {
                instance = new TestingBase();
            }
            return instance;
    
        }
    
        public static WebDriver getDriver() {
            return getTestingBase().driver;
        }
    }
    
    0 讨论(0)
  • 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.

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-picocontainer</artifactId>
        <version>1.2.3</version>
        <scope>test</scope>
    </dependency>
    

    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.

    0 讨论(0)
提交回复
热议问题