{"id":13711890,"url":"https://github.com/crossbrowsertesting/selenium-php-behat","last_synced_at":"2025-05-06T21:32:36.766Z","repository":{"id":55951819,"uuid":"70067787","full_name":"crossbrowsertesting/selenium-php-behat","owner":"crossbrowsertesting","description":"A quick guide to using Behat with CBT","archived":true,"fork":false,"pushed_at":"2020-12-04T18:50:27.000Z","size":16,"stargazers_count":4,"open_issues_count":4,"forks_count":7,"subscribers_count":9,"default_branch":"master","last_synced_at":"2024-11-13T22:35:11.163Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/crossbrowsertesting.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-10-05T14:22:50.000Z","updated_at":"2023-12-13T18:13:50.000Z","dependencies_parsed_at":"2022-08-15T10:10:44.856Z","dependency_job_id":null,"html_url":"https://github.com/crossbrowsertesting/selenium-php-behat","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-php-behat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-php-behat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-php-behat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crossbrowsertesting%2Fselenium-php-behat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crossbrowsertesting","download_url":"https://codeload.github.com/crossbrowsertesting/selenium-php-behat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252772189,"owners_count":21801871,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T23:01:12.665Z","updated_at":"2025-05-06T21:32:36.466Z","avatar_url":"https://github.com/crossbrowsertesting.png","language":"PHP","funding_links":[],"categories":["By Technology"],"sub_categories":["PHP"],"readme":"\u003cimg src=\"https://crossbrowsertesting.com/design/images/brand/cbt-sb-logo.svg\" width=\"50%\"\u003e\n\n----\n\n# Behat and CBT\n\n[Behat](http://behat.org/en/latest/) is a [Behavior Driven Development](https://dannorth.net/introducing-bdd/) test framework for PHP. The power of Behat, and BDD in general, is that your tests define how your application should *behave* in different *scenarios*. More than just confirming that parts of your application work, BDD helps define and clarify the overall design of your application. And since Behat/BDD tests are writen in [plain English](http://docs.behat.org/en/v2.5/guides/1.gherkin.html), they can be understood by devs, managers, marketers, and everyone else involved with your application. \n\n## Getting set up\n\n*This example is for Behat 2, if you are using Behat 3, please look at our [Behat3 Example](https://github.com/crossbrowsertesting/selenium-php-behat3)*\n\n### Installing Composer\n\nFirst, make sure you have PHP Dependency Manager [**Composer**](https://getcomposer.org/) installed. Follow the instructions on their [download page](https://getcomposer.org/download/) to install it.\n\n### Installing Behat\n\nOnce you have Composer, the easiest way to get set up would be to clone this repository, then run `composer install` to install the modules listed in `composer.json` (and their dependencies). \n\n### Setting up conf.yml files\n\nIn order to run any tests, you'll need to make a few changes to the configuration files located in `config/`.\n\nIn order to run tests, you'll need a valid credentials, so set `user` to the email address associated with your crossbrowsertesting.com account, and set `key` to your authkey. The easiest way to find your authkey by going to your [account page](https://crossbrowsertesting.com/account).\n\nYou can also change the `capabilities` and `browers` keys to rename/re-version your test, extend the `max_duration`, or change your target browser(s).\n\nTo find the os_api_name and browser_api_name that correspond to your targeted platform, you can either go to CrossBrowserTesting's [Selenium Page](https://app.crossbrowsertesting.com/selenium/run), open the wizard, and watch the capabilities in the sample script change as you select different browers, OR you can parse the os/browser you want out of the JSON response from https://crossbrowsertesting.com/api/v3/selenium/browsers\n\nIf you run into any trouble or have any questions, send an email to info@crossbrowsertesting.com.\n\n## Writing tests\n\nNow that the environment is set up, we can start actually writing tests. There are two main steps to writing test with Behat: define application behavior as scenarios and steps, then code each step as a function. \n\n### Defining behavior in `*.feature` files\n\nAccurately and concisely describing application behavior is the core of BDD. Behat (and most other BDD frameworks) use the [Gherkin](https://github.com/cucumber/cucumber/wiki/Gherkin) language to write real automated tests in plain English. \n\nLet's walk through writing a couple simple tests for our example [login page](http://crossbrowsertesting.github.io/login-form.html). It's a simple page, so our tests won't be very long.\n\nLet's start by defining what our feature is in `features/login.feature` \n\n```gherkin\nFeature: Login to page \n    As a user \n    I should be able to log in \n    If I supply valid user credentials\n```\n\nSimple enough. The whole block will be displayed at the start of the test, so make sure that it says something useful. \n\n\nNext, we define how our application should behave in different scenarios. For this simple login page there are only two scenarios we need to test: a valid login, and an invalid login.\n\n```gherkin\nScenario: Login with bad credentials\n    Given I go to \"http://crossbrowsertesting.github.io/login-form.html\"\n    When I fill in \"#username\" with \"badusername@crossbrowsertesting.com\"\n    And I fill in \"#password\" with \"badpassword\"\n    And I press \".form-actions \u003e button\" \n    Then I should see \".alert-danger\" say \"Username or password is incorrect\" \n\nScenario: Login with good credentials\n    Given I go to \"http://crossbrowsertesting.github.io/login-form.html\"\n    When I fill in \"#username\" with \"tester@crossbrowsertesting.com\"\n    And I fill in \"#password\" with \"test123\"\n    And I press \".form-actions \u003e button\" \n    Then I should see \"#logged-in-message\u003ep\" say \"You are now logged in!\" \n```\n\nAs you can see, we use plain english to describe the setup (being on the right page), the actions for the test (filling in username and password then clicking the login button), and the desired outcome (the message we see). \n\nThe Gherkin language can handle some very complicated test logic, so I recommend taking a look at Behat's official guide to [writing features with Gherkin](http://docs.behat.org/en/v2.5/guides/1.gherkin.html).\n\n### Coding steps in the `FeatureContext.php` file\n\nNow that our feature is defined, we can run our tests with `$ bin/behat`. \n\nYou should see an output like this:\n\n```\n2 scenarios (2 undefined)\n10 steps (10 undefined)\n0m0.011s\n\nYou can implement step definitions for undefined steps with these snippets:\n\n    /**\n     * @Given /^I go to \"([^\"]*)\"$/\n     */\n    public function iGoTo($arg1)\n    {\n        throw new PendingException();\n    }\n\n    [...]\n\n```\n\nSo, we can see that Behat parsed `login.feature` but wasn't able to find functions in `features/bootstrap/FeatureContext.php` to tell it how to execute each step. \n\nBut we can copy the functions it generates and paste them into our `FeatureContext.php` file to give us a starting place. \n\nThe comment above each function describes the Regex that it uses to link steps in the `login.feature` file to the function, as well as how arguments get passed from the step to the function.\n\nNow we just have to write some Selenium code to define each step! This isn't a Selenium guide, so I'll just point you towards Facebook's [excellent documentation](https://facebook.github.io/php-webdriver/). If you encounter any trouble, be sure to shoot an email to info@crossbrowsertesting.com and we'll be able to give you a hand.\n\n## Running tests\n\nNow that everything is set up, let's try running the test! \n\n```\n$ bin/behat -c config/single.conf.yml\n\nFeature: Test a login form\n\n  Scenario: Login with bad credentials                                        # /Users/johnreese/Google Drive/CBT/repos/behat/features/single/login.feature:3\n    Given I go to \"http://crossbrowsertesting.github.io/login-form.html\"      # FeatureContext::iGoTo()\n    When I fill in \"#username\" with \"badusername@crossbrowsertesting.com\"     # FeatureContext::iFillInWith()\n    And I fill in \"#password\" with \"badpassword\"                              # FeatureContext::iFillInWith()\n    And I press \".form-actions \u003e button\"                                      # FeatureContext::iPress()\n    Then I should see \".alert-danger\" say \"Username or password is incorrect\" # FeatureContext::iShouldSeeSay()\n\n  Scenario: Login with good credentials                                       # /Users/johnreese/Google Drive/CBT/repos/behat/features/single/login.feature:10\n    Given I go to \"http://crossbrowsertesting.github.io/login-form.html\"      # FeatureContext::iGoTo()\n    When I fill in \"#username\" with \"tester@crossbrowsertesting.com\"          # FeatureContext::iFillInWith()\n    And I fill in \"#password\" with \"test123\"                                  # FeatureContext::iFillInWith()\n    And I press \".form-actions \u003e button\"                                      # FeatureContext::iPress()\n    Then I should see \"#logged-in-message\u003ep\" say \"You are now logged in!\"     # FeatureContext::iShouldSeeSay()\n\n2 scenarios (2 passed)\n10 steps (10 passed)\n0m43.527s\n\n```\n\nSuccess!\n\n### Parallel execution\n\nBut how can we run the test on multiple browsers at once? Easy! This repo includes a file called `run-parallel.php` which can help manage parallel execution across multiple browsers at once. \n\nI've included a configuration file that will run tests on IE8 through IE11 as well as Edge called `ie.conf.yml`. It is identical to `single.conf.yml`, except that it lists more than one browser.\n\nTo start the test, just run our helper file like this:\n\n```\n$ php run-parallel.php -c config/ie.conf.yml\n```\n\n### Websites behind your firewall\n\nIf you have our Node module `cbt_tunnels` installed, you can run tests to websites behind your firewall by running `run-parallel.php` with the -l flag. \n\nIf you don't have `cbt_tunnels` installed, you can install it by running `$ npm install -g cbt_tunnels`.\n\n## Help! \n\nIf you got stuck, or something doesn't make sense, don't worry! Just shoot an email to info@crossbrowsertesting.com and we'll help you out. \n\n### Happy testing!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossbrowsertesting%2Fselenium-php-behat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrossbrowsertesting%2Fselenium-php-behat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrossbrowsertesting%2Fselenium-php-behat/lists"}