Loading...

Adding Nightwatch tests to the pipeline

This page contains instructions to add Nightwatch tests to your Code Studio pipeline and ensure that your Drupal projects are tested efficiently.

Prerequisites

Visit the following before you add Nightwatch tests to your pipeline:

Implementing a Nightwatch test job in the Code Studio pipeline

Adding a Nightwatch test to your project

  1. Create the following path from your root directory:
    tests/src/Nightwatch/Tests
  2. Create a file named createDrupalLoginScreen.js in the Tests directory.
  3. Update the file with the following:

    module.exports = {
    
      '@tags': ['myproject'],
    
      before(browser) {
    
        browser.globals.drupalSitePath = 'sites/default';
    
      },
    
      'Test login screen': (browser) => {
    
        browser
    
          .url(`${process.env.DRUPAL_TEST_BASE_URL}/user/login`)
    
          .waitForElementVisible('body', 1000)
    
          .assert.visible('input[name="name"]', 'Login input field is visible')
    
          .assert.visible('input[name="pass"]', 'Password input field is visible')
    
          .assert.visible('input#edit-submit', 'Submit button is visible');
    
      },
    
    };

Adding the nightwatch.conf.js file

  1. Create the following path from your root directory:
    tests/Drupal/Nightwatch
  2. Create a file named nightwatch.conf.js in the Nightwatch directory.
  3. Update the file with the following:

    const collectedFolders = {
    
      Tests: ["./src/Nightwatch/Tests"],
    
      Commands: [],
    
      Assertions: [],
    
      Pages: [],
    
    };
    
    module.exports = {
    
      src_folders: collectedFolders.Tests,
    
      output_folder: process.env.DRUPAL_NIGHTWATCH_OUTPUT,
    
      custom_commands_path: collectedFolders.Commands,
    
      custom_assertions_path: collectedFolders.Assertions,
    
      page_objects_path: collectedFolders.Pages,
    
      webdriver: {
    
        start_process: true,
    
        server_path: "/usr/local/bin/chromedriver",
    
        port: 9515
    
      },
    
      test_settings: {
    
        default: {
    
          default_path_prefix: process.env.DRUPAL_TEST_WEBDRIVER_PATH_PREFIX || '',
    
          desiredCapabilities: {
    
            browserName: "chrome",
    
            chromeOptions: {
    
              args: ["--headless", "--no-sandbox", "--disable-gpu"]
    
            }
    
          }
    
        }
    
      }
    
    };

Adding the .env.example file

  1. Create a file named .env.example in your /tests directory.
  2. Update the file with the following contents:

    DRUPAL_TEST_BASE_URL=http://127.0.0.1:8888
    
    DRUPAL_TEST_DB_URL=mysql://drupal:[email protected]/drupal
    
    DRUPAL_TEST_WEBDRIVER_HOSTNAME=localhost
    
    DRUPAL_TEST_WEBDRIVER_PORT=9515
    
    DRUPAL_TEST_CHROMEDRIVER_AUTOSTART=true
    
    DRUPAL_NIGHTWATCH_OUTPUT=reports/nightwatch

Updating the .gitignore file

Use the .gitignore file that is located in the Drupal repository.

Creating or updating the .yarnrc.yml file

Ensure that your .yarnrc.yml file in the tests directory contains the following:

nodeLinker: node-modules

Updating the package.json file

  1. If you do not have the package.json file in the tests directory, use the file that is located in the Drupal repository. Modify it according to your requirements.
  2. Update your package.json file with the following:

    "scripts": {
      "test:nightwatch": "node -r dotenv-safe/config ./node_modules/.bin/nightwatch --config ./Drupal/Nightwatch/nightwatch.conf.js"
    }

Updating the .gitlab-ci.yml file

  1. Add the following job in the .gitlab-ci.yml file:

    "Nightwatch Test":
    
      extends: "Test Drupal"
    
      variables:
    
        ACQUIA_TASKS_SETUP_DRUPAL: "true"
    
        ACQUIA_TASKS_SETUP_DRUPAL_STRATEGY: "install"
    
        ACQUIA_TASKS_SETUP_DRUPAL_PROFILE: "minimal"
    
        ACQUIA_TASKS_SETUP_DRUPAL_CONFIG_IMPORT: "false"
    
      before_script:
    
        - eval $YARN_PACKAGE
    
        - eval $YARN_PATH
    
      stage: "Test Drupal"
    
      script:
    
        - !reference [.clone_standard_template, script]
    
        - . "$STANDARD_TEMPLATE_PATH"/ci-files/scripts/utility/setup_prereqs.sh
    
        - . "$STANDARD_TEMPLATE_PATH"/ci-files/scripts/utility/config_ssh.sh
    
        - . "$STANDARD_TEMPLATE_PATH"/ci-files/scripts/utility/composer_install.sh
    
        - . "$STANDARD_TEMPLATE_PATH"/ci-files/scripts/utility/install_drupal.sh
    
        - $CI_PROJECT_DIR/vendor/bin/drush runserver 127.0.0.1:8888 &
    
        - cd tests
    
        - cp .env.example .env
    
        - yarn install
    
        - yarn run test:nightwatch
    
      allow_failure: true
    
      artifacts:
    
        when: always
    
        paths:
    
          - tests/reports/nightwatch/
    
        expire_in: 7 days
  2. Ensure that the following variables are set in your job:

  • ACQUIA_TASKS_SETUP_DRUPAL must be set to true.
  • ACQUIA_TASKS_SETUP_DRUPAL_CONFIG_IMPORT can be true or false.
  • ACQUIA_TASKS_SETUP_DRUPAL_STRATEGY can be install or sync.
  • ACQUIA_TASKS_SETUP_DRUPAL_PROFILE can be minimal, standard, or other allowed values.

Did not find what you were looking for?

If this content did not answer your questions, try searching or contacting our support team for further assistance.

Back to Section navigation