Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test with behat two sites in the same test with different environments (so different URLs)

I'm using Behat and Mink to test several sites. Two of them use Single Sign On, so one of my test includes login in one site, go to second site and check I'm logged in.

This is acomplished with this behat code:

Feature: SSO
  In order to login in all related sites
  As a anonymous user
  I want to login in just one site


Scenario: Login with normal user
  Given I am on "/user/login"
  When I fill in "name" with "username"
    And I fill in "pass" with "password"
    And I press "login"
  Then I should see "My account"
    And I go to "http://server.developer1.workstations/"
  Then I should see "Close session"

This works, but the problem is this line:

    And I go to "http://server.developer1.workstations/"

We have different environments (local devs, dev, stagging) with different URLs, so I can't write a test that works in every machine. The first server it's ok because I can have different base_url settings for each machine, but I can't find a way to set the second server as a general settings instead of rewritting every feature file changing that second server URL.

Is there any way to declare variables, constants or something that can be stored in a file that will be different for each machine?

like image 429
sanzante Avatar asked Nov 08 '12 13:11

sanzante


1 Answers

to solve this it's possible to:

  • Define master and slaves URL in a machine dependent files or environment variables. For example, behat.yml can be customized for every machine, with some extra params:
default:
  context:
    parameters:
       master_url: 'http://master_site.workstation1'
       slave1_url: 'http://.slave1.master_site.workstation1'
       slave2_url: 'http://.slave2.master_site.workstation1'
  extensions:
    Behat\MinkExtension\Extension:
      slave1_url: 'http://.slave1.master_site.workstation1'
      goutte: ~
      selenium2: ~

Then, include defined param into your own FeatureContext class:

class FeatureContext extends MinkContext
{
    /**
     * Initializes context.
     * Every scenario gets it's own context object.
     *
     * @param array $parameters context parameters (set them up through behat.yml)
     */
    public function __construct(array $parameters)
    {
        $this->site_urls = array(
          'master' => $parameters['master_url'],
          'slave1' => $parameters['slave_url1'],
          'slave2' => $parameters['slave_url2'],
        );

    }

Finally, define your own step:

/**
 * @Then /^I should be logged in master$/
 */
public function iShouldBeLoggedInMaster()
{
  $this->visit($this->site_urls['master']);
  $this->assertPageContainsText('Close session');      
}  

An scenario with this step check with base_url for a slave site could be:

Scenario: Login as normal user
  Given I am on "/user/login"  
  When I fill in "name" with "username"
    And I fill in "pass" with "password"
    And I press "Login"
  Then I should see "My account"
  And I should be logged in master

Then, include a new step to check if user is logged in master.

like image 176
sanzante Avatar answered Sep 28 '22 01:09

sanzante