user3746259
user3746259

Reputation: 1581

Behat Scenario Registration - not finding text

I have a very strange behaviour at a very basic scenario i've written. When not running the scenario with "@javascript" tag and selenium server, behat always fails and tells me that the specified text could not be found. But when I put @javascript, start selenium server and run the scenario again, everything passes. The site it is testing is just a basic registration site that gets submitted via post, and when registration is completed, the user gets redirected to "/register/success", where the message is displayed on the screen. When I do not run with selenium, the user does not even get saved in the database and the test fails due to missing text.:

@javascript // <-- only passing when adding this and starting selenium
Scenario: Fill in correct fields and register user
Given I am on "/register"
When I fill in the following:
  | Username        | usernotreserved         |
  | Email           | [email protected]            |
  | Password        | test1234!               |
  | Repeat password | test1234!               |
And I press "Register"
Then I should see "The user has been created successfully" 

I've also tried the scenario manually by filling out the form and the user gets registered and saved in the database correctly.

Running Behat v3 on PHP7.

Running behat with -vvv gives the following output:

behat -vvv output

The huge error is not being shown when running with "javascript".

Behat.yml file:

extensions:
 Behat\Symfony2Extension: ~
 Behat\MinkExtension:
  base_url: "http://127.0.0.1:8000"
  goutte: ~
  selenium2: ~
  sessions:
    default:
      symfony2: ~

I think the best option would be to always use "@javascript", because then I don't need to worry about finished page loads..?

Upvotes: 0

Views: 610

Answers (2)

oli-chowdhury
oli-chowdhury

Reputation: 11

I suspect this is due to the fact that you are not giving any time for the page to load after you have hit the button. Try adding a waiting time such as:

Given I wait "3000" seconds // 3 seconds

I believe you have to write the function iwaitseconds(n){} for that to work.

Example:

public function iWaitSeconds($seconds) { 
    $this->getSession()->wait($seconds); 
}

You can also use the following function if you believe there is ajax involved.

public function iWaitForAjaxToFinish() {
    $this->getSession()->wait(5000, '(typeof(jQuery)=="undefined" || (0 === jQuery.active && 0 === jQuery(\':animated\').length))');
  } 

And this is how you call them in your scenario:

Given I wait for AJAX to finish
Given I wait "3000" seconds

Upvotes: 0

greg606
greg606

Reputation: 491

Use

Then print last response

or

Then print current URL

(put it after And I press "Register")

also

run behat with -vvv flag.

Maybe the url is pointing to wrongly configured env like app_test.php in normal behat scenario, or you have some js that prevents from normal page processing?

Upvotes: 1

Related Questions