iceblender
iceblender

Reputation: 59

"The selected node does not have a form ancestor"

I'm testing my PHP / bootstrap application with Behat + Mink. I'm currently trying to use variables to grab from a field with a Scenario Outline.

The website looks like this enter image description here

My code looks like this in feature file Feature: Stock Portfolio In order to see what stocks are owned As a user I want to display my portfolio

    Scenario: User Is On Home Page
        Given that I am logged in
        When print current URL
        Then I should see "Portfolio"


    Scenario Outline: User Buys Stock
        Given that I am logged in
        And I own <start> "AAPL" stocks
        When I fill in "ticker" with "AAPL"
        And I sell <sell> stock
        Then I should own <finish> "AAPL" stocks

        Examples:
            | start | sell | finish |
            |  13   |  5   |  8     |
            |  10   |  3   |  7     |

in php file

 /**
     * @Then /^I should own (.*) "([^"]*)" stocks$/
     */
    public function iShouldOwnStocks($start, $arg1)
    {
        $this->assertSession()->fieldValueEquals($arg1, $start);
    }



    /**
     * @Given /^I sell (.*) stock$/
     */
    public function iSellStock($sell)

    {
        $this->fillField("quantity", $sell);
    }

    /**
     * @Given /^I own (.*) "([^"]*)" stocks$/
     */
    public function iOwnStocks($start, $arg1)
    {
        $this->assertSession()->fieldValueEquals($arg1, $start);

    }

The error I keep getting is:

Error

I'm not quite sure what's going on here - help would be super appreciated!

Upvotes: 0

Views: 515

Answers (1)

kfairns
kfairns

Reputation: 3067

First, about the regex:

Do not use (.*) unless it is at the end of a step definition, as a helpful hint for you, or other developers bug fixing later on. This regex literally means, accept any character except a new line.

Instead, use ([0-9]+), which will only accept numbers, or ([\d]+), which again, will only accept numbers, and will only accept them if there is 1 or more.

Secondly, about the regex:

Try to also encapsulate the number using double quotes, "([0-9]+)" or "([\d]+)", which should do the same as above, but makes it slightly more clear, by only allowing numbers in quotes.

Penultimately, about the regex

If you are still having trouble with the digits, because of a decimal place, use this:

"([\d])*\.?([\d]){0,2}"

Which will capture any amount of numbers before, an optional decimal place, and up to two numbers after.

Lastly, about duplication of functions

You assert the same thing twice in two separate step definitions. Personally, I would change the

/**
 * @Then /^I should own (.*) "([^"]*)" stocks$/
 */
public function iShouldOwnStocks($start, $arg1)
{
    $this->assertSession()->fieldValueEquals($arg1, $start);
}

function to

/**
 * @Then /^I (?:|should )own "([0-9]+)" "([^"]*)" stocks$/
 */
public function iShouldOwnStocks($start, $arg1)
{
    $this->assertSession()->fieldValueEquals($arg1, $start);
}

Which will make the "should" optional, meaning no duplication of the same assertion needed, making your context less messy.

Basically, you will no longer need this:

/**
 * @Given /^I own (.*) "([^"]*)" stocks$/
 */
public function iOwnStocks($start, $arg1)
{
    $this->assertSession()->fieldValueEquals($arg1, $start);

}

If there is still a problem after the regex fixes I have suggested, leave a comment, and I will attempt to answer it in a new paragraph.

Upvotes: 1

Related Questions