简体   繁体   中英

Specflow Visual studio C# - How to IWebdriver resume the execution of another scenario on a new window

Hello I am very new to coding and specflow.

In my Feature File I have two scenarios. After Scenario A is run the window is closed and Scenario B is run on another window. However when Scenario B is being executed it is not resuming where Scenario A left.

I would like to how how to make the Scenario B to resume where Scenario A has left.

I have created a class which stores all my elements(Iwebelements.)

Here's my Feature File

@navigation
Scenario: Login 
Given user is in Main Page
And user selects menu to navigate to User Schedule
And User Schedule page is displayed

@FORM
Scenario: Verify data can be entered on the form
When user click on the calendar icon 
Then a calendar should appear
And user should be able to select any date and year
When user click on the field Time  
Then a dropdown list id displayed
And user should select a Time

Here's my Step Definition

public class FillForm
    {

    private DriverHelper _driverHelper;
    Userpage UPage;
    LoginSteps LogSteps;
    LoginPage LogPage;

    

    public FillForm(DriverHelper driverHelper)
    {
        _driverHelper = driverHelper;
        UPage= new UserPage(driverHelper.WebDriver);
        LogPage = new LoginPage(driverHelper.WebDriver);

    }

    [Given(@"user is in  Main Page")]
    public void GivenUserIsInMainPage()
    {

     

        _driverHelper.WebDriver.Navigate().GoToUrl("www.randomsite.com");
        
 _driverHelper.WebDriver.FindElement(By.XPath("//input[@id='username']")).SendKeys("Test123");
            _driverHelper.WebDriver.FindElement(By.Id("Password")).SendKeys("P@ssword!");
            LogPage.ClickSignUp();
            Thread.Sleep(5000);
    }
    
    [Given(@"user selects menu to navigate to User Schedule")]
    public void GivenUserSelectsMenuToNavigateToUserSchedule()
    {
        UPage.ClickMenu();
        Thread.Sleep(5000);
        UPage.ClickSolution();
        Thread.Sleep(5000);
        UPage.ClickPM();
        Thread.Sleep(5000);
        UPage.ClickonForm(); 
        Thread.Sleep(6000);
    }
    
    [Given(@"User Schedule page is displayed")] 
    public void GivenUserSchedulePageIsDisplayed()
    {
        Assert.That(Upage.SyncNowbtnIsDisplayed(),Is.True);
       
    }

    [When(@"user click on the calendar icon")] //This is where scenario B starts
    public void WhenUserClickOnTheCalendarIcon()
    {
        
    }

This is my Hooks

[Binding]
public sealed class Hooks
{
    
    private DriverHelper _driverHelper;

    public Hooks(DriverHelper driverHelper) => _driverHelper = driverHelper;

    [BeforeScenario]
    public void BeforeScenario()
    {
        ChromeOptions option = new ChromeOptions();
        option.AddArguments("--start-maximized");
        option.AddArguments("--disable-gpu");
        
        _driverHelper.WebDriver = new ChromeDriver(option);
        
    }

    [AfterScenario]
    public void AfterScenario()
    {
        _driverHelper.WebDriver.Quit();
    }

That's my class Driverhelper where I set the IWebDriver

public class DriverHelper
{
    public IWebDriver WebDriver { get; set; }
}

This is a misunderstanding about how behavior driven development works. Each scenario should be atomic. It should be completely executable on its own, and not rely on any other scenario. The trick is to create additional Given steps that simulate what a previous scenario did. Logging in is a prerequisite for your second scenario. Logging in is the behavior being tested in the first scenario. Each deserves its own list of steps:

@navigation
Scenario: Login 
    Given user is in Main Page
    When user selects menu to navigate to User Schedule
    Then User Schedule page is displayed

Some recommended changes to your first scenario include properly using the When and Then keywords to describe the action and assertion. You can reuse the same step definition for multiple steps, since Given user selects menu to navigate to User Schedule could be a "given" or a "when" depending on the context of the current scenario. I would recommend converting ... User Schedule page is displayed to a "Then" step, because it is making an assertion. Assertions should be a "Then" step.

[Given(@"user selects menu to navigate to User Schedule")]
[When(@"user selects menu to navigate to User Schedule")]
public void GivenUserSelectsMenuToNavigateToUserSchedule()
{
    ...
}

[Then(@"User Schedule page is displayed")] 
public void GivenUserSchedulePageIsDisplayed()
{
    ...
}

The second scenario becomes:

@FORM
Scenario: Verify data can be entered on the form
    Given user is in Main Page
    And user selects menu to navigate to User Schedule
    When user click on the calendar icon 
    Then a calendar should appear
    And user should be able to select any date and year
    When user click on the field Time  
    Then a dropdown list id displayed
    And user should select a Time

Completely eliminating duplicate steps in scenarios is not actually desirable. Scenarios, and behavior driven development, are first concerned with communication. While duplicating steps seems like a bad idea, it still communicates the full scope of that particular use-case. Duplication of steps is more forgivable than duplication of C# code.

If you are duplicating a large number of steps in each scenario, consider refactoring the common steps into a scenario background can eliminate duplicate steps:

Feature: ...

Background:
    Given common step 1
    And common step 2
    ...
    And common step N

Scenario: 1
    ...

Scenario: 2
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM