Skip to content

Behat Locally

mantarias edited this page Dec 9, 2024 · 5 revisions

Moodle Official Guide:

https://moodledev.io/general/development/tools/behat/running

Clarifying Documentation:

https://gist.github.com/danpoltawski/8036489

Background:

  • Dette er en guide baseret på Moodles guide. Den er lavet i et forsøg på at skære det overflødige væk fra Moodles guide og samtidig tilføje til Moodles guide, hvor den er fundet mangelfuld.
  • Indtil videre virker det kun på Windows og med Firefox browseren.
  • Guiden opdateres, når det virker på andre styresystemer og i andre browsere. Før du prøver at følge guiden skal du sikre dig, at du befinder dig på en stabil branch, der ikke har nogen database eller lignende problemer

Opsætning på Windows:

Først skal du clone repository moodle-browser-config

  • Åben din cmd
  • Naviger til en lokalisation du kan finde igen og hvor du vil clone et repository til
  • Kør: git clone https://github.com/andrewnicols/moodle-browser-config
  • Åben din Moodle installations config.php i en IDE eller notepad. Denne befinder sig under moodle-1/server/moodle
  • Tilføj require_once('/path/to/your/git/dir/moodle-browser-config/init.php'); til config filen. Husk selvfølgelig at erstatte denne PATH med den PATH til init.php filen i det repository du lige har clonet (Bemærk de skal placeres over require_once(__DIR__ . '/lib/setup.php');)
  • Hvis du gerne vil kigge på moodle-browser-config repositoriet kan du kigge på det her: https://github.com/andrewnicols/moodle-browser-config

Nu skal du download Selenium Server (Grid). Dette er ikke strengt krævet, men opfordres heftigt.

  • Tilgå: https://www.selenium.dev/downloads/
  • Scroll forbi .Net, Ruby, Java, Python og JavaScript download linkene
  • Download Selenium Server (Grid). Det er en enkel JAR fil
  • Gem filen et sted, der er nemt at tilgå
  • Naviger i din cmd til der hvor du har downloadet Selenium Server (Grid)
  • Kør filen med: java -jar selenium-server-4.25.0.jar standalone --selenium-manager true

Nu skal du sætte Moodle op til at kunne køre Behat

  • Lav en ny mappe i “moodle-1\server\moodledata”
  • Kald mappen “behat”
  • Tilføj følgende til din Moodle config.php (Bemærk de skal placeres over require_once(__DIR__ . '/lib/setup.php');)
$CFG->behat_dataroot = '/path/to/the/dataroot/you/created';

$CFG->behat_wwwroot = 'http://127.0.0.1';

$CFG->behat_prefix = 'beh_';
  • Erstat /path/to/the/dataroot/you/created med PATHen til den mappe du har lavet

  • Gem dine ændringer.

  • Start din Moodle server.

  • Åben et nyt instans din cmd

  • Naviger til moodle mappen (moodle-1\server\moodle)

  • Hvis du ikke har composer installeret, så kig på PHPUnit setup guiden og download composer

  • Kør: php admin/tool/behat/cli/init.php

  • Når denne kommando er færdig, så giver den dig en kommando du kan køre for at køre alle Behat test inkluderet i Moodle installationen

    Den minder om: vendor\bin\behat --config C:\moodle-1\server\moodledata\behat\behatrun\behat\behat.yml

  • Du kan se kommandoen virkede ved at din cmd siger noget i stil med “Test server set up on 127.0.0.1 in browser”

  • Der vil dukke grønne prikker op, der symboliserer tests, der var succesfulde.

  • Når den har kørt i et stykke tid, kan du stoppe den. Der er mere end 3000 Behat tests, hvilket tager flere timer at køre igennem

  • Det er muligt at tilføje --tags= til kommandoen for kun at køre specifikke tests. Se Moodle guiden for mere om tags

  • Hver gang I tilføjer en .feature fil, skal I Køre: php admin/tool/behat/cli/init.php I jeres cmd igen

