6c) How-to / Q&A

How can I get the latest version of CWB?

  1. First update CWB with the commandline CTF: mvn -U compile.
  2. Do a reimport of the Maven dependencies within IntelliJ.

How can I see which steps are possible?

You can find a complete listing in the language reference.  In IntelliJ you will get suggestions when you start typing a step. To get a list of all the suggestiosn without prefix, do ctrl+space after the Cucumber keyword (Given/When/Then/But).

doesnt work anymore?

How do I select one or multiple features for execution?

???? The current support for running features vanuit IntelliJ is stuk. ???? Automatisch uitvoeren van een scenario of feature met rechtermuisklik werkt enkel indien de Ruby plugin disabled is. In het andere geval moet de Run/Debug configurations manueel aangemaakt worden.

There are 2 ways to run features :

Run features in IntelliJ

Simply right mouse click on :

  • a directory with multiple .feature files (Run all features in)
  • a .feature file (Run Feature)
  • a Feature: statement (Run Feature)
  • a Scenario: statement (Run Scenario)

 

Both IntelliJ 13 and 14 have problems with correctly prefilling the glue property of a run configuration when you are executing a scenario from the UI. You can always modify a run configuration after it has been generated, however the easiest solution is to add the correct glue (usually com.foreach.cuke) to the default run configuration for Cucumber Java.


Automatic run configuration


Manual fix

Run features from within command-line

This can be done via maven.  Either all features within the features directory are executed, or all features that have a specific tag.  Example command-line:

c:\code\myproject\mvn integration-test -Denvironment=test -Dbrowser=firefox -Dtags=@long-test

See What are the relevant command-line properties?

 

Features and Scenario's can be tagged with the ' @ 'sign (fe: @my-tag).

These tags can be specified with property tags (-Dtags).  Examples:

-Dtags=~@ignore

All features that don't have the @ignore tag. This option is always on while running from the command-line and doesn't have be declared again.

When there's a negative tag defined, this should be the only tag.

Positive tags can be combined (see examples below).

-Dtags=@nobrowserAll features with tag @nobrowser.
-Dtags=@nobrowser,@firefox-onlyAll features with @nobrowser OR @firefox-only.

How can I prevent that the tests are executed via command-line?

This is particularly relevant when you have a combination of unit tests and cucumber in a larger project.  The result depends on the actual configuration, but by default you can set skipTests=true property.

How do i select one specific scenario for execution?

This can only be done in IntelliJ by right clicking the Scenario: keyword and select Run Scenario.

Where can I find reports of execution via command-line?

Reports are generated in the target \cucumber  directory .  Following reports are available:

plain-html-reportsStandard Cucumber HTML reports .
pretty-html-reportsProcessed HTML reports, better layout and easier to navigate .
cucumber-report.jsonJSON format, used for pretty HTML generation.
cucumber-report.xmlJUnit format, suitable for build server etc.

How do I pause a script and proceed manually?

Add the following step:  Then pause.  Then a dialogue box with an OK button appears, the scenario will continue to run after pressing OK.  

Please note that sometimes the diaologbox does not appear . Pressing alt-tab, you should be able to locate it.

The pause step will continue to wait forever unless you click OK, so this is only useful for temporary use. On a build server, the system property = nopause must be true in order to avoid that the execution is paused.

How can I run the tests on a build server?

All tests can be performed via the maven integration-test target

  • define nopause system property -Dnopause=true

     >>This will ensure that any pause steps are skipped and the build does not hang forever..

  • define environment with the  -Denvironment system property
  • all test reports are in the target/cucumber directory 
    • the xml file is JUnit xml format
    • the pretty-html-reports directory is useful to link to builds as an artifact
Example configuration in Bamboo

Task configuratie

Artifact with task

How can I run features with another browser?

  • define browser system property -Dbrowser=<browser>
  • the id of the browser has to be defined in Sahi (<userdata>/config/browser_types.xml

How can I run features without a browser (headless/PhantomJS)?

Install PhantomJS en configure it in SAHI.  The original instructions are documented on the SAHI website, this is the customized version:

  1. Download PhantomJS for Windows and unzip phantomjs.exe to<sahi-dir>\ext\phantomjs
  2. Copy the de 6c) How-to / Q&A file to the phantomjs directory
  3. Add browsertype in <sahi-dir>\userdata\config\browser_types.xml (see below - adjust path to sahi installation directory )
  4. Restart the SAHI proxy
  5. PhantomJS is now available as browser with id phantomjs

 

PhantomJS browser type
<browserType>
  <name>phantomjs</name>
  <displayName>PhantomJS</displayName>
  <icon>safari.png</icon>
  <path>c:\sahi\ext\phantomjs\phantomjs.exe</path>
  <options>--proxy=localhost:9999 $userDir\..\ext\phantomjs\sahi.js</options>
  <processName>phantomjs.exe</processName>
  <capacity>100</capacity>
  <useSystemProxy>false</useSystemProxy>
</browserType>

How do I add parameters to the features run-environment?

Use property files and specify the environment.

