Actions

Difference between revisions of "Testing/Behat Testing/Setup"

From Mahara Wiki

< Testing/Behat Testing
(Updating instructions to cover what happens if you have OpenJDK 9+ installed which won't work with Selenium)
 
(47 intermediate revisions by 9 users not shown)
Line 1: Line 1:
<h1>How to install Mahara and Behat</h1>
+
{{note|These instructions for the Behat setup are only for machines running Linux. They will not work for Windows machines.}}
  
{{note|These instructions for Mahara and Behat setup are only for machines running Linux. They will not for Windows machines|error}}
+
1. Set up your [[Developer_Area/Developer_Environment | developer environment]] if you haven't already done so.
  
 +
2. Install Behat's dependencies:
 +
<source lang="bash" enclose="div">
 +
$ sudo apt-get install curl openjdk-8-jre-headless
 +
</source>
  
For devs instructions to install a Mahara please follow:
+
3. Add the following config settings to the bottom of your Mahara config.php file inside the htdocs/ subdirectory of the Mahara codebase.  
[https://wiki.mahara.org/index.php/System_Administrator%27s_Guide/Installing_Mahara]
 
  
For Catalyst Moodle devs with a Moodle already installed please follow:
+
{{note|If /var/www/maharadata is not where you created your dataroot, you'll need to replace that with the correct path in the following commands}}
[[https://wiki.wgtn.cat-it.co.nz/wiki/Jinelle_Foley-Barnes/moodle]]
 
  
 +
<source lang="bash" enclose="div">
 +
// Behat config
 +
$cfg->behat_dbprefix = 'behat_'; // must not empty
 +
$cfg->behat_dataroot = "/var/lib/maharadata/master_behat"; // Behat's copy of maharadata
 +
$cfg->behat_wwwroot = 'http://localhost:8000'; // Must be this
 +
$cfg->behat_selenium2 = "http://127.0.0.1:4444/wd/hub"; // Must be this
 +
</source>
  
For everyone else please follow the steps below:
+
{{note|There should be a behat_dataroot for each Mahara instance that you set up. Here the one for the master branch / this Mahara instance is created.}}
 +
{{note|If the directory for $cfg->behat_dataroot does not exist then it will need to be created.}}
  
 +
4. Make your Behat data directory (check this matches what you set in config.php):
 +
<source lang="bash" enclose="div">
 +
$ sudo mkdir /var/lib/maharadata/master_behat
 +
</source>
  
Install Apache, PHP5, Database, Git:
+
5. Make the directory and any subdirectories writeable by Mahara (check this is correct):  
  sudo apt-get install php5 php5-cli php5-pgsql php5-xmlrpc php5-intl php5-curl php5-gd php5-xdebug php5-dbg postgresql apache2 git curl
+
<source lang="bash" enclose="div">
 +
$ sudo chmod 777 -R /var/lib/maharadata/master_behat
 +
</source>
  
Restart Apache
+
6. Change the directory and any subdirectories permissions to be owned by apache user (check this is correct):
  sudo service apache2 restart
+
<source lang="bash" enclose="div">
 +
$ sudo chown -R www-data.www-data /var/lib/maharadata/master_behat
 +
</source>
  
Make a Mahara directory in the same place as Moodle
+
* For Ubuntu, apache runs with user www-data
  sudo mkdir /var/www/html/mahara
+
* For Centos, apache runs with user apache
  
Then change into the Mahara directory
+
7. Run Behat tests (change into your Mahara code directory first) as the apache user:
  cd /var/www/html/mahara
+
<source lang="bash" enclose="div">
 +
$ cd
 +
$ cd code/mahara
 +
./test/behat/mahara_behat.sh run
 +
</source>
  
Then change the permission (insert the username of the machine into the username. Don't use the word username)
+
Or if you have sudo access:
sudo chown username:username /var/www/html/mahara
+
<source lang="bash" enclose="div">
 +
$ cd
 +
$ cd code/mahara
 +
sudo -u www-data ./test/behat/mahara_behat.sh run
 +
</source>
  
Get Mahara clone.
 
  git clone https://github.com/MaharaProject/mahara.git .
 
  
 +
For the first time of running Behat, you need to wait for the Behat environment initialisation. This can take a while.
  
Checkout the master(15.04) branch
+
To run all tests:
  git checkout master
+
./test/behat/mahara_behat.sh run
  
Note: if this step doesn't work. You may have forgotten the fullstop off the end of the git clone command. It created a mahara directory inside the other mahara directory. Go in to the file explorer and delete the second instance.  
+
To run your specific tests marked with @yourtags:
 +
./test/behat/mahara_behat.sh run @yourtags
  
Become Postgres user:
+
To run a particular feature file:
  sudo su postgres
+
./test/behat/mahara_behat.sh run my_file.feature
  
Open psql. After some preamble you should see the prompt postgres=#.
+
The run / runheadless / rundebug / rundebugheadless / runfresh are interchangable with run in above commands
  psql
 
  
You should now see postgres=# ready and awaiting your command.
+
[[Category:Behat]]
  
Create a user and don't use the PW that you use for your workstation:
+
== Running Behat tests with html report and screenshots on failed steps ==
  CREATE USER maharauser WITH PASSWORD 'yourpassword';
 
 
 
As you previously did, you should see postgres=#
 
You are now going to create a database:
 
  CREATE DATABASE mahara WITH OWNER maharauser;
 
  
Exit to leave psql by pressing Ctrl-D
+
To run tests with html output, add the flag 'html' to your test run command, e.g:
  
Exit back to your own user:
+
<source>./test/behat/mahara_behat.sh run create_page.feature html</source>
  exit
 
  
Make a data directory:
+
This will automatically open an html report of the test run in your browser. If there were failed steps, the the html report plugin takes a screenshot of the page that failed.
  sudo mkdir /var/www/maharadata
+
You can find the screenshots in your behat dataroot (usually /var/lib/maharadata/master_behat) at the following path:
 +
behat/html_results/<feature title>/<scenario title>.png
  
Make directory writeable by mahara:
+
Hopefully we will rig up the report to link to the screenshot soon.
  sudo chmod 777 /var/www/maharadata
 
  
 +
== How to Run Mahara behat test on Chrome ==
  
In the code below make sure you change <your email address> to your actual work email address, without the <> signs.
+
{{note|Mahara behat is running on chrome driver by default in 17:04+}}
  
<pre>
+
This is steps for older versions of Mahara
<?php
 
$cfg = new stdClass();
 
  
$branch = 'master';
+
1. Download chromedriver here http://www.seleniumhq.org/download/ ( download the latest release)
  
// database connection details
+
2. Start selenium Server with chrome driver
// valid values for dbtype are 'postgres8' and 'mysql5'
 
$cfg->dbtype  = 'postgres';
 
$cfg->dbhost  = 'localhost';
 
$cfg->dbuser  = 'maharauser';
 
$cfg->dbname  = "mahara";
 
$cfg->dbpass  = 'password';
 
  
$cfg->dataroot = "/var/www/maharadata";
+
$ java -jar selenium-server-standalone-2.53.1.jar -Dwebdriver.chrome.driver=path of chromedriver
  
$cfg->sendemail = true;
+
3. Change the behat.yml file as follows,
$cfg->sendallemailto = '<your email address>';
 
  
$cfg->productionmode = false;
+
Here is the file path var/lib/maharadata/master_behat/behat.yml
$cfg->perftofoot = true;
 
</pre>
 
  
 +
when you open the beaht.yml file replace the following code with the lines that contain sessions to goutte=NUll( Niranjan or Lovesh can exactly tell you where to replcae the code if it is confusing) and save it
  
An example config.php should appear:
+
javascript_session: selenium2
 +
        selenium2:
 +
          browser: chrome
 +
            goutte: ~
  
* Copy all of the code above
+
4. Run the behat test without rundebug
* open text editor from start menu
 
* paste code
 
* save file in the directory: /var/www/html/mahara/htdocs/ and call it config.php
 
* save
 
  
===Behat installation with Mahara===
+
for example  ./test/behat/mahara_behat.sh run example.feature
  
Add these config settings to the bottom of your config.php file.
+
== Debugging ==
  
<pre>
+
In some cases, depending on the version of Ubuntu you have, the wrong version of OpenJDK will be installed. Selenium only runs with OpenJDK 8, but Ubuntu Bionic tends to install OpenJDK 11 (especially if 'default-jre' was installed)
// Behat config
 
$cfg->dbprefix = ''; // Behat complains without this
 
$cfg->wwwroot = 'http://127.0.0.1/mahara/htdocs';
 
$cfg->behat_dbprefix = 'behat_'; // must not empty
 
$cfg->behat_dataroot = '/var/www/maharadata/behat';
 
$cfg->behat_wwwroot = 'http://localhost:8000';
 
</pre>
 
  
Make a data directory:
+
If you run into issues where the script says it has started Selenium, but that Behat throws an exception because it thinks Selenium has not started, check the JRE installed:
  sudo mkdir /var/www/maharadata/behat
 
  
Make directory writeable by mahara:
+
<source>java -version</source>
  sudo chmod 777 /var/www/maharadata/behat
 
  
Run Behat tests
+
If it says you're using OpenJDK 10 or 11 (which can ship with Ubuntu 18.04 depending on which service pack is used), this needs to be uninstalled first:
 
 
  cd /var/www/html/mahara
 
  ./test/behat/mahara_behat.sh run @yourtags
 
  
For the first time of running behat, you need to wait for the Behat environment initialisation.
+
<source>sudo apt-get remove openjdk*</source>
  
Disable test site
+
Once this is removed, you will need to re-run the OpenJDK 8 installation:
  ./test/behat/mahara_behat.sh action disable
 
  
 
+
<source lang="bash" enclose="div">
[[Category:Behat]]
+
$ sudo apt-get install openjdk-8-jre-headless
 +
</source>

Latest revision as of 06:15, 7 August 2019

Note: These instructions for the Behat setup are only for machines running Linux. They will not work for Windows machines.

1. Set up your developer environment if you haven't already done so.

2. Install Behat's dependencies:

$ sudo apt-get install curl openjdk-8-jre-headless

3. Add the following config settings to the bottom of your Mahara config.php file inside the htdocs/ subdirectory of the Mahara codebase.

Note: If /var/www/maharadata is not where you created your dataroot, you'll need to replace that with the correct path in the following commands
// Behat config
$cfg->behat_dbprefix = 'behat_'; // must not empty
$cfg->behat_dataroot = "/var/lib/maharadata/master_behat"; // Behat's copy of maharadata
$cfg->behat_wwwroot = 'http://localhost:8000'; // Must be this
$cfg->behat_selenium2 = "http://127.0.0.1:4444/wd/hub"; // Must be this
Note: There should be a behat_dataroot for each Mahara instance that you set up. Here the one for the master branch / this Mahara instance is created.
Note: If the directory for $cfg->behat_dataroot does not exist then it will need to be created.

4. Make your Behat data directory (check this matches what you set in config.php):

$ sudo mkdir /var/lib/maharadata/master_behat

5. Make the directory and any subdirectories writeable by Mahara (check this is correct):

$ sudo chmod 777 -R /var/lib/maharadata/master_behat

6. Change the directory and any subdirectories permissions to be owned by apache user (check this is correct):

$ sudo chown -R www-data.www-data /var/lib/maharadata/master_behat
  • For Ubuntu, apache runs with user www-data
  • For Centos, apache runs with user apache

7. Run Behat tests (change into your Mahara code directory first) as the apache user:

$ cd
$ cd code/mahara
./test/behat/mahara_behat.sh run

Or if you have sudo access:

$ cd
$ cd code/mahara
sudo -u www-data ./test/behat/mahara_behat.sh run


For the first time of running Behat, you need to wait for the Behat environment initialisation. This can take a while.

To run all tests:

./test/behat/mahara_behat.sh run

To run your specific tests marked with @yourtags:

./test/behat/mahara_behat.sh run @yourtags

To run a particular feature file:

./test/behat/mahara_behat.sh run my_file.feature

The run / runheadless / rundebug / rundebugheadless / runfresh are interchangable with run in above commands

Running Behat tests with html report and screenshots on failed steps

To run tests with html output, add the flag 'html' to your test run command, e.g:

./test/behat/mahara_behat.sh run create_page.feature html

This will automatically open an html report of the test run in your browser. If there were failed steps, the the html report plugin takes a screenshot of the page that failed. You can find the screenshots in your behat dataroot (usually /var/lib/maharadata/master_behat) at the following path: behat/html_results/<feature title>/<scenario title>.png

Hopefully we will rig up the report to link to the screenshot soon.

How to Run Mahara behat test on Chrome

Note: Mahara behat is running on chrome driver by default in 17:04+

This is steps for older versions of Mahara

1. Download chromedriver here http://www.seleniumhq.org/download/ ( download the latest release)

2. Start selenium Server with chrome driver

$ java -jar selenium-server-standalone-2.53.1.jar -Dwebdriver.chrome.driver=path of chromedriver

3. Change the behat.yml file as follows,

Here is the file path var/lib/maharadata/master_behat/behat.yml

when you open the beaht.yml file replace the following code with the lines that contain sessions to goutte=NUll( Niranjan or Lovesh can exactly tell you where to replcae the code if it is confusing) and save it

javascript_session: selenium2

        selenium2:
          browser: chrome
           goutte: ~

4. Run the behat test without rundebug

for example ./test/behat/mahara_behat.sh run example.feature

Debugging

In some cases, depending on the version of Ubuntu you have, the wrong version of OpenJDK will be installed. Selenium only runs with OpenJDK 8, but Ubuntu Bionic tends to install OpenJDK 11 (especially if 'default-jre' was installed)

If you run into issues where the script says it has started Selenium, but that Behat throws an exception because it thinks Selenium has not started, check the JRE installed:

java -version

If it says you're using OpenJDK 10 or 11 (which can ship with Ubuntu 18.04 depending on which service pack is used), this needs to be uninstalled first:

sudo apt-get remove openjdk*

Once this is removed, you will need to re-run the OpenJDK 8 installation:

$ sudo apt-get install openjdk-8-jre-headless