Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Acceptance testing for the Moodle App: Difference between revisions

From MoodleDocs
Line 130: Line 130:
<tt>And '''I press "Course 1" in the app'''</tt>
<tt>And '''I press "Course 1" in the app'''</tt>


This finds an element which contains the visible text 'Course 1' and clicks it. It should work for buttons and similar.
This finds an element which contains either the visible text, or Aria label, 'Course 1' and clicks it. It should work for links, buttons and similar.  


* Exact matches (an element which contains only the specified text) will be preferred. If there are no exact matches, then partial ones (anything containing that text) will be considered.
For buttons that are icons with no text, you can usually find them using the Chrome inspector - look for the 'aria-label' attribute.
 
* Exact matches (an element which contains only the specified text, or where the Aria label is exactly the specified text) will be preferred. If there are no exact matches, then partial ones (anything containing that text) will be considered.
* If there are multiple matches, or none, the step will fail.
* If there are multiple matches, or none, the step will fail.



Revision as of 12:11, 23 November 2018

This is only a proposal, MDL-63977, and is not yet available in Moodle. Please contact sam marshall if you have any queries and do not use this information for anything yet!

From Moodle 3.7 if this change is accepted it will be possible to write Behat tests for mobile app features.

Summary

There are now Behat tests for the mobile app. These are used for automated functionality testing of the mobile app.

By default, these do not run as part of a normal Behat run. This page tells you how to run the tests, and how to write them.

A key point is that these tests for some parts of the mobile app (and for its plugins) are included within the Moodle codebase, not within the app codebase, because they are run using the Moodle Behat infrastructure..

Running Behat tests for the mobile app

Set up a mobile app development environment

First you will need to set up a mobile app development environment.

Follow the first part of the instructions on this page:

You need to get as far as the part in section 5 where you open the app in the browser; this is what Behat will do. You don't need to complete the later steps.

  • You will need to update this environment periodically, for example when a new version of the mobile app is released. Behat does not do this for you.
  • If you need to run tests against multiple versions of the mobile app, you can do this by updating the code in this mobile app workspace and then running the Behat tests again, or by having multiple copies of the mobile workspace and changing which one Behat points to.

Add the mobile app Behat configuration

You need to add a line to your config.php to enable app testing. There are two ways to do this. First, you need to be aware of a couple of facts about the Ionic server used for app testing:

  • Depending on your computer, it may take about 3 minutes to start up.
  • The server uses about 1GB RAM.

Let Behat launch the app environment

If you want Behat to launch the app environment for you, then use this line in config.php:

$CFG->behat_approot = '/path/to/app/workspace/moodlemobile2';

When you do this, the Ionic server will be started automatically when Behat runs a test that uses the 'I enter the mobile app' step. It will be automatically terminated when the Behat test run finishes. If the test run includes multiple scenarios that use the app, they will all reuse the one server; it won't restart each time.

This is simple and convenient, but it is probably not a good approach for developers who frequently re-run a short Behat run (as you have to wait for it to start Ionic every time) or for complex systems that run Behat in parallel (as you may end up with multiple copies of Ionic eating up your RAM).

Manually launch the app environment yourself

The other option is to launch the Ionic server yourself (using ionic serve -b) before running any Behat tests, and also kill it yourself once you are finished with it. In this case, after launching it you will see output like:

[OK] Development server running!

    Local: http://localhost:8100
    External: http://137.108.5.43:8100, http://192.168.56.1:8100

To use it in Behat, add this line to the Moodle config.php:

$CFG->behat_ionicaddress = 'http://localhost:8100';

The advantage of this approach is that you are in charge of bringing up and taking down the Ionic server, so you can do this efficiently, share a copy between parallel runs, etc. The disadvantage is that you do have to remember to do it; if the server isn't running, tests which use the app will fail.

Browser profiles

Mobile tests only run in Chrome, so you need to make sure you have a Chrome profile set up in your config.php Behat settings.

Behat will automatically run app tests (those with @app tag) only in a Chrome browser profile. So, if you run multiple browser tests, it won't waste time trying to run the app tests in each one.

Behat init

After you have set up the config.php, you will need to re-run Behat init:

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

This is necessary because by default, Behat won't run app tests (those with @app tag) at all, since you didn't have it configured.

Running Behat

To run mobile tests in Behat, simply launch Behat in the usual way, but make sure you are using a Chrome profile. (Depending on your setup, this might mean using --profile=chrome.)

You can specify the scenarios to run as normal. The app tests all have the @app tag, so if you want to run all the mobile tests you can specify --tags=app, but you can also run any other set of scenarios. It is OK to combine app and normal tests in the same run.

Writing tests

This page assumes you already know all about Writing_acceptance_tests in general.

Test structure

  • Mobile app test scenarios should be marked @app and @javascript in addition to any other tags that may be required.
  • If creating a feature file specifically for app tests, call it app_whatever.feature (i.e. use the app_ prefix). This is not technically required, it's just for consistency.

You are writing a normal Behat test and this is likely to require background steps similar to any other Moodle Behat test, for example the following "courses" exist, and so on.

Start the app

Once all necessary Moodle configuration steps (creating courses, users, groups, etc.) are done, use this Behat step to start the app:

Given I enter the app

This will:

  • Set up your Moodle settings to allow the mobile app to connect.
  • Launch Ionic if necessary
  • Open the Ionic server address in the test Chrome browser.
  • Install necessary JavaScript code in the page that supports Behat testing.
  • Automatically enter the server URL into the app if necessary.

After this step completes, if it is the first time you ran the app inside this scenario, you will be left at the login screen. If you already logged in earlier, then you will be at the start page.

You can use this step even when you are already in the app; this will restart it.

Log in to the app

All the other app-specific Behat steps end with the words 'in the app' to distinguish them from the normal steps. To log in:

When I log in as "student1" in the app

This step can only be used on the app login page. It will log in with the given username and password. You will then be left at the start page.

Navigation

To press something on the screen:

And I press "Course 1" in the app

This finds an element which contains either the visible text, or Aria label, 'Course 1' and clicks it. It should work for links, buttons and similar.

For buttons that are icons with no text, you can usually find them using the Chrome inspector - look for the 'aria-label' attribute.

  • Exact matches (an element which contains only the specified text, or where the Aria label is exactly the specified text) will be preferred. If there are no exact matches, then partial ones (anything containing that text) will be considered.
  • If there are multiple matches, or none, the step will fail.

Standard test steps

You can use all the normal Moodle Behat test steps in app testing, but some of them don't work very well. The app has a complex DOM and previous pages that are 'back' from your current page may still be present in the DOM, which means that any steps that just look for the first matching element in the DOM are likely to look for elements on a page you're not even on.

I should see and I should not see are fine.

The I reload the page step does not work in the app. Use I enter the app to do this.

Leaving the app

If you want to leave the app and go back to Moodle, simply use a Moodle step that goes to a page, such as I am on site homepage or I am on "Course 1" course homepage.

Limitations

  • Currently it doesn't work very well if you click a link in the app which opens a new browser window.

Work in progress

I haven't finished writing this page yet