4

I need help please, I have a working Selenium Webdriver/Gherkin/Specflow project that I created.

I am trying to migrate my code to another environment (stage), but I only want to make changes in the app.config to switch between the different environments. I have successfully executed the URL switch with app.config using the 'Switch Cases', the problem I am facing now is how to create second login and how will gherkin knows to switch to the specific environment that I am running?

Currently I have my username and password store in gherkin like this:

Examples:
| firstname | username | password |
| Cam       | Newton   | 12345dev |

This is how I am performing my url switch:

switch (environment)
{
    case "DEV":

        if (!(_driver.Url.Equals(Constants.DEV_LandingPage)))
        {
            _driver.Navigate().GoToUrl(Constants.DEV_LandingPage);
        }
        break;
    case "STAGE":
        if (!(_driver.Url.Equals(Constants.STAGE_LandingPage)))
        {
            _driver.Navigate().GoToUrl(Constants.STAGE_LandingPage);
        }
        break;
}

2 Answers 2

1

You will have to change the environment based on the parameter from the Gherkin file. For example: Given The actor logs in the "environment" environment with "username" and "password".
I do not know how to escape tags in these posts, so change the " with tags.
Examples: | firstname | username | password | environment | | Cam | Newton | 12345dev | DEV | | Mac | Oldton | 12345stage | STAGE |
Then in the Steps file the implementation will be:

@Given("The actor logs in the ".*" environment with ".*" and ".*"") public void theGameIsRunning(string environment, string username, string password) { if(environment == "Dev") { string devUrl = ConfigurationManager.AppSettings["DevUrl"]; driver.Navigate().GoToUrl(devUrl); } else if (environment == "Stage") { string stageUrl = ConfigurationManager.AppSettings["StageUrl"]; driver.Navigate().GoToUrl(stageUrl); } }

Every test will be executed but on a different environment.

When you only want to test on Dev or Stage you will have to switch environments in the app.config.

1
  • Thanks a lot for your help Koen Meijer. I believe I can take it from here...
    – Geezus
    Commented Sep 19, 2017 at 17:54
1

Every Example in a Gherkin file will execute as a test.

So when you want to use every user credential you would also have to switch the environment via the Gherkin file. In this way every test will execute with the right credentials. So when you want to execute different logins from a gherkin file you also have to switch environments via the Gherkin file.

Then the implementation would be:
Examples: | firstname | username | password | environment | | Cam | Newton | 12345dev | DEV | | Mac | Oldton | 12345stage | STAGE |

But when you want to keep the responsibiliy of switching environments in the app.config, you also have to have the credentials in the app.config.

You can keep all the credentials for the two environments in the app.config.
When you are releasing the tests in different environments you can make the credentials dynamic. There are several methods for this, for instance by using tokens which values vary per environment.

1
  • Thanks Koen for the response. Ok I understand the part that you can have this implemented in the gherkin example table, but my problem is I don't know how to do the USER switch between 2 logins in the C# part of the code. Can you please give me an example on how to handle the switch in the C#? I saw an example online that uses 'For Each' but the example also uses "TableNode" which I don't know how to implement in C#
    – Geezus
    Commented Sep 15, 2017 at 16:09

Your Answer

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

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