How to run Codeception Selenium on Safari with GitHub Actions

remarkablemark
2 min read6 days ago

--

This story goes over how to run Codeception Selenium tests on Safari browser in GitHub Actions.

Codeception
Codeception

Prerequisites

Bootstrap

Install Codeception via Composer:

composer require --dev codeception/codeception

Bootstrap Codeception:

vendor/bin/codecept bootstrap

Create an acceptance test:

vendor/bin/codecept generate:cest Acceptance First

Write 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 --steps

Selenium

Install WebDriver:

composer require --dev codeception/module-webdriver

Install and launch Selenium using selenium-standalone:

npx selenium-standalone install && npx selenium-standalone star

Update configuration in tests/Acceptance.suite.yml:

actor: AcceptanceTester
modules:
enabled:
- - PhpBrowser:
+ - WebDriver:
url: https://example.com/
+ browser: safari

When you run your tests:

vendor/bin/codecept run --steps

You 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"
PASSED

GitHub 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 --steps

Demo

See the repository.

--

--

No responses yet