Search code examples
c#seleniumautomationspecflow

Running specific profile from command line in Specflow C#


I've been working quite a lot with PHP Behat and I'm trying to emulate the same thing in C# with specflow. One of the issues I'm running into is that my I want to use the same test for multiple (50+)websites, basically pushing an argument in command line and telling it which website to go to.
In Behat I'd use Behat.yml, set multiple profiles with a format similar to this:

websiteA: url: whatever.com websiteB: url: blabla.com

And then specify in command line the profile I want "behat -p websiteA" and based on that I have a function that uses the url for my specific website.

I'm pretty new to everything C# and specflow so I am wondering how could I have this solution in specflow. So far I managed to create a basic scenario that I run with "dotnet test" in command line.

Also I'm using selenium.

thanks a lot!


Solution

  • It's not quite the solution you've requested (i.e. not using specflow profiles) but as a general c# approach to toggling websites you can use the build configuration options.

    In Build > Configuration Manager you can create new profiles for each environment/URL. Expand out the Active solution configutations and you can create new:

    Build Configuration Manager

    You say you have 50 websites, and as this is just a driver, the same information can be controlled by editing your solution (.sln) file:

    GlobalSection(SolutionConfigurationPlatforms) = preSolution
            Debug|Any CPU = Debug|Any CPU
            Demo|Any CPU = Demo|Any CPU
            Dev|Any CPU = Dev|Any CPU
            Prod|Any CPU = Prod|Any CPU
                *...etc*
        EndGlobalSection
        GlobalSection(ProjectConfigurationPlatforms) = postSolution
            {C634C2D6-7FB9-4B47-923F-9E8634843928}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
            {C634C2D6-7FB9-4B47-923F-9E8634843928}.Debug|Any CPU.Build.0 = Debug|Any CPU
            {C634C2D6-7FB9-4B47-923F-9E8634843928}.Demo|Any CPU.ActiveCfg = Demo|Any CPU
              *...etc*
    

    The GUID is a reference to the project in the solution. It's all copy+paste-able.

    Once those are added you can create code to read and review the runtime settings to create/set variables as you need.

    Our approach (and i'm sure there are others) was to create a powershell script which would take that Configuration name and build our settings file as a pre-build event for the project: pre-build settings

    You can see the input into the powershell is $(ConfigurationName). The content of this scripts is limited by your imagination :-)

    For us, this was especially useful as we had to get passwords and other API keys from our secret manager per environment. It would create a local .json file with everything we needed to run our tests. The framework would then only ever need to know about a single file and it worked just as well running in CI pipes.

    Finally - when it comes to running dotnet test, you use -c and pass it the build you want (options are here):

    -c|--configuration <CONFIGURATION>

    Defines the build configuration. The default value is Debug, but your project's configuration could override this default SDK setting.