I have been trying to inject webdriver
into the steps. I have used this instructions and it works well.
The idea is to inject WebDriver into steps classes as a service. At the initial step, you need to add the following dependency.
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-spring</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency>
There are three main classes which are involved in the dependency injection. Here we introduce them one by one.
BaseUtil
BaseUtil is the class which has an attribute for WebDriverof Selenium. The class is quite simple:
public class BaseUtil { private WebDriver driver; public WebDriver getDriver() {return driver;} public void setDriver(WebDriver driver) { this.driver = driver;} }
Hook
The Hook class contains @Before, @After
. The method testInitialier() is responsible to load the the webDriver file and create an instance, while the method testTearDown() is responsible for closing the browser.
public class Hook extends BaseUtil{ BaseUtil base; @Before public void testInitializer(){ File file = new File(IgniteTaskApplication.class.getClassLoader().getResource("driver/chromedriver.exe").getFile()); String driverPath=file.getAbsolutePath(); System.out.println("Webdriver is in path: "+driverPath); System.setProperty("webdriver.chrome.driver",driverPath); base.setDriver(new ChromeDriver()); } public Hook(BaseUtil base) { this.base = base; } @After public void tearDownTest(){ base.getDriver().close(); } }
Steps
And the steps class contains the steps which came from compiled features file. To compile the feature file in Eclipse you need to have Eclipse-Cucumber plugin installed in your Eclipse.
public class ClickButton_Steps extends BaseUtil{ BaseUtil base; public ClickButton_Steps(BaseUtil base){ super(); this.base=base; } @When("^I clcik on the button$") public void i_clcik_on_the_button() throws Throwable { cb=new ClickButtonPage(base.getDriver()); cb.navigator(); } // The other steps ... }
How do i run it?
Open the feature file -> Run as -> Run with Junit
Question
I am wondering what is the order of running methods in a way which it leads to dependency injection?
I guess the order is as following:
- Junit calls
@Before
method which istestInitializer()
- The
testInitializer()
is in Hook class so it needs to make an instance ofHook
class. - It leads to call the constuctor of the Hook class.
But, i cannot understand the rest of the steps. Maybe even it does not true at all. I mean, I have a functional code but i cannot explain how it works?
0 comments:
Post a Comment