BDD Multi Dependent Script

I am using SpecFlow using .Net to automate the user interface. I defined functions and scripts for them. The My Question script depends on other scripts, before I run the main scripts, which I must ensure that all dependent data that is defined as a script in the same properties file is created first, so I put all the ones that are in the background . So, when am I going to run the following function, which also depends on the same scripts that we already created with Feature 1st, which is already created or not? therefore, we do not need to do the same thing again.

So, is there a way to guarantee that before running any script, background scripts are already running / created / present at the user interface level.

+4
source share
2 answers

It sounds as if you want to have a common background in several different function files. There are at least two different ways to do this:

Composite steps

One way to do this is what AlSki suggested, creating a complex step that calls for many other steps. I will not review this here because he did an excellent job with the answer. You can call this composite step (a step that calls many steps) from any script (or background) in any function that you like.

Hook tag filtering

Another approach is to use the Hooks defined in SpecFlow, for example [BeforeScenario] to run code before each script. You can use tag filtering (see Link for interceptors) to specify which launch to launch by adding the appropriate tag to the script or function. Let me show you an example:

Say I have scripts that run using Selenium to control a web browser, but not all of my scripts use Selenium. If I wanted to configure Selenium only for scripts that need it, I can create a BeforeScenario binding that only runs if the script has an @web tag.

Here is my LoggingIn.feature file:

 Feature: Logging In @web Scenario: Log In Given I am on the login page When I supply valid credentials Then I should be taken to the homepage 

Here is my StepDefinitions.cs step definition file:

 [Binding] public class StepDefinitions { [BeforeScenario("web")] public static void BeforeWebScenario() { // Code to startup selenium } [BeforeScenario] public static void BeforeAllScenarios() { // Code that executes before every scenario...regardless of tag. } } 

For any script that has an @web tag, both BeforeAllScenarios() and BeforeWebScenario() will be executed before the script starts. For a script that does not have an @web tag, only the BeforeAllScenarios() method will be executed.

That way, you can simply run the code set by applying a specific attribute to the script.

FYI: Starting with SpecFlow 1.9, you cannot specify the order in which these hooks are executed if more than one is specified.

When to use this or that?

It depends on whether you fix technical problems or business problems.

Use tags to configure technical issues

If you want to customize some technical aspect of your test that a business user should not know about, I would use a tag approach. A good example is the one I introduced ... the creation of Selenium. A business user can take care of what Selenium is, so it makes no sense to create a step in the script that sets it up.

Use complex steps to set up business tasks

If you need to indicate the state of the system that a business user is interested in (for example, a registered user or existing product data), this should be another step in the script (or in the background). This is because the steps should include everything you need to read and understand behavior from a business perspective.

+4
source

I think you are asking about domains here because you have a domain that you called the background, and now you want to enter another higher level domain called UI. Therefore, since I do not have enough information about your application, I will try to illustrate it with another example.

Imagine that we have a store, we can write scripts in many domains, such as inventory control, opening in the morning, changing and closing at night. This probably means we have a lot of bindings like WhenWeFillTheShelves() , WhenWeUnlockTheDoor() , WhenWeHaveAChangeInTheTill() .

Now we come to interact with customers, so we can write

 Given the shop is ready for business 

At this moment I will write

 [Binding] public void GivenTheShopIsReadyForBusiness() { WhenWeFillTheShelves(); WhenWeHaveChangeInTheTill(); WhenWeUnlockTheDoor(); } 

Thus, we reused our lower level, more boundary domains to create a test of a higher level domain, and you can guarantee that everything is in the correct state every time.

I also suggest you read Dan North Whose domain it's all the same

+2
source

Source: https://habr.com/ru/post/1438825/


All Articles