How to wait for Selenium to start during Codeception tests

If you test using PHP and Codeception you may have run into the issue of how to start Selenium before your tests. Some people use a wait or sleep command but here is a tip to automatically start Selenium during tests and wait for it to start:

Configure acceptance.suite.yml

With the RunProcess extension with can add a special timeout wait command to wait for Selenium to start before starting our tests:

actor: AcceptanceTester
        - Codeception\Extension\RunFailed
        # the lines will run the selenium server before tests start and wait for the process to answer on port 4444
        - Codeception\Extension\RunProcess:
            - java -Dwebdriver.gecko.driver=./geckodriver -jar selenium-server.jar
            - "timeout 300 bash -c 'while [[ \"$(curl -s -o /dev/null -w ''%{http_code}'' localhost:4444)\" != '200' ]]; do sleep 5; done' || false"
        - WebDriver:
           url: ""
           browser: firefox
        - \Helper\Acceptance
step_decorators: ~        

That’s it!

More information

For more tips on testing with Codeception see the PHP guide.