├── .gitignore ├── .gitpod.Dockerfile ├── .gitpod.yml ├── README.md ├── bin ├── behat ├── yaml-lint └── yaml-lint.bat ├── composer.json ├── config ├── parallel.conf.yml └── single.conf.yml ├── exec-parallel.php ├── features ├── bootstrap │ └── FeatureContext.php └── single.feature └── lib └── LambdaContext.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | composer.lock -------------------------------------------------------------------------------- /.gitpod.Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gitpod/workspace-full 2 | 3 | # Install custom tools, runtime, etc. 4 | RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 5 | RUN php -r "if (hash_file('sha384', 'composer-setup.php') === '906a84df04cea2aa72f40b5f787e49f22d4c2f19492ac310e8cba5b96ac8b64115ac402c8cd292b8a03482574915d1a8') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" 6 | RUN php composer-setup.php 7 | RUN php -r "unlink('composer-setup.php');" 8 | RUN sudo rm /usr/bin/composer 9 | RUN sudo mv composer.phar /usr/bin/composer -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | image: 2 | file: .gitpod.Dockerfile 3 | 4 | # List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/config-start-tasks/ 5 | tasks: 6 | - init: composer install && chmod +x ./bin/behat 7 | command: composer single 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Run Selenium Tests With Behat On LambdaTest 2 | 3 | ![image](https://user-images.githubusercontent.com/70570645/171988795-ed884ca8-f431-48b3-afcc-91a014fd5059.png) 4 | 5 |

6 | Blog 7 |   ⋅   8 | Docs 9 |   ⋅   10 | Learning Hub 11 |   ⋅   12 | Newsletter 13 |   ⋅   14 | Certifications 15 |   ⋅   16 | YouTube 17 |

18 |   19 |   20 |   21 | 22 | *Learn how to use Behat framework to configure and run your PHP automation scripts on the LambdaTest platform* 23 | 24 | [](https://accounts.lambdatest.com/register?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) 25 | 26 | ## Table Of Contents 27 | 28 | * [Pre-requisites](#pre-requisites) 29 | * [Run Your First Test](#run-your-first-test) 30 | * [Parallel Testing With Behat](#running-parallel-tests-using-behat) 31 | * [Local Testing With Behat](#testing-locally-hosted-or-privately-hosted-projects) 32 | 33 | ## Prerequisites For Running Behat Selenium Scripts 34 | 35 | Before you begin automation testing with Selenium and Behat, you would need to: 36 | 37 | * Make sure that you have the latest **PHP** installed on your system. You can download and install **PHP** using following commands in the terminal: 38 | 39 | * **MacOS:** Previous versions of **MacOS** have **PHP** installed by default. But for the latest **MacOS** versions starting with **Monterey**, **PHP** has to be downloaded and installed manually by using below commands: 40 | ```bash 41 | /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 42 | brew install php 8 43 | ``` 44 | * **Windows:** 45 | ```bash 46 | sudo apt-get install curl libcurl3 libcurl3-dev php 47 | ``` 48 | **Note:** For **Windows**, you can download **PHP** from [here](http://windows.php.net/download/). Also, refer to this [documentation](http://php.net/manual/en/install.windows.php) for ensuring the accessibility of PHP through Command Prompt(cmd). 49 | 50 | * Download **composer** in the project directory ([Linux/MacOS](https://getcomposer.org/download/), [Windows](https://getcomposer.org/doc/00-intro.md#installation-windows)). 51 | 52 | **Note:** To use the **composer** command directly, it either should have been downloaded in the project directory or should be accessible globally which can be done by the command below: 53 | 54 | ```bash 55 | mv composer.phar /usr/local/bin/composer 56 | ``` 57 | ### Installing Selenium Dependencies And Tutorial Repo 58 | 59 | **Step 1:** Clone the LambdaTest’s Behat-Selenium-sample repository and navigate to the code directory as shown below: 60 | ```bash 61 | git clone https://github.com/LambdaTest/behat-selenium-sample 62 | cd behat-selenium-sample 63 | ``` 64 | **Step 2:** Install the composer dependencies in the current project directory using the command below: 65 | ```bash 66 | composer install 67 | ``` 68 | **Step 3:** Set up **Behat** automation for your OS using composer. 69 | ```bash 70 | php composer.phar require behat/behat 71 | ``` 72 | 73 | ### Setting Up Your Authentication 74 | 75 | Make sure you have your LambdaTest credentials with you to run test automation scripts. You can get these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/build?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) or by your [LambdaTest Profile](https://accounts.lambdatest.com/login?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample). 76 | 77 | **Step 2:** Set LambdaTest **Username** and **Access Key** in environment variables. 78 | 79 | * For **Linux/macOS**: 80 | 81 | ```bash 82 | export LT_USERNAME="YOUR_USERNAME" 83 | export LT_ACCESS_KEY="YOUR ACCESS KEY" 84 | ``` 85 | * For **Windows**: 86 | ```bash 87 | set LT_USERNAME="YOUR_USERNAME" 88 | set LT_ACCESS_KEY="YOUR ACCESS KEY" 89 | ``` 90 | 91 | ## Run Your First Test 92 | 93 | >**Test Scenario**: Checkout sample [FeatureContext.php](https://github.com/LambdaTest/behat-selenium-sample/blob/master/features/bootstrap/FeatureContext.php) file. This Behat script tests a sample to-do list app by marking couple items as done, adding a new item to the list and finally displaying the count of pending items as output. 94 | 95 | ```bash 96 | Feature: LambdaTest TodoApp Functionality 97 | 98 | Scenario: Check and Verify 99 | Given I am on "https://lambdatest.github.io/sample-todo-app/" 100 | When I click on checkboxes 101 | And I add checkbox with text "New check box" 102 | Then I get checkbox text as "New check box" 103 | ``` 104 | 105 | ### Configuration of Your Test Capabilities 106 | 107 | **Step 5:** In [single.conf.yml](https://github.com/LambdaTest/behat-selenium-sample/blob/master/config/single.conf.yml), you need to update your test capabilities like OS, browser, browser version and so on. 108 | 109 | You can generate capabilities for your test requirements with the help of our inbuilt [Desired Capability Generator](https://www.lambdatest.com/capabilities-generator/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample). 110 | 111 | 112 | ### Executing The Test 113 | 114 | **Step 6:** The tests can be executed in the terminal using the following command: 115 | 116 | ```bash 117 | composer single 118 | ``` 119 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on LambdaTest Automation Dashboard. LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests. 120 | 121 | ## Running Parallel Tests Using Behat 122 | 123 | To run parallel tests using **Behat**, we would have to execute the below command in the terminal: 124 | 125 | ```bash 126 | composer parallel 127 | ``` 128 | 129 | ## Testing Locally Hosted Or Privately Hosted Projects 130 | 131 | You can test your locally hosted or privately hosted projects with LambdaTest Selenium grid using LambdaTest Tunnel. All you would have to do is set up an SSH tunnel using tunnel and pass toggle `tunnel = True` via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are live. 132 | 133 | Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) for more information. 134 | 135 | Here’s how you can establish LambdaTest Tunnel. 136 | 137 | Download the binary file of: 138 | * [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip) 139 | * [LambdaTest Tunnel for macOS](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip) 140 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip) 141 | 142 | Open command prompt and navigate to the binary folder. 143 | 144 | Run the following command: 145 | 146 | ```bash 147 | LT -user {user’s login email} -key {user’s access key} 148 | ``` 149 | So if your user name is lambdatest@example.com and key is 123456, the command would be: 150 | 151 | ```bash 152 | LT -user lambdatest@example.com -key 123456 153 | ``` 154 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below : 155 | 156 | **Tunnel Capability** 157 | ``` 158 | "tunnel" => true 159 | ``` 160 | 161 | ## Documentation & Resources :books: 162 | 163 | 164 | Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing. 165 | 166 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) 167 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) 168 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) 169 | 170 | ## LambdaTest Community :busts_in_silhouette: 171 | 172 | The [LambdaTest Community](https://community.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe 🌎 173 | 174 | ## What's New At LambdaTest ❓ 175 | 176 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/) 177 | 178 | ## About LambdaTest 179 | 180 | [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs. 181 | 182 | ### Features 183 | 184 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments. 185 | * Real-time cross browser testing on 3000+ environments. 186 | * Test on Real device cloud 187 | * Blazing fast test automation with HyperExecute 188 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale. 189 | * Smart Visual Regression Testing on cloud 190 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more. 191 | * Automated Screenshot testing across multiple browsers in a single click. 192 | * Local testing of web and mobile apps. 193 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems. 194 | * Geolocation testing of web and mobile apps across 53+ countries. 195 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports 196 | 197 | 198 | [](https://accounts.lambdatest.com/register?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) 199 | 200 | 201 | 202 | ## We are here to help you :headphones: 203 | 204 | * Got a query? we are available 24x7 to help. [Contact Us](mailto:support@lambdatest.com) 205 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=behat-selenium-sample) 206 | -------------------------------------------------------------------------------- /bin/behat: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | handle = fopen($opened_path, $mode); 33 | $this->position = 0; 34 | 35 | // remove all traces of this stream wrapper once it has been used 36 | stream_wrapper_unregister('composer-bin-proxy'); 37 | 38 | return (bool) $this->handle; 39 | } 40 | 41 | public function stream_read($count) 42 | { 43 | $data = fread($this->handle, $count); 44 | 45 | if ($this->position === 0) { 46 | $data = preg_replace('{^#!.*\r?\n}', '', $data); 47 | } 48 | 49 | $this->position += strlen($data); 50 | 51 | return $data; 52 | } 53 | 54 | public function stream_cast($castAs) 55 | { 56 | return $this->handle; 57 | } 58 | 59 | public function stream_close() 60 | { 61 | fclose($this->handle); 62 | } 63 | 64 | public function stream_lock($operation) 65 | { 66 | return $operation ? flock($this->handle, $operation) : true; 67 | } 68 | 69 | public function stream_tell() 70 | { 71 | return $this->position; 72 | } 73 | 74 | public function stream_eof() 75 | { 76 | return feof($this->handle); 77 | } 78 | 79 | public function stream_stat() 80 | { 81 | return fstat($this->handle); 82 | } 83 | 84 | public function stream_set_option($option, $arg1, $arg2) 85 | { 86 | return true; 87 | } 88 | } 89 | } 90 | 91 | if (function_exists('stream_wrapper_register') && stream_wrapper_register('composer-bin-proxy', 'Composer\BinProxyWrapper')) { 92 | include("composer-bin-proxy://" . __DIR__ . '/..'.'/vendor/behat/behat/bin/behat'); 93 | exit(0); 94 | } 95 | } 96 | 97 | include __DIR__ . '/..'.'/vendor/behat/behat/bin/behat'; 98 | -------------------------------------------------------------------------------- /bin/yaml-lint: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | dir=$(cd "${0%[/\\]*}" > /dev/null; cd ../vendor/symfony/yaml/Resources/bin && pwd) 4 | 5 | if [ -d /proc/cygdrive ]; then 6 | case $(which php) in 7 | $(readlink -n /proc/cygdrive)/*) 8 | # We are in Cygwin using Windows php, so the path must be translated 9 | dir=$(cygpath -m "$dir"); 10 | ;; 11 | esac 12 | fi 13 | 14 | "${dir}/yaml-lint" "$@" 15 | -------------------------------------------------------------------------------- /bin/yaml-lint.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | setlocal DISABLEDELAYEDEXPANSION 3 | SET BIN_TARGET=%~dp0/../vendor/symfony/yaml/Resources/bin/yaml-lint 4 | php "%BIN_TARGET%" %* 5 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "require": { 3 | "behat/behat": "^3.14.0", 4 | "php-webdriver/webdriver": "^1.15", 5 | "symfony/yaml": "*", 6 | "ext-zip": "*" 7 | }, 8 | "config": { 9 | "bin-dir": "bin/" 10 | }, 11 | "scripts": { 12 | "test": "composer single && composer parallel", 13 | "single": "./bin/behat --config=config/single.conf.yml", 14 | "parallel": "CONFIG_FILE=config/parallel.conf.yml /usr/bin/env php exec-parallel.php" 15 | }, 16 | "autoload": { 17 | "classmap": [ 18 | "lib/" 19 | ] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /config/parallel.conf.yml: -------------------------------------------------------------------------------- 1 | default: 2 | autoload: 3 | '': '%paths.base%/../features/bootstrap' 4 | suites: 5 | default: 6 | paths: [ '%paths.base%/../features' ] 7 | contexts: 8 | - FeatureContext: 9 | parameters: 10 | server: "hub.lambdatest.com" 11 | user: "YOUR LT_USERNAME" 12 | key: "YOUR LT_ACCESS_KEY" 13 | capabilities: 14 | # common capabilities added for every test 15 | build: "behat-selenium-sample" 16 | environments: 17 | # each capability in this list is execute as single test 18 | # so to run multiple tests in parallel, 19 | # simply define multiple capabilities below and execute the test with 20 | # $ php run-parallel.php -c config/.yml 21 | - 22 | browserName: chrome 23 | browserVersion: latest 24 | platformName: Windows 10 25 | name: "parallel behat test latest" 26 | - 27 | browserName: chrome 28 | browserVersion: latest-1 29 | platformName: Windows 10 30 | name: "parallel behat test latest-1" 31 | - 32 | browserName: chrome 33 | browserVersion: latest-2 34 | platformName: Windows 10 35 | name: "parallel behat test latest-2" 36 | - 37 | browserName: chrome 38 | browserVersion: latest-3 39 | platformName: Windows 10 40 | name: "parallel behat test latest-3" 41 | - 42 | browserName: chrome 43 | browserVersion: latest-4 44 | platformName: Windows 10 45 | name: "parallel behat test latest-4" 46 | -------------------------------------------------------------------------------- /config/single.conf.yml: -------------------------------------------------------------------------------- 1 | default: 2 | autoload: 3 | '': '%paths.base%/../features/bootstrap' 4 | suites: 5 | default: 6 | paths: [ '%paths.base%/../features' ] 7 | contexts: 8 | - FeatureContext: 9 | parameters: 10 | server: "hub.lambdatest.com" 11 | user: "YOUR LT_USERNAME" 12 | key: "YOUR LT_ACCESS_KEY" 13 | capabilities: 14 | build: "behat-selenium-sample" 15 | name: "single-behat-test" 16 | environments: 17 | - 18 | browserName: chrome 19 | browserVersion: latest 20 | platformName: Windows10 21 | 22 | 23 | -------------------------------------------------------------------------------- /exec-parallel.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | $value) { 21 | // TEST_RUN_ID=0 ./bin/behat --config=single.conf.yml 2>&1 22 | putenv("TEST_RUN_ID=$index"); 23 | $cmd = realpath("./bin/behat") . " --config=" . $config_file . " 2>&1\n"; 24 | print $value['platformName'] . ", " . $value['browserName'] . ", " . $value['browserVersion'] . "\n"; 25 | $procs[$index] = popen($cmd, "r"); 26 | } 27 | 28 | foreach ($procs as $key => $value) { 29 | while (!feof($value)) { 30 | print fgets($value); 31 | } 32 | pclose($value); 33 | } 34 | 35 | ?> 36 | -------------------------------------------------------------------------------- /features/bootstrap/FeatureContext.php: -------------------------------------------------------------------------------- 1 | get($url); 12 | } 13 | 14 | /** 15 | * @When I click on checkboxes 16 | */ 17 | public function iClickOnCheckboxes() { 18 | $element = self::$driver->findElement(WebDriverBy::name("li1")); 19 | $element->click(); 20 | $element2 = self::$driver->findElement(WebDriverBy::name("li2")); 21 | $element2->click(); 22 | sleep(5); 23 | } 24 | 25 | /** 26 | * @When I add checkbox with text :arg1 27 | */ 28 | public function iAddCheckboxWithText($arg1) 29 | { 30 | $inputBox = self::$driver->findElement(WebDriverBy::id("sampletodotext")); 31 | $inputBox->sendKeys($arg1); 32 | $addbox = self::$driver->findElement(WebDriverBy::id("addbutton")); 33 | $addbox->click(); 34 | } 35 | 36 | 37 | /** @Then /^I get checkbox text as "([^"]*)"$/ */ 38 | public function iShouldGet($string) { 39 | $title = self::$driver->findElement(WebDriverBy::xpath("/html/body/div/div/div/ul/li[6]/span"))->getText(); 40 | if ((string) $string !== $title) { 41 | throw new Exception("Expected title: '". $string. "'' Actual is: '". $title. "'"); 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /features/single.feature: -------------------------------------------------------------------------------- 1 | Feature: LambdaTest TodoApp Functionality 2 | 3 | Scenario: Check and Verify 4 | Given I am on "https://lambdatest.github.io/sample-todo-app/" 5 | When I click on checkboxes 6 | And I add checkbox with text "New check box" 7 | Then I get checkbox text as "New check box" -------------------------------------------------------------------------------- /lib/LambdaContext.php: -------------------------------------------------------------------------------- 1 | $capsValue) { 42 | if(!array_key_exists($capsName, $caps)) 43 | $caps[$capsName] = $capsValue; 44 | } 45 | 46 | self::$driver = RemoteWebDriver::create($url, $caps, 120000, 120000); 47 | } 48 | 49 | /** @AfterFeature */ 50 | public static function tearDown() 51 | { 52 | if(self::$driver) 53 | self::$driver->quit(); 54 | } 55 | } 56 | ?> 57 | --------------------------------------------------------------------------------