0

I am currently using Specflow with Selenium and FluentAutomation, and am running into significant problems with maintaining state between steps in specflow.

See my example code below:

[Binding]
public class RegistrationSteps : FluentTest
{

    [Given(@"I create an account")]
    public void GivenICreateAnAccount()
    {
        new HomePage(this)
            .Go()
            .StartRegistration()
            .EnterDetailsAndClickSubmit(); // takes me to deposit page
    }

    [When(@"Deposit '(.*)' dollars in my account")]
    public void GivenDepositMoneyInMyAccount(int amount)
    {
        new DepositPage(this)
            .EnterDetailsAndClickSubmit(amount);
    }
}

My problem is:

  • In the first step the page is loaded using Go() and everything happens fine
  • In the second step my tests continue, here I expect I am on a different page, based in the Submit in the previous
  • Because I am no on a different PageObject it gets confused, I don't use Go because the previous step shouldve brought me here, and at this stage it wont find the expected elements

So my question is, how can I use one browser session and several PageObjects across multiple Specflow tests?

shenku
  • 11,969
  • 12
  • 64
  • 118
  • You have a typo. You said'no' when i think you mean 'now' or 'not'. Also What do you mean by 'it gets confused'? Is there an error? What exactly is the problem? Are things just happening too quickly? Where are you waiting for the second page to load? – Sam Holder Sep 02 '15 at 08:34

1 Answers1

0

According to the FluentAutomation doc, you should do something like this:

[Binding]
public class RegistrationSteps : FluentTest
{
    private PageObject _currentPage;

    [Given(@"I create an account")]
    public void GivenICreateAnAccount()
    {
        _currentPage = new HomePage(this)
            .Go()
            .StartRegistration()
            .EnterDetailsAndClickSubmit(); // takes me to deposit page
    }

    [When(@"Deposit '(.*)' dollars in my account")]
    public void GivenDepositMoneyInMyAccount(int amount)
    {
        _currentPage = _currentPage
            .EnterDetailsAndClickSubmit(amount);
    }
}

Provided that you return the page object that is switched to in the EnterDetailsAndClickSubmit method of your concrete page object like:

Public PageObject EnterDetailsAndClickSubmit() {

    // [.. enter details here and click submit ..]
    return this.Switch();
}
AutomatedChaos
  • 7,267
  • 2
  • 27
  • 47
  • the problem is, when that switch changes the type of page object (which it generally does) then casting to a base PageObject, you would lose all the method information and not be able to continue. – shenku Sep 02 '15 at 22:06
  • Well, you can cast it to the correct type like: `var depositPage = _currentPage as DepositPage; _currentPage = depositPage.EnterDetailsAndClickSubmit(amount);`. With explicit stating the type it also shows intention. – AutomatedChaos Sep 03 '15 at 05:25