Headless browsers:

  • Hvis din headless browser åbner et vindue som står blankt ændrer følgende
  • Åben din “Moodle-browser-config” mappe
  • Åben source folder
  • Åben ProfileManager php source file
  • Lokaliser funktionen for hvilken browseren du ønsker at ændre, fx. Chrome
  • public function getStandardChromeProfiles(bool $w3c): array {
  • Ændrer ‘headless=new’ til ‘headless=old’ for funktionen under kommentaren
    // Google Chrome using Selenium.
  • Gem din fil og restart alle dine cmd prompter

før: image

efter: image

Opsætning på Mac(ARM) and Linux

This guide is written for Macs with ARM processors using the Firefox Browser and also works for Linux

Preconditions:

  1. Download the driver for your browser
    • What is a browser driver?:
      • “A browser driver is a tool that allows automation and programmatic control of a web browser, often in combination with a web automation framework like Selenium or Playwright”
    • The driver for firefox is called geckodriver: GeckoDriver GitHub
    • Driver: geckodriver-v0.35.0-macos-aarch64.tar.gz
    • Save it in a place where you can access it. We will refer to this path as /driver_location
  2. Ensure you have a version of the JDK:
    • If you do not have a version of the JDK, download it here: JDK
  3. Download Selenium:
    • What is Selenium:
      • “Selenium is an open-source suite of tools used for automating web browsers. It is commonly used for testing web applications
    • Go to: https://www.selenium.dev/downloads/
    • Download Selenium Server (Grid). This is a single .jar file

I would recommend to have a folder /User/Drivers, to store both the geckodriver and Selenium driver

Preconfigured Browser Profiles:

  • moodle-browser-config is a recommended inclusion for Behat
  • This configuration tooling provides a range of standard browser profiles for testing
  • Clone the below repository to a place where you can easily access it. We will call the path to the moode-browser-config: /path-to-brwsr-config
  • You can place it in moodle-1, just remember to untrack

git clone https://github.com/andrewnicols/moodle-browser-config

  • Open up config.php of the moodle installation and add the following line

require_once('/path-to-brwsr-config/init.php');

  • Note the above line has to be placed above the following line in the code: require_once(__DIR__ . '/lib/setup.php');

Add browser and browser driver to your $PATH

  • First find out what SHELL you are using by: echo $SHELL

  • I get the output /bin/zsh. You could also be using bash, in that case the file you have to edit is called .bashrc

  • Navigate to your home directory, either by finder or with cd ~

  • If you do not have the .zshrc file, create one by writing touch ~/.zshrc

  • Then edit the file with sudo nano ~/.zshrc

  • Add the path to where your drivers are located, mine are located at ~/Drivers

  • My final .zshrc file looks like:

export PATH=$PATH:~/Drivers
export PATH=$PATH:/Applications/Firefox.app/Contents/MacOS
  • Save the file and write: source ~/.zshrc to apply the changes
  • Notice! You also have to add the path to all of the browsers you will be using
  • These can be found in the /Applications directory
  • If you want to access the files behind an application in finder hold control and left-click on the application. This will open up a menu where you can select “Show Package Contents” image

NOTE(LINUX): On Linux, it is enough to just add the selenium jar’s directory to path, if geckodriver is installed globally

Installing Behat:

  • Before we can install behat we have to make some changes to our config.php file and create a place to store our test results
  • Create a folder called behat, and place somewhere you know the path to. I place mine in /moodledata
  • Ensure these lines are present in your config.php
$CFG->wwwroot   = 'http://localhost:8000';
$CFG->behat_dataroot = $CFG->dataroot . '/behat'; //THE PATH FOR YOUR BEHAT TEST DATA
$CFG->behat_wwwroot = 'http://127.0.0.1:8000';
$CFG->behat_prefix = 'beh_';
  • Your moodle site will run on: http://localhost:8000/
  • Your behat site which also serves the same moodle files will run on http://127.0.0.1:8000/
  • Commands for installing behat:
    • Navigate into the moodle directory and execute: php admin/tool/behat/cli/init.php

    • You will be provided by the command you have to use to run behat tests in the terminal. Mine is:

vendor/bin/behat --config /Users/adomas/Desktop/P5/Code/moodle-1/server/moodledata/behat_test_data/behatrun/behat/behat.yml --profile=firefox

Running Behat:

  • Ensure Selenium Server is running by navigating to where you have installed it and run: java -jar selenium-server-4.25.0.jar standalone

  • Now navigate to to your moodle folder and run the vendor command from when you installed behat

Clone this wiki locally