1

In our Application, We have webservices which are communicating with the other sub systems We are implementing cucumber BDD framework for testing. We have cucumber feature file, Step def file and Runner class. Currently, in development environment Test cases are running successfully and Test report got generated in target folder. We have used Java along with spring boot to build the project When we are trying to configure the cucumber framework with different environments we are facing difficulty to get the cofig.properties details. Application URL :http://127.0.0.1:8783//plan/apr For different environments we are giving the ipaddress and port as follows DNA : 172.34.56.89:61142

Could you please help us to configure the cucumber test cases in different environments

config.properties file

#DEV DEV.ENV=http://127.0.0.1:8476/plan/apr

#DNA DNA.ENV=http://172.25.193.330:62251/plan/apr

#Model E ModelE.ENV=http://172.25.343.210:62342/plan/apr

#Model B ModelB.ENV=http://172.25.123.234:61234/plan/apr

Properties class :

public class RetriveCucumberProperties { public ExternalUrlPropKeys propKeys;

String baseUrl;
protected static final String URL = ".url";


@Bean
public String RetriveCucumberProperties() {
    
    FeatureFilePropertiesReader featureFilePropertiesReader = SpringContext
            .getBean(FeatureFilePropertiesReader.class);

    if (URL.equals("DEV.ENV")) {
        Properties url = featureFilePropertiesReader.getSubset(URL);
        baseUrl = url.getProperty(URL);
    } else if (URL.equals("DNA.ENV")) {
        Properties url = featureFilePropertiesReader.getSubset(URL);
        baseUrl = url.getProperty(URL);
    } else if (URL.equals("ModelE.ENV")) {
        Properties url = featureFilePropertiesReader.getSubset(URL);
        baseUrl = url.getProperty(URL);
    } else if (URL.equals("ModelB.ENV")) {
        Properties url = featureFilePropertiesReader.getSubset(URL);
        baseUrl = url.getProperty(URL);
    }
    return baseUrl;
}

}

Step def class :

@Given("the user is on the login page") public void the_user_is_on_the_login_page() throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();
         JsonNode jsonNode = objectMapper.readTree(new File(".\\JSON\\Cardplanning.json"));
         String user = jsonNode.get("LOGIN").get("LOGIN_URL").asText();
        
    }
    @SuppressWarnings("deprecation")
    @When("the user enters valid  {string} and {string}")
    public void the_user_enters_valid_and(String username, String password) throws IOException {
       
        RestAssured.baseURI = retriveCucumberProperties.RetriveCucumberProperties()+"/login";
        RequestSpecification http = RestAssured.given();
      

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(new File(".\\JSON\\Cardplanning.json"));
         String user = jsonNode.get("LOGIN").get("username").asText();
         String pass = jsonNode.get("LOGIN").get("password").asText();
         
         http.header("Content-Type" , "application/json");
         http.body(jsonNode.toString());
         Response response = http.request(Method.POST,"/login");
         String resposebj = response.getBody().asString();
         ResponseBody body = response.body();
         String bodyobj = body.asString();
         Assert.assertEquals(bodyobj.contains(username), false);
         Assert.assertEquals(bodyobj.contains(password), false);
        
         int statuscode = response.getStatusCode();
         Assert.assertEquals(statuscode, 404);  
    }
    

I have tried the way you have explained but unable to to retrive the URL.

  1. How I can get URL in step def file according to the selection is URL based on env
  2. How I can integrate with maven properties, so that i can test from the maven commands like mvn test -Dtest=dev

Could you please help on this.

1 Answer 1

0

To configure the Cucumber test cases in different environments, you need to create a separate properties file for each environment. These properties files should contain environment-specific details like the IP address and port number of the server, database credentials, etc. You can then load the appropriate properties file based on the environment.

Here is an example of how to load properties files in Spring Boot:

  1. Create a folder called config in your project's root directory.

  2. Create a separate properties file for each environment in the config folder. For example, dev.properties, qa.properties, prod.properties, etc.

  3. Each properties file should contain environment-specific details like the IP address and port number of the server, database credentials, etc. Load the appropriate properties file based on the environment in your Spring Boot application. You can do this by creating a @Configuration class with a @Bean method that returns a Properties object. Here's an example:

    @Configuration public class AppConfig {

     @Value("${env}")
     private String environment;
    
     @Bean
     public Properties properties() throws IOException {
         Properties props = new Properties();
         String propsFileName = environment + ".properties";
         InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propsFileName);
    
         if (inputStream != null) {
             props.load(inputStream);
         } else {
             throw new FileNotFoundException("Property file '" + propsFileName + "' not found in the classpath.");
         }
    
         return props;
     }
    

    }

In this example, we're using a @Value annotation to inject the environment variable into the environment variable. We're then using this variable to load the appropriate properties file. The properties() method returns a Properties object that contains the environment-specific details.

  1. In your step definition file, you can then use the @Autowired annotation to inject the Properties object and retrieve the environment-specific details. Here's an example:

    @Component public class MyStepDefs {

     @Autowired
     private Properties properties;
    
     @Given("^I am on the home page$")
     public void i_am_on_the_home_page() {
         String ipAddress = properties.getProperty("ipAddress");
         int portNumber = Integer.parseInt(properties.getProperty("portNumber"));
    
         // Use the IP address and port number to connect to the server
     }
    

    }

In this example, we're using the @Autowired annotation to inject the Properties object into the step definition class. We're then using the getProperty() method to retrieve the environment-specific details.

References:

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.