Articles

Getting Started with Selenium in PHP

2024-08-08·3 min read
Photo by Ben Griffiths on Unsplash

Imagine this: you've just finished building a shiny new PHP application that you’re incredibly proud of. You’ve tested it manually, and everything seems to work just fine. But the problem arises when you need to ensure that the application continues to function correctly over time as you add new features. Manually running through your test suite every time can be tedious and error-prone. Enter Selenium, a robust framework for automated testing of web applications, that helps you breathe easy knowing your webapp is behaving as it should be. In this article, you'll learn how to get started with Selenium in a PHP context, ensuring that your application remains in tip-top shape.

What is Selenium?

Selenium is an open-source tool that automates browsers, supporting a wide array of browsers and operating systems. It’s an essential tool in your development toolkit, designed to automate web applications for testing purposes, but is also capable of much more. For anyone working with web applications, Selenium is a game-changer.

Pre-requisites

Before we dive into the technical nitty-gritty, let's quickly cover the prerequisites.

  1. PHP: Ensure you have PHP installed on your system. You can check this by running php -v in your terminal or command prompt. If not installed, you can download it from PHP's official site.

  2. Composer: Composer is a dependency manager for PHP. You can download and install it from Composer's official site.

  3. Webdriver: Selenium requires a browser driver to interface with your chosen browser. ChromeDriver for Chrome, GeckoDriver for Firefox, and so on. Make sure to download the WebDriver that corresponds to your preferred testing browser.

Photo by Reinis Birznieks on Unsplash

Setting Up Selenium with PHP

Alright, let's crack on with setting up Selenium with PHP.

Step 1: Installing Selenium and PHP Webdriver

First up, you need to install the Selenium server. While this article primarily focuses on the PHP aspect, you will be running the server in the background, so make sure to download the Selenium Server and place it somewhere convenient.

Next, you’ll need to install the PHP WebDriver client bindings. To do this, we use Composer.

Run the following command in your project's root directory:

composer require php-webdriver/webdriver

This command will pull in the necessary libraries to start automating browsers using Selenium.

Step 2: Starting the Selenium Server

Before running your tests, you need to have the Selenium Server up and running. Navigate to the directory where you placed the Selenium Server jar file and run the following command:

java -jar selenium-server-standalone-<version>.jar

Make sure to replace <version> with the actual version you downloaded.

Step 3: Writing your First Test

Now, let’s write a simple PHPUnit test case to ensure everything is working smoothly.

First, ensure PHPUnit is installed via Composer if not already installed:

composer require phpunit/phpunit

Now, navigate to your test directory and create a new PHP file, let’s name it SeleniumTest.php.

Here’s a simple test script to get you started:

<?php

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;

class SeleniumTest extends PHPUnit\Framework\TestCase
{
    protected $webDriver;

    public function setUp(): void
    {
        $serverUrl = 'http://localhost:4444/wd/hub';
        $desiredCapabilities = DesiredCapabilities::chrome();
        $this->webDriver = RemoteWebDriver::create($serverUrl, $desiredCapabilities);
    }

    public function tearDown(): void
    {
        $this->webDriver->quit();
    }

    public function testTitle()
    {
        // Navigate to the site you want to test
        $this->webDriver->get('https://www.example.com');

        // Get the title of the page
        $title = $this->webDriver->getTitle();

        // Assert the title is correct
        $this->assertEquals('Example Domain', $title);
    }
}

Step 4: Running Your Test

With everything set up, it’s time to run the test. Use the following command in your terminal:

vendor/bin/phpunit SeleniumTest.php

If everything is set up correctly, you should see something like this:

PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
.
Time: 2.52 seconds, Memory: 8.00 MB
OK (1 test, 1 assertion)

Congratulations! You have successfully written and executed your first Selenium test in PHP.

More Advanced Use Cases

The example provided is quite basic, but it paves the way for more advanced test scenarios such as:

The possibilities are virtually endless. Selenium is incredibly powerful, providing you with a way to automate nearly any interaction you can perform manually within a browser.

Conclusion

Selenium is a potent tool in the realm of web application testing, and integrating it with PHP is smoother than it might initially appear. With the combination of PHPUnit and the PHP WebDriver bindings, you can streamline your testing process immensely, ensuring that your application remains robust against the pitfalls of future changes.

By following this setup, you’ve taken the first step towards a more automated, reliable testing process for your web applications. Now, no matter how complex your application becomes, you can confidently move forward knowing that your trusty Selenium tests have got your back. Happy coding!

Report bugs like it's 2024
Bug reports has looked the same since forever. You try to jam as much detail as possible to avoid the dreaded "can't reproduce". It's time to fix that. Whitespace captures every possible detail automatically and puts it all in a neat little package you can share as link.

Read more

Getting Started with Playwright in C#

In the fast-evolving world of web development, you need reliable tools for your end-to-end testing to ensure your applications run smoothly across different browsers and environments. Read more

Published 3 min read
Getting Started with Playwright in Java

Modern web development can sometimes feel like a whirlwind of continuous updates and new tools. Read more

Published 5 min read
Getting Started with Playwright in JavaScript

Development and testing can often feel like taming a herd of wild animals. Read more

Published 4 min read
Getting Started with Playwright in Node.js

In the world of web development, automated browser testing can be a complex yet essential task. Read more

Published 3 min read
Getting started with Selenium in C#

In today's fast-paced development environment, ensuring that web applications are functioning correctly is crucial. Read more

Published 4 min read
Getting Started with Selenium in Python

Ever found yourself in a situation where you need to automate tedious web tasks but don't know where to start? Perhaps you're frequently logging into a website, scraping data for a project, or testing web applications manually. Read more

Published 3 min read
Getting Started with Selenium in JavaScript

The rapid pace of web development brings along a host of challenges, one of which is ensuring that web applications behave as expected across different browsers and devices. Read more

Published 3 min read
Getting Started with Selenium in Java

Developers are often on the hunt for efficient ways to automate web browser interactions for testing purposes. Read more

Published 4 min read
Selenium vs Puppeteer vs Playwright

Web automation has become an essential tool in the arsenal of modern developers. Read more

Published 3 min read
Getting Started with Selenium in Node.js

Web development projects often require automated testing to ensure consistent and reliable performance across various browsers. Read more

Published 3 min read
One-click bug reports straight from your browser
Built and hosted in EU 🇪🇺