├── .gitignore ├── src └── test │ ├── resources │ ├── features │ │ └── search.feature │ └── application.properties │ └── java │ └── com │ └── natritmeyer │ └── examplecucumberjvmspringframework │ ├── aut │ ├── model │ │ ├── BingSearchResultsPage.java │ │ └── BingHomePage.java │ └── implementations │ │ ├── desktop │ │ ├── DesktopBingSearchResultsPage.java │ │ └── DesktopBingHomePage.java │ │ └── mobile │ │ ├── MobileBingSearchResultsPage.java │ │ └── MobileBingHomePage.java │ ├── config │ └── Config.java │ ├── glue │ ├── CucumberSpringConfiguration.java │ └── BingSearchSteps.java │ ├── testrunners │ └── RunCucumberIT.java │ └── browsers │ ├── DesktopBrowser.java │ └── MobileBrowser.java ├── LICENSE ├── pom.xml └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea/ 3 | *.iml 4 | target/ 5 | -------------------------------------------------------------------------------- /src/test/resources/features/search.feature: -------------------------------------------------------------------------------- 1 | Feature: Search for content 2 | 3 | Scenario: Search for information 4 | Given I am on the bing search engine 5 | When I enter a search term 6 | Then relevant results for that search term are displayed 7 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/aut/model/BingSearchResultsPage.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.aut.model; 2 | 3 | public interface BingSearchResultsPage { 4 | String getFirstResultTitle(); 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/aut/model/BingHomePage.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.aut.model; 2 | 3 | public interface BingHomePage { 4 | void load(); 5 | 6 | void searchFor(String searchTerm); 7 | } 8 | -------------------------------------------------------------------------------- /src/test/resources/application.properties: -------------------------------------------------------------------------------- 1 | browser.desktop.width=1024 2 | browser.desktop.height=768 3 | 4 | browser.mobile.width=400 5 | browser.mobile.height=800 6 | browser.mobile.useragent=Mozilla/5.0 (iPhone; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.25 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1 7 | 8 | aut.urls.home=https://www.bing.com/ 9 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/config/Config.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.config; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.PropertySource; 5 | 6 | @PropertySource("application.properties") 7 | @ComponentScan("com.natritmeyer.examplecucumberjvmspringframework") 8 | public class Config { 9 | } 10 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/glue/CucumberSpringConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.glue; 2 | 3 | import com.natritmeyer.examplecucumberjvmspringframework.config.Config; 4 | import io.cucumber.spring.CucumberContextConfiguration; 5 | import org.springframework.test.context.ContextConfiguration; 6 | 7 | @ContextConfiguration(classes = Config.class) 8 | @CucumberContextConfiguration 9 | public class CucumberSpringConfiguration { 10 | } 11 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/testrunners/RunCucumberIT.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.testrunners; 2 | 3 | import io.cucumber.junit.Cucumber; 4 | import io.cucumber.junit.CucumberOptions; 5 | import org.junit.runner.RunWith; 6 | 7 | @RunWith(Cucumber.class) 8 | @CucumberOptions( 9 | features = {"classpath:features/"}, 10 | plugin = {"pretty"}, 11 | glue = {"com.natritmeyer.examplecucumberjvmspringframework.glue"}) 12 | public class RunCucumberIT { 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/aut/implementations/desktop/DesktopBingSearchResultsPage.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.aut.implementations.desktop; 2 | 3 | import com.natritmeyer.examplecucumberjvmspringframework.aut.model.BingSearchResultsPage; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.context.annotation.Profile; 8 | import org.springframework.stereotype.Component; 9 | 10 | @Component 11 | @Profile("desktop") 12 | public class DesktopBingSearchResultsPage implements BingSearchResultsPage { 13 | private final WebDriver driver; 14 | 15 | @Autowired 16 | public DesktopBingSearchResultsPage(WebDriver driver) { 17 | this.driver = driver; 18 | } 19 | 20 | @Override 21 | public String getFirstResultTitle() { 22 | return driver.findElement(By.cssSelector("ol#b_results > li > h2 > a")).getText(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/aut/implementations/mobile/MobileBingSearchResultsPage.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.aut.implementations.mobile; 2 | 3 | import com.natritmeyer.examplecucumberjvmspringframework.aut.model.BingSearchResultsPage; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.context.annotation.Profile; 8 | import org.springframework.stereotype.Component; 9 | 10 | @Component 11 | @Profile("mobile") 12 | public class MobileBingSearchResultsPage implements BingSearchResultsPage { 13 | private final WebDriver driver; 14 | 15 | @Autowired 16 | public MobileBingSearchResultsPage(WebDriver driver) { 17 | this.driver = driver; 18 | } 19 | 20 | @Override 21 | public String getFirstResultTitle() { 22 | driver.findElement(By.cssSelector("ol#b_results")); //waits for the results to be displayed 23 | return driver.findElement(By.cssSelector("ol#b_results > li.b_algo > div > a > h2")).getText(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/browsers/DesktopBrowser.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.browsers; 2 | 3 | import org.openqa.selenium.Dimension; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | import org.springframework.beans.factory.annotation.Value; 7 | import org.springframework.context.annotation.Bean; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.context.annotation.Profile; 10 | 11 | import java.util.concurrent.TimeUnit; 12 | 13 | @Configuration 14 | public class DesktopBrowser { 15 | @Value("${browser.desktop.width}") 16 | private int desktopWidth; 17 | 18 | @Value("${browser.desktop.height}") 19 | private int desktopHeight; 20 | 21 | @Bean 22 | @Profile("desktop") 23 | public WebDriver getDriver() { 24 | WebDriver driver = new ChromeDriver(); 25 | driver.manage().window().setSize(new Dimension(desktopWidth, desktopHeight)); 26 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 27 | return driver; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Nat Ritmeyer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/aut/implementations/desktop/DesktopBingHomePage.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.aut.implementations.desktop; 2 | 3 | import com.natritmeyer.examplecucumberjvmspringframework.aut.model.BingHomePage; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.Profile; 9 | import org.springframework.stereotype.Component; 10 | 11 | @Component 12 | @Profile("desktop") 13 | public class DesktopBingHomePage implements BingHomePage { 14 | @Value("${aut.urls.home}") 15 | private String homePageUrl; 16 | 17 | private final WebDriver driver; 18 | 19 | @Autowired 20 | public DesktopBingHomePage(WebDriver driver) { 21 | this.driver = driver; 22 | } 23 | 24 | @Override 25 | public void load() { 26 | driver.get(homePageUrl); 27 | } 28 | 29 | @Override 30 | public void searchFor(String searchTerm) { 31 | driver.findElement(By.id("sb_form_q")).sendKeys(searchTerm); 32 | driver.findElement(By.id("sb_form_go")).click(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/glue/BingSearchSteps.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.glue; 2 | 3 | import com.natritmeyer.examplecucumberjvmspringframework.aut.model.BingHomePage; 4 | import com.natritmeyer.examplecucumberjvmspringframework.aut.model.BingSearchResultsPage; 5 | import io.cucumber.java8.En; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | 8 | import static org.assertj.core.api.Assertions.assertThat; 9 | 10 | public class BingSearchSteps implements En { 11 | private final BingHomePage bingHomePage; 12 | private final BingSearchResultsPage bingSearchResultsPage; 13 | 14 | @Autowired 15 | public BingSearchSteps(BingHomePage bingHomePage, BingSearchResultsPage bingSearchResultsPage) { 16 | this.bingHomePage = bingHomePage; 17 | this.bingSearchResultsPage = bingSearchResultsPage; 18 | 19 | Given("I am on the bing search engine", () -> { 20 | bingHomePage.load(); 21 | }); 22 | 23 | When("I enter a search term", () -> { 24 | bingHomePage.searchFor("cucumber"); 25 | }); 26 | 27 | Then("relevant results for that search term are displayed", () -> { 28 | assertThat(bingSearchResultsPage.getFirstResultTitle()).contains("Cucumber"); 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/aut/implementations/mobile/MobileBingHomePage.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.aut.implementations.mobile; 2 | 3 | import com.natritmeyer.examplecucumberjvmspringframework.aut.model.BingHomePage; 4 | import org.openqa.selenium.By; 5 | import org.openqa.selenium.WebDriver; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.Profile; 9 | import org.springframework.stereotype.Component; 10 | 11 | @Component 12 | @Profile("mobile") 13 | public class MobileBingHomePage implements BingHomePage { 14 | @Value("${aut.urls.home}") 15 | private String homePageUrl; 16 | 17 | private final WebDriver driver; 18 | 19 | @Autowired 20 | public MobileBingHomePage(WebDriver driver) { 21 | this.driver = driver; 22 | } 23 | 24 | @Override 25 | public void load() { 26 | driver.get(homePageUrl); 27 | } 28 | 29 | @Override 30 | public void searchFor(String searchTerm) { 31 | driver.findElement(By.id("hc_popnow")); //wait for the trending stories - js has done loading by this point 32 | driver.findElement(By.id("sb_form_q")).sendKeys(searchTerm); 33 | driver.findElement(By.id("sbBtn")).click(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/java/com/natritmeyer/examplecucumberjvmspringframework/browsers/MobileBrowser.java: -------------------------------------------------------------------------------- 1 | package com.natritmeyer.examplecucumberjvmspringframework.browsers; 2 | 3 | import org.openqa.selenium.Dimension; 4 | import org.openqa.selenium.WebDriver; 5 | import org.openqa.selenium.chrome.ChromeDriver; 6 | import org.openqa.selenium.chrome.ChromeOptions; 7 | import org.springframework.beans.factory.annotation.Value; 8 | import org.springframework.context.annotation.Bean; 9 | import org.springframework.context.annotation.Configuration; 10 | import org.springframework.context.annotation.Profile; 11 | 12 | import java.util.concurrent.TimeUnit; 13 | 14 | @Configuration 15 | public class MobileBrowser { 16 | @Value("${browser.mobile.width}") 17 | private int mobileWidth; 18 | 19 | @Value("${browser.mobile.height}") 20 | private int mobileHeight; 21 | 22 | @Value("${browser.mobile.useragent}") 23 | private String userAgent; 24 | 25 | @Bean 26 | @Profile("mobile") 27 | public WebDriver getDriver() { 28 | String userAgentString = String.format("--user-agent=%s", userAgent); 29 | 30 | ChromeOptions chromeOptions = new ChromeOptions(); 31 | chromeOptions.addArguments(userAgentString); 32 | 33 | WebDriver driver = new ChromeDriver(chromeOptions); 34 | driver.manage().window().setSize(new Dimension(mobileWidth, mobileHeight)); 35 | driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 36 | return driver; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.natritmeyer 8 | examplecucumberjvmdependencyinjection 9 | 1.0-SNAPSHOT 10 | 11 | 12 | UTF-8 13 | 14 | 15 | 16 | 17 | 18 | io.cucumber 19 | cucumber-junit 20 | 5.7.0 21 | test 22 | 23 | 24 | io.cucumber 25 | cucumber-java8 26 | 5.7.0 27 | test 28 | 29 | 30 | io.cucumber 31 | cucumber-spring 32 | 5.7.0 33 | test 34 | 35 | 36 | 37 | 38 | org.seleniumhq.selenium 39 | selenium-java 40 | 3.141.59 41 | test 42 | 43 | 44 | 45 | 46 | org.springframework 47 | spring-context 48 | 5.2.6.RELEASE 49 | test 50 | 51 | 52 | org.springframework 53 | spring-test 54 | 5.2.6.RELEASE 55 | provided 56 | 57 | 58 | 59 | 60 | org.assertj 61 | assertj-core 62 | 3.11.1 63 | test 64 | 65 | 66 | 67 | 68 | 69 | 70 | org.apache.maven.plugins 71 | maven-compiler-plugin 72 | 3.8.1 73 | 74 | 1.8 75 | 1.8 76 | 77 | 78 | 79 | org.apache.maven.plugins 80 | maven-failsafe-plugin 81 | 2.22.2 82 | 83 | false 84 | 85 | 86 | 87 | 88 | integration-test 89 | verify 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cucumber-JVM Dependency Injection with Spring 2 | 3 | ## UPDATE: 4 | 5 | The way this repo used to demonstrate how to set up dependency injection for cucumber 6 | using spring [has been deprecated](https://github.com/cucumber/cucumber-jvm/pull/1940). 7 | You can still find the old approach demonstrated in this repo's `cucumber_xml` branch; 8 | I've updated `master` to reflect the new way of doing it. 9 | 10 | --- 11 | 12 | This repo accompanies [this](https://natritmeyer.com/howto/cucumber-jvm-dependency-injection-with-spring/) blog post and is an example maven project demonstrating how to use Spring's dependency 13 | injection with Cucumber-JVM in a test automation framework. 14 | 15 | ## Intro 16 | 17 | The purpose of this repo is to demonstrate how a cucumber scenario can be written in such a way as to be implementation 18 | agnostic, achieving this using dependency injection. Depending on one system property the tests will either run the 19 | mobile implementation of the application under test, or the desktop implementation. 20 | 21 | ## The Scenario 22 | 23 | It's pretty simple: webdriver loads [Bing](https://www.bing.com), searches for "cucumber", and verifies that the 24 | first hit in the search results contains the word "Cucumber". 25 | 26 | ## The dependency injection bit 27 | 28 | The project is set up such that Page Objects are implementation specific, i.e. there's a `MobileBingHomePage` and a 29 | `DesktopBingHomePage`, but both implement a single `BingHomePage` interface. The decision between which implementation 30 | is used is determined at runtime through the use of spring [Profiles](https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-definition-profiles-java). 31 | If the `-Dspring.profiles.active` command line argument is set to `desktop` then the step definition class will be injected 32 | with `DesktopBingHomePage`, or, if it's set to `mobile` then `MobileBingHomePage` will be injected. 33 | 34 | The same happens with the Page Objects themselves. They all need a WebDriver instance so one is injected in. Depending, 35 | again, on which spring profile is set, the Page Object will be injected with a WebDriver instance that is either set up 36 | with "desktop" dimensions, or with "mobile" dimensions and appropriate user-agent. 37 | 38 | That's about the sum of it. 39 | 40 | Note, this is just one way to arrange things. This repo is to demonstrate how all the pieces work together, not their 41 | optimal arrangement - that'll be determined by your own unique requirements. 42 | 43 | ## Usage 44 | 45 | To run the feature with a "desktop browser": 46 | 47 | ```sh 48 | mvn clean verify -Dspring.profiles.active=desktop 49 | ``` 50 | 51 | To run the feature with a "mobile browser": 52 | 53 | ```sh 54 | mvn clean verify -Dspring.profiles.active=mobile 55 | ``` 56 | 57 | ## Tested with... 58 | 59 | On 14th Jan 2019, with the following setup, It Worked On My Machine: 60 | 61 | * Macbook Pro 13" 2018 62 | * macOS 10.14.2 (18C54) 63 | * Maven 3.6.0 64 | * Java openjdk 11.0.1 2018-10-16 65 | * Chrome for Mac 71.0.3578.98 (Official Build) (64-bit) 66 | * ChromeDriver 2.45.615355 67 | 68 | Assumes: 69 | 70 | * No proxy required 71 | * No corporate SSL MiTM in place 72 | * Microsoft haven't tinkered with [bing.com](https://www.bing.com) 73 | --------------------------------------------------------------------------------