Acceptance Testing XVIII - Separate Environment Files

Using separate environment files to control environments used in a test


In the previous article, we created we used environment setting in acceptance.suite.php to run our test in separate browsers. In this one, we'll do the same thing, but with the environment code in separate files.

We'll still be using our T17_ResourceProtectionFinalCest test file.


MODX logo

This article assumes that you've installed and configured Composer, Codeception, PhpUnit, MODX, Java, WebDriver, and ChromeDriver as described in earlier articles, and that you've created the acceptance test support files from those articles.


Separate Files

When using separate environment files, you add envs: tests/_envs in the paths: section of your master _build/codeception.yml file (if it's not there already), like this:

paths:
    output: tests/_output
    data: tests/_data
    envs: tests/_envs

The last line tells Codeception where to look for the environment-specific .yml files: the tests/_envs directory. Suppose that you want to use separate environments for Chrome and Firefox. If you tell Codeception that you want to use Chrome, when it finishes reading the acceptance.suite.yml file, it will look in the tests/_envs directory for a file named chrome.yml. If that file is found, it will be read. Any settings in it will override matching settings read earlier. Settings that don't match anything read earlier will also be applied

You can create the specific environment .yml files by letting Codeception generate them. For example, go ahead and give the following four commands while in the _build directory. These are the environment files needed for the this article and the next one:

    codecept g:env chrome
    codecept g:env firefox
    codecept g:env modx2
    codecept g:env modx3
    

That will create the four files: chrome.yml, firefox.yml, modx2.yml, and modx2.yml in the tests/_envs/ directory. Remember that we specified that directory for environment files in the master _build/codeception.yml file. The final two won't be used until the following article.


The Code

Here is the code for the four .yml files (also available at GitHub) here:

chrome.yml

# `chrome` environment config goes here    
modules:
    config:
        WebDriver:
            url: 'http://localhost/test/'
            browser: 'chrome'
            window_size: 'maximize'
            wait: 5
extensions:
    enabled:
        - Codeception\Extension\RunProcess:
            - echo "Launch Selenium server in chrome.yml"
            - java -jar c:/Users/BobRay/Downloads/Selenium/selenium-server-standalone-3.141.59.jar
            - echo "Launch Chrome driver in chrome.yml"
            - C:/Users/BobRay/Downloads/selenium-drivers/chromedriver.exe

firefox.yml

# `firefox` environment config goes here
modules:
  config:
    WebDriver:
      url: 'http://localhost/test/'
      path: ''
      browser: 'firefox'
      window_size: 'maximize'
      wait: 5
extensions:
  enabled:

    - Codeception\Extension\RunProcess:
        - echo "launch selenium server in firefox.yml"
        - java -jar c:/Users/BobRay/Downloads/Selenium/selenium-server-standalone-3.141.59.jar
        - echo "Launch geckodriver in firefox.yml"
        - C:/Users/BobRay/Downloads/selenium-drivers/geckodriver.exe

modx2.yml

    # `modx2` environment config goes here
    bootstrap: _bootstrap2.php

    modules:
    config:
    WebDriver:
    url: 'http://localhost/test/'

modx3.yml

    # `modx3` environment config goes here
    bootstrap: _bootstrap3.php

    modules:
    config:
    WebDriver:
    url: 'http://localhost/modx3/'

Organization

The material in the separate files is exactly the same as the acceptance.suite.yml code from the individual parts of the env: section with two changes. First, the env: and environment_name: lines have been removed because they are unnecessary. Second, the remaining lines have been moved two indentation levels to the left.

Other than those changes, the code is identical — it has to be if it's going to override the code processed earlier.


Running Tests

Now you should be able to run any of the tests in Chrome or Firefox, but with the configuration coming from the chrome.yml or firefox.yml files. Try something like this:

codecept run acceptance T12_ContactFormCest --env firefox

You should see these messages in the output:

"launch selenium server in firefox.yml"
 ...
"Launch geckodriver in firefox.yml"

As we mentioned in the previous article, you can now run tests in both browsers in one run by doing something like this:

codecept run acceptance T12_ContactFormCest --env firefox --env chrome

Dual Code

At this point, you could remove (or comment out) the env: section of the acceptance.suite.yml file, since the same code is in the individual .yml files. The test will run with both sets of code, but it's generally a bad practice to have them both there. I can lead to confusion and hard-to-track errors in your tests.

It's recommended that you pick one method or the other. It's a matter of personal preference. Some people like having all the code in one file. Others prefer to put only code that applies to all environments in the acceptance.suite.yml file and have any environment-specific code in its own .yml file. It's up to you.

For the upcoming articles, we'll assume that you're using the four environment files we created in the previous article. You can comment out (or remove) the entire env: section of the acceptance.suite.yml file by adding a # at the beginning of each line of that section.

If you're using PhpStorm, you can do that by highlighting the entire env: section (including the env: line), and pressing "Ctrl-/" (the Ctrl key and the forward slash key next to the right shift key).

Be sure the envs: tests/_envs in the master _build/codeception.yml file is *not* commented out.


Coming Up

In the next article, we'll look at running a single test in multiple versions of MODX (MODX2 and MODX3) using Codeception environments and some tweaks to the code.



For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)