Help a Behat newbie

Help a Behat newbie

by Jean-Michel Védrine -
Number of replies: 4

Hello,

I have some problem writting a Behat test for tracker issue MDL-32870

You can see the actual state of my test here.

Background: This issue is about adding filepicker for answers and response fields in questions in the lesson activity.

To test this feature, as a teacher I create a lesson with 2 question page, a multichoice one and a true/false one with a small image, in one of the answer for the first one and in one of the response for the second (lesson "responses" are similar to quiz feedbacks).

I have 2 problems

The first one is that the image is not inserted in the right editor: it is inserted in the "Content" editor and not in one of the "Answer" editors

So obviously my

And I select the text in the "Answer" Atto editor

and

And I select the text in the "Response" Atto editor

steps are wrong which is quite understandable as there are several editors with that label on the page !

I tried to use the editor id for instance

And I select the text in the "id_answer_editor_2" Atto editor but it doesn't work.

The second problem is how I verify that everything is OK when I attempt the lesson as a student, so how I verify there is an image with name moodle_logo.jpg on screen and it is not broken ?

Thanks for any help.

For a "little developer" (in fact I don't claim to be a developer, I am a math teacher smile ) it become harder and harder to contribute to Moodle and with all the new tools introduced in a short time (phpunit, shifter, Behat, selenium, ...) I am fearing the Moodle Community will loose one of it's beauty: the number of people contributing to the project.  Unfortunately the recent decrease in the number of contributors seems to support this fear.

Obviously the solution is not to stop bringing Moodle development to a higher level, each of the above mentioned tool is a step forward (and for instance once you get Behat working it's quite fun and rewarding to look at the tests when they are running). IMHO the solution is for Moodle HQ to better support little developers like me.

For instance I had a lot of trouble getting Behat to work on my windows computer, and the docs weren't really helpful (I know docs are a wiki so I should contribute to make them better but for a teacher like me time is a big constraint and all time I will spend on Moodle docs will be time not spend on trying to contribute to Moodle fixing bugs and developing new plugins).

A Behat cookbook would be very helpful too (How do I ... in my scenarios ?) maybe there is one but I was not able to find it.

To finish on a positive note on a completely different subject, some weeks ago I was rather angry about the work needed to modify my plugins for the new logging and event stuff introduced in Moodle 2.7. I was even doubtful it will be of any use to plugins author. Then I discovered Frederic's beautiful block_xp (Alias Level Up!) plugin and this reconciliate me with the events stuff. Well done Fred (and Moodle HQ) it's little gems like this that makes Moodle so wonderful.

Average of ratings: Useful (2)
In reply to Jean-Michel Védrine

Re: Help a Behat newbie

by Itamar Tzadok -

Selecting text by the editor id does work. I guess you're running tests with firefox in which case you'd better switch to chrome. The selenium firefox webdriver seems to have serious issues finding elements with recent versions of firefox.

To run selenium with chrome you need to add to config.php something like:

$CFG->behat_config = array(
   'default' => array(
       'extensions' => array(
           'Behat\MinkExtension\Extension' => array(
               'selenium2' => array(
                   'browser' => 'chrome',
               )
           )
       )
   ),
   'chrome-win' => array(
       'filters' => array(
           'tags' => '~@_switch_window&&~@_file_upload'
       ),
   ),
);

You also need to download the chrome webdriver from https://code.google.com/p/selenium/wiki/ChromeDriver

Init behat for the new config to take effect.

And to run selenium:

java -jar path-to-your-selenium-jar/selenium-server-standalone.jar -Dwebdriver.chrome.driver=path-to-your-driver/chromedriver.exe

See https://docs.moodle.org/dev/Acceptance_testing/Browsers  for some more details.

As for verifying that the img is displayed, try something like:

And "//img[contains(@src, \'pluginfile.php\')]" "xpath_element" should exist in the ".some_css_class_of_a_container_element" "css_element"
And "//img[contains(@src, \'moodle_logo.jpg\')]" "xpath_element" should exist in the ".some_css_class_of_a_container_element" "css_element"

These steps verify that an img with src with pluginfile.php and the image name exists.

There may be a way to use regular expression for the contained value in which case one step could do the trick.

hth smile

In reply to Itamar Tzadok

Re: Help a Behat newbie

by Jean-Michel Védrine -

Hello Itamar,

Thanks a lot. I already switched to chrome yesterday following a suggestion by Tim Hunt because I had several random failures with firefox (quite bad considering I only managed to install Behat 3 days ago).

I will retry to use the editor id.

Maybe the problem is not in that step but in the next one

And I click on "Image" "button"

as there are several "Image" buttons on the page

In reply to Jean-Michel Védrine

Re: Help a Behat newbie

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Jean-Michel, thanks for the reflections in your post and I can only say that I'm sorry about the increased workload that you see in implementing all the additional tools for testing and so on.  I also want to make developing easier but at the same time we also need to make the platform richer and more powerful so there is a tension.  Good documentation, examples and templates should help.

I've asked all the HQ developers to read your post and to read it again any time they are thinking about a new API or subsystem.  smile

Average of ratings: Useful (2)
In reply to Jean-Michel Védrine

Re: Help a Behat newbie

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators

Jean-Michel "For a "little developer" [...] it become harder and harder to contribute to Moodle and with all the new tools introduced in a short time (phpunit, shifter, Behat, selenium, ...)"

+1

Joseph (another "small-time developer") wink