How to run Codeception Selenium on Safari with GitHub Actions
This story goes over how to run Codeception Selenium tests on Safari browser in GitHub Actions.
Prerequisites
Bootstrap
Install Codeception via Composer:
composer require --dev codeception/codeceptionBootstrap Codeception:
vendor/bin/codecept bootstrapCreate an acceptance test:
vendor/bin/codecept generate:cest Acceptance FirstWrite your test tests/Acceptance/FirstCest.php:
<?php
declare(strict_types=1);
namespace Tests\Acceptance;
use Tests\Support\AcceptanceTester;
final class FirstCest
{
public function _before(AcceptanceTester $I): void
{
$I->amOnPage('/');
}
public function tryToTest(AcceptanceTester $I): void
{
$I->see('Example');
}
}Configure your acceptance tests tests/Acceptance.suite.yml:
actor: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: https://example.com/Run your tests:
vendor/bin/codecept run --stepsSelenium
Install WebDriver:
composer require --dev codeception/module-webdriverInstall and launch Selenium using selenium-standalone:
npx selenium-standalone install && npx selenium-standalone starUpdate configuration in tests/Acceptance.suite.yml:
actor: AcceptanceTester
modules:
enabled:
- - PhpBrowser:
+ - WebDriver:
url: https://example.com/
+ browser: safariWhen you run your tests:
vendor/bin/codecept run --stepsYou may get the error:
[Facebook\WebDriver\Exception\SessionNotCreatedException] Could not start a new session. Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: Could not create a session: You must enable the ‘Allow Remote Automation’ option in Safari’s Develop menu to control Safari via WebDriver.
To enable the ‘Allow Remote Automation’ option in Safari’s Develop menu to control Safari via WebDriver, follow the instructions.
If you run the test again, it should pass:
Tests.Acceptance Tests (1) ---------------------------------
FirstCest: Try to test
Signature: Tests\Acceptance\FirstCest:tryToTest
Test: tests/Acceptance/FirstCest.php:tryToTest
Scenario --
I am on page "/"
I see "Example"
PASSEDGitHub Actions
The workflow to run the acceptance tests on Safari browser in GitHub Actions:
# .github/workflows/test.yml
name: test
on: push
jobs:
test:
runs-on: macos-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
- name: Install dependencies
run: composer install
- name: Start Selenium
run: npx selenium-standalone install && npx selenium-standalone start &
- name: Run tests
run: vendor/bin/codecept run --stepsDemo
See the repository.