Example:

  • suppose environment test
  • add parameters to the features with properties & variables.
  • fill-in default values (fe. development) in the property file test/resources/properties/default.properties
  • make a property file test/resources/properties/test.properties and change parameters to suite this environment 
  • run tests with systeem property -Denvironment=test

How to use variables and random values?

Properties

Pre-defined variables can be put in to properties in the separate property files . These files are located in the test/resources directory.

Each environment can have its own properties files. Properties files are loaded in the following order:

  1. default.properties
  2. <environment >.properties

The collection of available properties is the sum of  the 2 files , without doubles .  Properties with the same key in the first file ( default) will be overwritten by those from the second file (environment).  The second file is loaded only when there is an environment system property is specified (fe. -Denvironment=test).  

Its possible to load other files by edting the cucumber.xml .

Properties can be used just like variables in steps, with the  ${property} syntax.

Variables  

Contrary to properties, that can be shared by all scenario's, variables only exist within the scope of one scenario.

Variables must be defined in the scenario and can be used afterwards..

Just like properties, variables a used in steps via the ${variabele} syntax.  When there's no varable with a given key, a property is still sought.

Example use variables:

Given i save "http://www.foreach.be" as "homepage"
And i am on "${homepage}"

Random & unique values

A standard object, that renders a number of unique values, is available.  It's about all objects with the id.* reference.  See chapter  Standard objects.

How do i define my own steps and combine them with CWB libraries?

See document 6e) CWB for developers.

What are the relevant command-line properties?

Properties defined via the command-line will always overwrite values from the properties files.

Definition can be done like a Java system property, with syntax -Dpropname=propvalue.

nopause=truemake sure pause steps are skipped.
browser=<browser>Specify in which browser features are to be run . Each browser has to be configured in SAHI.
environment=<environment>Specify which environment has to be loaded : the name of the extra properties file that extends or overwrites the default properties.
tags=<tags>Comma separated list of tags features have to tagged with . Only features tagged with one of the listed tags will be run. Negative taggin is also possible with ~@tagname
takeScreenShotOnFailure=true

Takes a screenshot of the desktop screen as it is displayed while running the tests. default=false

The screenshot will show whatever is on the screen at the moment a scenario step fails. This means that your browser should not be minimized and be displayed on top of any other window that may be present.

Should the content of your screenshots be black, it could be you've run into the Windows session 0 isolation feature (http://msdn.microsoft.com/en-us/library/aa480152.aspx#appcomp_topic12). Make sure that the process that takes the screenshots does not run in session 0.

This feature will also embed the screenshots into the cucumber test reports.

exportDomOnFailure=trueUses javascript to export the DOM at the moment of scenario failure to a html file on disk. These pages are saved in 'target/cucumber/domdumps/'

How to tweak SAHI and and let it select the placeholder attribute?

Extend the SAHI concat.js file with the new attributes of your choice.  It has be done for each type in the addAD steps.

See the SAHI documentatie.

Why do I get strange encoding characters in certain steps or why do my steps with UTF-8 characters fail?

 

This happens sometimes if encoding is not specified at Cucumber JVM start-up. CWB uses the maven-antrun-plugin to start the Cucumber JVM like this :

            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <phase>integration-test</phase>
                        <configuration>
                            <tasks>
                                <java classname="cucumber.api.cli.Main" fork="yes">
                                    <!-- Respect UTF-8 encoding AND fork the cucumer JVM http://confluence.projects.foreach.be/x/ngGNAQ -->
                                    <sysproperty key="file.encoding" value="UTF-8"/>
                                    <classpath refid="maven.test.classpath"/>
                                    <arg line="--format com.foreach.cuke.core.formatter.ConsoleReporter"/>
                                    <arg line="--format html:${project.build.directory}/cucumber/${report.output.dir}plain-html-reports"/>
                                    <arg line="--format junit:${project.build.directory}/cucumber/${report.output.dir}cucumber-report.xml"/>
                                    <arg line="--format json:${project.build.directory}/cucumber/${report.output.dir}cucumber-report.json"/>
                                    <arg line="--glue com.foreach.cuke"/>
                                    <arg line="--glue be.mediafin.tests"/>
                                    <arg line="--tags ~@ignore"/>
                                    <arg line="--tags ${tags}"/>
                                    <arg line="${project.basedir}/features/${features}"/>
                                </java>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The <sysproperty key="file.encoding" value="UTF-8"/> en fork="yes" are important here.

If these parameters are not set for the <java/> ant task, Cucumber JVM will be started with the default encoding maven got started with.

On windows machine this is default Cp1252, so you can get strange character encodings like:

 

error:_click(_link("‘Ik geloof dat afzien uiteindelijk beloond wordt.'", _in(_div("/focusblock/i")))) 

Running from IntelliJ 14.1.x fails with Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/transaction/TransactionDefinition

This problem has been reported since IntelliJ IDEA 14.1.3.  The cause is incorrect detection of the Glue property, for some reason the package cucumber.api.spring is added incorrectly.

The solution is to manually define the glue (eg com.foreach.cuke) in the default run configuration.  You will have to remove all automatically generated run configuration, these will get generated correctly when you try to run again by right-clicking on the scenario or feature.