DrupalExtension is an extension for Behat which makes it easier to test a Drupal application. One of it's features is that it allows normal Drupal modules to provide Behat sub contexts by simply implementing them in a file called module_name.behat.inc within the module. Sub contexts allow you to provide new step definitions to Behat.

It seems like a great idea to me for chosen to provide a Behat subcontext which allows a developer to manipulate chosen page elements via behat and assert their current state.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ceng created an issue. See original summary.

johnennew’s picture

Issue summary: View changes
johnennew’s picture

He's a rough starter in case this of interest.

This provides the following step definition:

When I the set the chosen element "My Field" to "Some value"

This sets the value of the field labeled "My Field" to "Some value" which should be one of the options.

There is work to do on this to provide multiple selections / deselections and to assert the current state of the select element.

clemens.tolboom’s picture

Status: Active » Needs work

We are testing with behat and do use chosen. Can you please add some documentation with the patch on how to use this new context as I have no clue how to manage that. But it's awesome you try this as I was wondering how this would land in contrib.

+++ b/chosen.behat.inc
@@ -0,0 +1,81 @@
\ No newline at end of file

No whiteline.

johnennew’s picture

I don't think the patch is ready yet (I left it on active) but its great you think its useful.

There is work to do on this to provide multiple selections / deselections and to assert the current state of the select element. Since this is of interest I'll try and find some time for it.

I can add some information to the README or could add to doc comment in the file.

Effectively the steps to use a sub context with drupalextension are documented at the link below (you add an extra line to the behat.yml file):
http://behat-drupal-extension.readthedocs.org/en/latest/subcontexts.html

This then lets you use the step definitions in the sub context, in this case:

When I the set the chosen element "My Field" to "Some value"

Where "My Field" is a field identifier, usually a label and "Some value" is the option element to select.

John

clemens.tolboom’s picture

I'm completely focused on a behat 3 og.behat.inc but your patch helped :-)

I'm not sure how to indicate the difference between Behat 2 & 3 regarding DrupalExtension but

+++ b/chosen.behat.inc
@@ -0,0 +1,81 @@
+   * @When /^I the set the chosen element "([^"]*)" to "([^"]*)"$/

This is Behat 2 syntax imho. For Behat 3 (we use)

@When I the set the chosen element :locator to :$value

johnennew’s picture

og? Do you not use group yet?! Thats what all the cool kids are using :)

Thanks for the information on the regex syntax, I am using behat 3 and the patch works for it - but we should probably just support version 3 and do it properly.

wingmanjd’s picture

How can I add more than one item to a multi-select chosen field?

wingmanjd’s picture

Nevermind, just calling the line again allows me to select additional items.

wingmanjd’s picture

FileSize
2.58 KB

There was a small typo in the step definition. I've removed the extraneous "the" from the step definition.