Make step definition dynamic to handle any incoming text

745 Views Asked by At

I have to run test1.feature file against two urls. In one of the url I have got a field named as EIN, but in second url the same field named as ABN How can we make the step definition dynamic to handle any text coming in second string.

Url 1 : https://testsite.us.com/student

The Field is named as "EIN"

Url 2: https://testsite.au.com/student

The Field is named as "ABN"

test1.feature

And I type "11 234 234 444" in "ABN" field

step_definition

//Type text value in textbox input field

Then('I type {string} in {string} field', (textval, textboxname) => {
  switch (textboxname) {
    case "Email":
    case "Work Phone":
    case "ABN":
    case "EIN":
    case "ACN":
      cy.get('#profile_container').parent().find('.fieldHeaderClass').contains(textboxname)
        .next().find('input')
        .clear({force:true})
        .type(textval, { force: true });
      break;
      
    //final else condition
    default:
      cy.get('#profile_container').parent().find('.fieldHeaderClass').contains(textboxname)
        .next()
        .type(textval, { force: true });

  }
});
1

There are 1 best solutions below

0
On BEST ANSWER

First of all a great tip: Make use of page objects (POM design pattern). A page objects is an object (inside a third .js file besides your features and stepdifinitions) that you import in the stepdefinitions file that contains all of your selector code (cy.get(......)). You don't want selector code in your stepdefinitions. Makes it messy and more difficult to read.

Concerning your problem: If you want to repeat the same logic for (all kinds of) input values. Why not write your logic just ones (so without the long case statement) and use Scenario Outline to repeat your step for the different inputs? If your logic must be different every time, than don't even bother fixing this problem. Then you should simply write different steps for different logic..

Here is an example of scenario outline (inside a .feature):

Scenario Outline: Scenario Outline name  
    Given I type "<textval>" in "<textboxname>" field
     Examples:
      | textval            | textboxname |
      | some_value         | ABN         |
      | some_other_value   | EIN         |
      | some_other_value_2 | email       |
      | some_other_value_3 | ACN         |
      | some_other_value_4 | Work Phone  |

Conclusion: Use Scenario Outline to repeat logic. If logic needs to be different for different inputs, then write another stepdefinition and don't try to combine and clip different logics into 1 step.