0

There is a test.properties file from where i need to fetch profile then based on profile I need to execute cucumber cases. For "WebDriver driver", @Autowired has used in LoginSteps class but driver object is getting null value when I execute the case.

IMAGE1

1 Answers1

0

Adding the cucumber-spring dependency. In order to use Spring with Cucumber, we need to add a cucumber-spring dependency to our pom.xml:

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>2.4.0</version>
    <scope>test</scope>
</dependency>

XML configuration

Alternatively, you can specify your beans in a .cucumber.xml file, like this. Here is an example (again using Selenium WebDriver; this could also be one of your own classes):

<bean class="org.openqa.selenium.WebDriver" scope="cucumber-glue" />

You can annotate your StepDefinitions with @ContextConfiguration("classpath:cucumber.xml").

Using the Beans You can now use the Beans, by autowiring them where you need them.

For example, you can Autowire your WebDriver to your PageObject:

import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
public class PageObject {
    @Autowired
    WebDriver driver;
    // the rest of your page object
}

More details here: https://medium.com/@mlvandijk/managing-state-in-cucumber-jvm-using-spring-a795e9a1dd18

Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
  • Thanks Mebin for your comments. I am using gradle, If i use cucumber-spring2.4.0 then i am getting error "can't fine cucumber" in all stepdefination class. cucumber-spring1.2.4 doesn't give any error . 1. What changes I need to make in cucumber.xml file ? – S Mahapatra Apr 17 '19 at 07:35
  • @SMahapatra Just add bean definition in xml like `` and check you are getting the same when Autowiring. – Mebin Joe Apr 17 '19 at 07:38
  • I am getting " nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.openqa.selenium.WebDriver" error – S Mahapatra Apr 17 '19 at 08:44
  • @SMahapatra Do you have `` – Mebin Joe Apr 17 '19 at 09:46
  • I have used as shown in "Image1" but still I am getting error can't able to autowired webdriver. Thanks a lot for your effort. I want to fetch data from test.properties file then want to use in stepDefination class – S Mahapatra Apr 17 '19 at 11:44
  • @SMahapatra We are missing something. I assume you are using WebDriver for firefox. As a workaround we could try `WebDriver driver = new FirefoxDriver();` rather than injecting bean/Autowiring. – Mebin Joe Apr 17 '19 at 12:09
  • Yes we have this workaround but this is how exciting cases were written and which are not in executable state so I am working on it to make them executable. – S Mahapatra Apr 17 '19 at 12:41
  • @SMahapatra It's better to use latest/stable versions of cucumber-spring. Once you are done with that you would be able to do things easily like most of the configuration will be predefined and use Java configurations when possible. – Mebin Joe Apr 17 '19 at 12:45
  • So sorry It didn't work out. https://stackoverflow.com/questions/35320857/selenium-and-cucumber-proxy-setting-cucumber-xml-or-cucumberrunner/55779551#55779551 this is the same problem I am getting. – S Mahapatra Apr 30 '19 at 09:36