Continuing on my previous post about setting up a Behat test environment on Windows 10 now we will create some tests to see how the setup is working. These are some basic tests, as we are learning together.For running test we need to create a “behat.yml” file which Behat use as configuration file by default. more about it on this link: http://docs.behat.org/en/v3.0/user_guide/configuration.html#behat-yml

You can also find some example files on the web.
My experimenting config file contains values sufficient for some first basic tests:

default:
  extensions:
    Behat\MinkExtension:
      base_url: 'http://wedoqa.com/'
      browser_name: chrome
      goutte: ~
      selenium2: ~
paths:
    features: features
    bootstrap: %behat.paths.features%/Context

This configuration will:
– use http://wedoqa.com/ as base page to load on start,
– run tests in chrome (if browser is required),
– use goutte and selenium2 basic configurations,
– setup paths for Features and FratureContext.

In order to run tests with Selenium2 known as Webdriver we will need to download the standalone server from http://www.seleniumhq.org/download/.
Currently it is 3.0.0-beta2 version is the latest, but for certain stability i have chosen 2.53.1 version for this examples. For your choice i leave it up to you. Previous releases can be found on link http://selenium-release.storage.googleapis.com/index.html.

Once you have downloaded the standalone server, also we need to download ChromeDriver and other drivers for browser. Most of them you can find linked at the http://www.seleniumhq.org/download/ and download those as well. For ChromeDriver follow link https://sites.google.com/a/chromium.org/chromedriver/. Best way is to safe the drivers in a separate folder e.g. “C:/webdrivers” and add the to system environment variable path. Save the selenium server in the Behat folder or by choice.

To run the selenium server by opening a Command Prompt, selecting the path to the server and run command line:
“java -jar selenium-server-standalone-2.53.1.jar”

Features:
To create tests Behat is using features to write down the test scenarios and steps.
More about that you can read from official documentation http://docs.behat.org/en/v3.0/user_guide/features_scenarios.html. All features should be saved into features folder of behat, in my example it is “C:/behat/features”

For this test examples I have experimented a bit with approaches in Gherkin language and background implementation of various steps.
Created “homepage.feature” with two scenarios:

Feature: Homepage Wedoqa
    In order to visit the WeDoQA website
    As a website visitor
    I need to see the start page with links in header

    @javascript
    Scenario: Check homepage content
       Given I am on "/"
       When I click "References"
       Then I should see "Meet the team"

    @javascript
    Scenario: Check homepage content
       Given I am on "/"
       When I click "Contact info"
       Then I should see "office@wedoqa.com"

Some explanation:
1. Behat is using PantomJS headless driver without an actual browser by default, once we add the “@javascript” tag it will recognize is as a command to use and actual browser, a Chrome in our case.
2. The step -Given I am on “/”– states that we are on the homepage of the website, you can add extensions to link to open a different page immediately at load
3. The step –When I click “LINK”– is a command to Gherkin/Behat to click on a link based on a text in our case, with a background PHP function, explained later
4. The step — Then I should see “SOMETHING” — is a command to Gherkin/Behat to search for a test on some element, in first scenario it is a plain test field, on the second scenario it is text on a button, without any background code.

FeatureContext (http://docs.behat.org/en/v2.5/guides/4.context.html):
By initializing the test framework with command line command “behat –init” will automatically create and empty FeatureContext.php file under “C:/Behat/features/bootstrap” path.

By default it will have some imports usually:

    use Behat\MinkExtension\Context\MinkContext;
    use Behat\Gherkin\Node\PyStringNode,
        Behat\Gherkin\Node\TableNode;

For addition I have added for scenario event handling:
use Behat\Behat\Event\ScenarioEvent;

The primary FeatureContext will extend MinkContext in order to be able to use some goodies form Mink:

    /**
     * Defines application features from the specific context.
     */
    class FeatureContext extends MinkContext

Added some protected variables and setup of main constructor (which is empty by default):

    /**
	 * @var \RemoteWebDriver
	 */
	 protected $webDriver;
	 protected $session;
	 
    /**
     * Initializes context.
     *
     * Every scenario gets its own context instance.
     * You can also pass arbitrary arguments to the
     * context constructor through behat.yml.
     */
    public function __construct()
    {
		$this->webDriver = new \Behat\Mink\Driver\Selenium2Driver();
		$this->session = new \Behat\Mink\Session($this->webDriver);
    }

NOTE: you can use aliases for shorter typing, but in this example I left all as it is, for simplicity.

I struggled to maximize the window of browser on initial test runs, that caused me some issues in testing, but thanks to James Morgan (https://gist.github.com/jimjam88/6596194) a bypass was made and i added it into a beforeScenario section:

    /** @BeforeScenario */
	public function before($event)
	{
		$wd = $this->getSession()->getDriver()->getWebDriverSession();
		$wd->window($wd->window_handle())->maximize();
	}
	
	/** @AfterScenario */
	public function after($event)
	{
		$this->getSession()->stop();
	}

The @BeforeScenario and @AfterScenario tags will give us additional handlers what to happen between scenarios. As mentioned before each scenario the code from James Morgan will maximize the browser so we can have better handling. In the after scenario section i decided to stop the Session in order to force Behat to reopen the browser for each scenario. Remowing that code itself will execute the test faster in a single test, but for some own purposes i did it this way.

Gherkin/Behat mostly can create background code to execute commands, and it is well set, but as mentioned i decided to handle the links on my own, so i created a function:

    /**
     * @When I click :arg1
     */
    public function iClick($arg1)
    {
        $clickOn = $this->getSession()
                        ->getPage()
                        ->find("xpath",".//a[text()='" . $arg1 . "']")
                        ->click();
    }

NOTE: Gherkin recognize commands as “I press” for buttons and “I click” on elements and links and “I follow” will search for links, i did some experiment and came to this solution. Feel free to experiment yourself as well. Maybe you will find more interesting solutions.

Hope you liked the example and if you have some comments and/or questions please write me in the section bellow. Thank you for reading.

Posted By Arnold Gergelj

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>