Skip to content

Instantly share code, notes, and snippets.

@danpoltawski
Last active March 22, 2023 17:20
Show Gist options
  • Save danpoltawski/8036489 to your computer and use it in GitHub Desktop.
Save danpoltawski/8036489 to your computer and use it in GitHub Desktop.
Simplified behat for Tim Hunt

Simplified behat for Tim Hunt.

Disclaimers:

  • I'm hoping this will help you understand so you can help us improve our documentation.
  • This is simplified version, i'm ignoring some details which are unimportant for discusing this.

What Behat requires.

1/ A Moodle instance running on a webserver which you can connect to, just like you use for development and manual testing. One requirement we put on behat is that this runs on a different wwwroot to your 'normal' moodle install. So you will need to configure apache to listen on two urls for the same moodle checkout if you want to run tests manually. e.g.

$CFG->wwwroot = 'http://dan.moodle.local/'
$CFG->behat_wwwroot = 'http://127.0.0.1/'

2/ A seperate database prefix and dataroot (just like phpunit):

$CFG->behat_prefix = 'behat_';
$CFG->behat_dataroot = '/Users/danp/moodles/im/moodledata_behat';

2/ Selenium. Download the jar and run it.

3/ Behat installed from composer. The admin tool will attempt to do it:

php admin/tool/behat/cli/init.php

To run behat:

Run the behat command which "php admin/tool/behat/cli/init.php" tells you to do:

vendor/bin/behat --config /Users/danp/moodles/pm_behating/moodledata_behat/behat/behat.yml

Troubleshooting:

There tend to be three things which can stop it from working:

  • Composer problems (I guess this generally works ok on windows, since phpunit doesn't seem to be plagued the same)
  • Behat can't connect to selenium (How to debug - is it running, can you telnet and connect on the selenium port?)
  • The selenium browser can't connect to your webserver (can you connect to your $CFG->behat_wwwroot in a browser?)
@nawshad
Copy link

nawshad commented Jun 22, 2014

I followed the steps and when I ran vendor/bin/behat --config C:\wamp\moodledata_behat\behat\behat,yml

I found an error: Exception is thrown in before scenario hook. The base url is not a behat test site, ensure you started the built in web server in the correct directory or your webserver is incorrectly started or set up.

I am using wamp. I created virtual host for running behat tests. And the message after running php admin/tools/cli/init.php says, test environment is successfully setup in http://localbehatsite/moodle26forkedtests

@jgramp
Copy link

jgramp commented Jun 1, 2015

I get this same error message as nawshad "The base url is not a behat test site, ensure you started the built in web server in the correct directory or your webserver is incorrectly started or set up."

Any clues what I might be doing wrong?

My config looks like this:

$CFG->wwwroot = 'http://localhost/moodle29';
$CFG->dataroot = 'C:\wamp\moodledata';

$CFG->behat_prefix = 'behat_';
$CFG->behat_dataroot = 'C:\wamp\behatdata';
$CFG->behat_wwwroot = 'http://127.0.0.1'; // must be different from wwwroot

@jgramp
Copy link

jgramp commented Jun 1, 2015

Ok, I figured it out. I thought I only had to initialise the behat environment once, but it seems like it's something I have to do every time I run the tests by changing to my moodle29 root environment in the command line and running:
php admin/tool/behat/cli/init.php

My config is as above, except I have to point $CFG->behat_wwwroot to the moodle root folder.

$CFG->wwwroot = 'http://localhost/moodle29';
$CFG->dataroot = 'C:\wamp\moodledata';

$CFG->behat_prefix = 'behat_';
$CFG->behat_dataroot = 'C:\wamp\behatdata';
$CFG->behat_wwwroot =http://127.0.0.1/moodle29 // must be different from wwwroot

@ilyatregubov
Copy link

Same problem. Though I ran behat successfully on my local machine, when I tried doing the same on virtual machine get "The base URL (http://localhost/moodle) is not a behat test site, ensure you started the built-in web server in the correct directory or your web server is correctly started and set up"

Here is output for init script:

php admin/tool/behat/cli/init.php

Behat test environment already installed
Acceptance tests environment enabled on http://localhost/moodle, to run the tests use:
vendor/bin/behat --config /var/lib/sitedata/behat-moodle-site/behat/behat.yml

Here are config details for behat:
$CFG->behat_prefix = 'b_';
$CFG->behat_dataroot = '/var/lib/sitedata/behat-moodle-site';
$CFG->behat_wwwroot = 'http://localhost/moodle';

my wwwroot is different from behat_wwwroot

@AnastasiaLd
Copy link

any updates on how to fix this issue?

After running php admin/tool/behat/cli/init.php
Acceptance tests environment enabled on http://127.0.0.1

After running php admin/tool/behat/run.php
The base URL (http://127.0.0.1) is not a behat test site, ensure you started the built-in web server in the correct directory or your web server is correctly started and set up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment