PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label cucumber. Show all posts
Showing posts with label cucumber. Show all posts

Tuesday, September 20, 2022

[FIXED] How to match a parameter value which contains ("${") using Cucumber DataTable in Java

 September 20, 2022     cucumber, datatable, hashmap, java, rest-assured     No comments   

Issue

I have the below Cucumber scenario with data table. I want to set query parameters in REST Assured framework. Here we have key= at and value=${atToken} which I am trying to get runtime value instead of passing hardcoded value through data table. In the below setParams method, I am trying to match the ${atToken} using param.contains("${"), but I am not able to get the desired result. Please let me know what I need to change in this for loop.

for (String param : params.keySet()) {
    if (param.contains("${")) {
        params.replace(param, RUN_TIME_DATA.get(param));
    }
}

Feature file:

  And I set query parameters
  | location     | NY         |
  | at           | ${atToken} |

Stepdefintion:

@And("set {} parameters")
public void set_query_parameters(String query, DataTable params) throws IOException {
    POC.setParams(query, params.asMap());
}

Utils file:

public void setParams (String type, Map < String, String > params) throws IOException {
    for (String param : params.keySet()) {
        if (param.contains("${")) {
            params.replace(param, RUN_TIME_DATA.get(param));
        }
    }
    switch (type.toLowerCase()) {
        case "form":
            request.formParams(params);
            break;
        case "query":
            request.queryParams(params);
            break;
        case "path":
            request.pathParams(params);
            break;

    }
}

Hooks file:

@Before
public void beforeScenario() throws Exception {
    RUN_TIME_DATA.put("atToken", POC.createAuth());
}

Solution

If I understood correctly your quetion, there are a few issues with your implementation.

First of all, you have a gherkin instruction as And I set query parameters but the step definition is @And("set {} parameters")

As you defined "query" in your step definition, I assume that you have a scenario like this.

Scenario Outline: Params scenario
Given ...
When ...
Then ...
    And I set "<queryType>" parameters
      | location | NY         |
      | at       | ${atToken} |
    Examples:
      | queryType |
      | form      |
      | path      |

Second problem is that params.asMap() does not create a mutable map but your setParams method will need a mutable one.

@And("I set {string} parameters")
public void set_query_parameters(String queryType, DataTable params) throws IOException {
    // POC.setParams(queryType, params.asMap()); // not mutable map
    POC.setParams(queryType, new HashMap(params.asMap()));
}

The main issue in your loop was that you were searching $ symbol in key instead of value.

public void setParams (String type, Map<String, String> params) throws IOException {
      for (Map.Entry<String, String> entry : params.entrySet()) {
         final String key = entry.getKey();
         final String value = entry.getValue();

         // if value is parameterized
         if (value.contains("${")) {
            // you are using stripped value as key in your RUN_TIME_DATA
            final String keyInRuntimeMap = getParamKeyFrom(value);                      
            params.replace(key, RUN_TIME_DATA.get(keyInRuntimeMap));
         }
      }

      switch (type.toLowerCase()) {
         case "form":
            request.formParams(params);
            break;
         case "query":
            request.queryParams(params);
            break;
         case "path":
            request.pathParams(params);
            break;
        // what should happen if unknown type given
         default:
            throw new IllegalArgumentException("unknown type=" + type);
      }
   }

     /**
    * Will strip someKey from ${someKey}
    *
    * @param text
    * @return stripped text or the original text
    */
   private String getParamKeyFrom(final String text) {
      // pattern to match -> ${someKey}
      final Pattern pattern = Pattern.compile("\\$\\{(.*)\\}");
      final Matcher matcher = pattern.matcher(text);

      if (matcher.find()) {
         return matcher.group(1);
      }

      // if pattern does not match return the same string
      return text;
   }

Hope it helps.



Answered By - ocos
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, April 21, 2022

[FIXED] How to use javascript to run automated tests in browser using selenium+CUCUMBER

 April 21, 2022     connection, cucumber, javascript, selenium, testing     No comments   

Issue

Well, I am kinda new to this. First of all, my main goal is to execute a simple cucumber example which tests automatically something extremely simple as well.By doing this I will try to get the idea of how should i do other kind of autmated test. So, I wrote some scenarios, and I want to test them somehow on a site(e.g. google.com). The site is written in JS and therefore I need to write JavaScript code to "connect" the scenarios with the language.

I google searched things like: "How to automatically test a site using cucumber" "How to automatically run scenarios with selenium-javascript" and so on...

Any ideas? No hatefull comments please :/ Thanks in advance!

DL.


Solution

I wrote some scenarios,

When you say that I believe you are able to execute your testcases with cucumber

The site is written in JS and therefore I need to write JavaScript code to "connect" the scenarios with the language.

thats not necessary, if you are site is based on javascript like AngularJS you can still use simple java + selenium but protractor is recommended for same as it have wrapper. protractor is a nodejs based project to deal with sites based on AngularJS.

https://www.protractortest.org/#/

How to automatically test a site using cucumber

You can use a CI/CD tool like jenkins which you can trigger manually or you can put an scheduler who will run all your test script against your website on it. You can also turn on the notification so when ever the test complete it will shoot an email to respective individuals

Refer:

https://jenkins.io/

You can get n number of tutorial regarding same. example:

Click Here



Answered By - Shubham Jain
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing