diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..24584df --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,24 @@ +--- +name: Tests + +on: push + +jobs: + tests: + env: + TB_KEY: ${{ secrets.TB_KEY }} + TB_SECRET: ${{ secrets.TB_SECRET }} + name: Unit Testing + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: mbstring, intl, zip + coverage: none + - name: Install PHP dependencies + run: composer update --no-interaction + - name: Run tests + run: composer single \ No newline at end of file diff --git a/.gitignore b/.gitignore index f137394..b29e087 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ composer.lock composer.phar vendor/ +bin/ diff --git a/README.md b/README.md index 6df4144..e9ce15d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # Behat-Selenium-Sample TestingBot provides an online grid of browsers and mobile devices to run Automated tests on via Selenium WebDriver. This example demonstrates how to use Behat to run tests across several browsers. +*This example uses Behat 3. We also have a [Behat 2 Example](https://github.com/testingbot/behat-selenium-sample)* + ## Setup 1. Clone this repo: diff --git a/bin/behat b/bin/behat deleted file mode 100755 index 9b6a14d..0000000 --- a/bin/behat +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env sh - -dir=$(cd "${0%[/\\]*}" > /dev/null; cd "../vendor/behat/behat/bin" && pwd) - -if [ -d /proc/cygdrive ] && [[ $(which php) == $(readlink -n /proc/cygdrive)/* ]]; then - # We are in Cgywin using Windows php, so the path must be translated - dir=$(cygpath -m "$dir"); -fi - -"${dir}/behat" "$@" diff --git a/bin/behat.bat b/bin/behat.bat deleted file mode 100644 index d10201c..0000000 --- a/bin/behat.bat +++ /dev/null @@ -1,4 +0,0 @@ -@ECHO OFF -setlocal DISABLEDELAYEDEXPANSION -SET BIN_TARGET=%~dp0/../vendor/behat/behat/bin/behat -php "%BIN_TARGET%" %* diff --git a/composer.json b/composer.json index 73c3efa..4744ad7 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "require": { - "behat/behat": "2.5.*@stable", - "php-webdriver/webdriver": "dev-master", + "behat/behat": "3.11.*@stable", + "php-webdriver/webdriver": "dev-main", "symfony/yaml": "*" }, "config": { diff --git a/config/parallel.conf.yml b/config/parallel.conf.yml index c7820b6..01e69b2 100644 --- a/config/parallel.conf.yml +++ b/config/parallel.conf.yml @@ -1,33 +1,32 @@ default: - paths: - features: '../features/single' - bootstrap: '../features/bootstrap' - context: - parameters: - testingbot: - server: "hub.testingbot.com" - user: "TB_KEY" - key: "TB_SECRET" - - capabilities: - build: "behat-selenium-sample" - name: "parallel-behat-test" - - - environments: - - - browserName: chrome - version: latest - platform: Win10 - - - browserName: chrome - version: latest-1 - platform: Win10 - - - browserName: chrome - version: latest-2 - platform: Win10 - - - browserName: chrome - version: latest-3 - platform: Win10 + autoload: + '': '%paths.base%/../features/bootstrap' + suites: + default: + paths: [ '%paths.base%/../features/single' ] + contexts: + - FeatureContext: + parameters: + server: "hub.testingbot.com" + user: "TB_KEY" + key: "TB_SECRET" + capabilities: + build: "behat-selenium-sample" + name: "single-behat-test" + browsers: + - + browserName: chrome + version: latest + platform: Win10 + - + browserName: chrome + version: latest-1 + platform: Win10 + - + browserName: chrome + version: latest-2 + platform: Win10 + - + browserName: chrome + version: latest-3 + platform: Win10 diff --git a/config/single.conf.yml b/config/single.conf.yml index 71cc72b..60e83e0 100644 --- a/config/single.conf.yml +++ b/config/single.conf.yml @@ -1,21 +1,20 @@ default: - paths: - features: '../features/single' - bootstrap: '../features/bootstrap' - context: - parameters: - testingbot: - server: "hub.testingbot.com" - user: "TB_KEY" - key: "TB_SECRET" - - capabilities: - build: "behat-selenium-sample" - name: "single-behat-test" - - - environments: - - - browserName: chrome - version: latest - platform: Win10 + autoload: + '': '%paths.base%/../features/bootstrap' + suites: + default: + paths: [ '%paths.base%/../features/single' ] + contexts: + - FeatureContext: + parameters: + server: "hub.testingbot.com" + user: "TB_KEY" + key: "TB_SECRET" + capabilities: + build: "behat-selenium-sample" + name: "single-behat-test" + browsers: + - + browserName: chrome + version: latest + platform: Win10 \ No newline at end of file diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index 4e61bac..28b8e51 100644 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -2,10 +2,6 @@ require "vendor/autoload.php"; -use Behat\Behat\Context\BehatContext, - Behat\Behat\Exception\PendingException; -use Behat\Gherkin\Node\PyStringNode, - Behat\Gherkin\Node\TableNode; use Facebook\WebDriver\WebDriverBy; class FeatureContext extends TestingBotContext { @@ -14,27 +10,18 @@ public function iAmOnSite($url) { self::$driver->get($url); } - /** @When /^I search for "([^"]*)"$/ */ - public function iSearchFor($searchText) { - $element = self::$driver->findElement(WebDriverBy::name("q")); - $element->sendKeys($searchText); - $element->submit(); + /** @When /^I click "([^"]*)"$/ */ + public function iClickOn($linkText) { + $element = self::$driver->findElement(WebDriverBy::linkText($linkText)); + $element->click(); sleep(5); } - /** @Then /^I get title as "([^"]*)"$/ */ + /** @Then /^the URL should be "([^"]*)"$/ */ public function iShouldGet($string) { - $title = self::$driver->getTitle(); - if ((string) $string !== $title) { - throw new Exception("Expected title: '". $string. "'' Actual is: '". $title. "'"); - } - } - - /** @Then /^I should see "([^"]*)"$/ */ - public function iShouldSee($string) { - $source = self::$driver->getPageSource(); - if (strpos($source, $string) === false) { - throw new Exception("Expected to see: '". $string. "'' Actual is: '". $source. "'"); + $currentUrl = self::$driver->getCurrentUrl(); + if ((string) $string !== $currentUrl) { + throw new Exception("Expected URL: '". $string. "'' Actual is: '". $currentUrl. "'"); } } } diff --git a/features/single/single.feature b/features/single/single.feature index df0b429..4950c6e 100644 --- a/features/single/single.feature +++ b/features/single/single.feature @@ -1,6 +1,6 @@ -Feature: Google's Search Functionality +Feature: TestingBot Functionality -Scenario: Can find search results - Given I am on "https://www.google.com/ncr" - When I search for "TestingBot" - Then I get title as "TestingBot - Google Search" +Scenario: Open TestingBot + Given I am on "https://testingbot.com" + When I click "Features" + Then the URL should be "https://testingbot.com/features" diff --git a/lib/TestingBotContext.php b/lib/TestingBotContext.php index 9bb28d4..6b07da2 100644 --- a/lib/TestingBotContext.php +++ b/lib/TestingBotContext.php @@ -4,31 +4,37 @@ use Facebook\WebDriver\Remote\RemoteWebDriver; -class TestingBotContext extends Behat\Behat\Context\BehatContext +class TestingBotContext implements Behat\Behat\Context\Context { - protected $CONFIG; + protected static $CONFIG; protected static $driver; public function __construct($parameters){ - $GLOBALS['CONFIG'] = $parameters["testingbot"]; + self::$CONFIG = $parameters; $GLOBALS['USERNAME'] = getenv('TB_KEY'); - if(!$GLOBALS['USERNAME']) $GLOBALS['USERNAME'] = $GLOBALS['CONFIG']['user']; + if (!$GLOBALS['USERNAME']) { + $GLOBALS['USERNAME'] = self::$CONFIG['user']; + } $GLOBALS['ACCESS_KEY'] = getenv('TB_SECRET'); - if(!$GLOBALS['ACCESS_KEY']) $GLOBALS['ACCESS_KEY'] = $GLOBALS['CONFIG']['key']; + if (!$GLOBALS['ACCESS_KEY']) { + $GLOBALS['ACCESS_KEY'] = self::$CONFIG['key']; + } + + if (!self::$driver) { + self::setup(); + } } - /** @BeforeFeature */ public static function setup() { - $CONFIG = $GLOBALS['CONFIG']; $task_id = getenv('TASK_ID') ? getenv('TASK_ID') : 0; - $url = "https://" . $GLOBALS['USERNAME'] . ":" . $GLOBALS['ACCESS_KEY'] . "@" . $CONFIG['server'] ."/wd/hub"; - $caps = $CONFIG['environments'][$task_id]; + $url = "https://" . $GLOBALS['USERNAME'] . ":" . $GLOBALS['ACCESS_KEY'] . "@" . self::$CONFIG['server'] ."/wd/hub"; + $caps = self::$CONFIG['browsers'][$task_id]; - foreach ($CONFIG["capabilities"] as $key => $value) { + foreach (self::$CONFIG["capabilities"] as $key => $value) { if (!array_key_exists($key, $caps)) $caps[$key] = $value; } @@ -39,7 +45,9 @@ public static function setup() /** @AfterFeature */ public static function tearDown() { - self::$driver->quit(); + if (self::$driver) { + self::$driver->quit(); + } } } ?>