App & Browser Testing Made Easy

Give your users a seamless experience by testing on 3000+ real devices and browsers. Don't compromise with emulators and simulators

Home Guide Selenium and PHP Tutorial: How to get started with Test Automation

Selenium and PHP Tutorial: How to get started with Test Automation

By Mohit Joshi, Community Contributor -

The world is witnessing exponential growth and rapid transformation of web applications. In the coming years, billions of devices will have internet access and use numerous web applications. Web applications now have evolved immensely, giving smarter functionalities and features and have to fulfill several aspects like security, stability, usability, etc. 

To determine the stability, automated testing is done as a part of web application testing. Automated testing is a method that helps developers to run numerous tests of an application automatically without intervening in the process.

When it comes to automation, there are several tools to perform testing. The easiest and most popular tool is Selenium for PHP. The ease of Selenium combined with the Simplicity of PHP manifests a great combo for testers across the world.

Getting Started with Selenium Testing using PHP

Selenium is among the most popular testing tools which support several scripts such as PHP, Java, Python, Perl, C#, and Ruby. It is supported across numerous browsers like Safari, Chrome, Firefox, etc., and several operating systems like Windows, Linux, macOS, Solaris, etc. It supports multiple frameworks, which keeps a wide range of developers and testers under its shade. Moreover, Selenium contains simple commands and has a straightforward implementation. 

Prerequisites of the Setup Configuration

  1. Xampp (an abbreviation for cross-platform, Apache, MySQL, PHP, and Perl). It is an open-source tool that helps in building a local server on your computer. Xampp is an ideal framework to test the functionality of applications based on several technologies, such as PHP, Perl, Apache, and MySQL. 
  2. Any IDE to write the code. In this Selenium PHP tutorial, let us use the most popular IDE, VS code.
  3. Java should be installed in your system. Also, set up all the necessary environment variables. 

Step 1: Installing Xampp

You can download Xampp from its official website. Note that there are multiple options; download Xampp in accord with whichever operating system you are on.

After Installing Xampp, launch the application, and you will see a window as shown above. Start the Apache and MySQL servers from here by clicking on Start corresponding to each of them.

download xmapp

Step 2: Download Selenium PHP Binding and Extract it to the htdocs folder

After starting the Xampp server, you have to download the Selenium PHP Bindings. Selenium PHP bindings serve as a link between the browser and the test scripts written with the help of Selenium.  

Step 3: Install any IDE

You have set up the environment; now you may write code to perform automation testing. Download VS Code, which is the most popular IDE developed by Microsoft.

Step 4: Download and Install a browser driver

The idea behind installing a browser driver is to automate the commands that you code. It serves as a link between the test script and the browser. For different browsers, you have to install a different browser driver. By default, Selenium supports Mozilla Firefox.

To install a web driver in Selenium, download a driver in accordance with the browser you want to use.

Step 5: Download the Selenium Server

Now download the most important ingredient in the entire automation testing process, Selenium Standalone Server. Note that the Selenium server package includes a jar file, for which you must already install Java on your system.

Step 6: Create a Test

Now, let’s create our first test using PHP. Open a PHP file in VS code and enter the following script. We’re creating a test that will perform a simple google search. 


<?php
require_once “phpwebdriver/WebDriver.php”;
$webdriver = new WebDriver(“localhost”, “4444”);
$webdriver->connect(“firefox”);                           
$webdriver->get(“http://google.com”);
$element = $webdriver->findElementBy(LocatorStrategy::name, “q”);
if ($element) {
    $element->sendKeys(array(“BrowserStack” ) );
    $element->submit();
}
$webdriver->close();
?>

Step 7: Execute the code by starting the Selenium Server

To start the Selenium Server, navigate to the folder where you have downloaded the Selenium Server package and open the command prompt in that folder and type the following command.


java -jar Selenium-server-standalone-<version>.jar -role hub

Replace <version> with the actual version of the Selenium Server package you have downloaded.

Now, open your localhost to see what your test is performing. To open your PHP file, make sure to save the file in the htdocs folder and then open localhost/file.php in the browser. It will be executing a google search.

Note: In case, you are looking to integrate BrowserStack with Selenium & PHP, visit our official documentation.

Best Practices using Selenium WebDriver and PHP

Even though Automation tests determine the reliability and stability of the software, it becomes important that the tests’ scripts have stability in themselves. However, there is no fixed method to create such reliable tests; it depends upon the situation; here are a few practices that you must adopt while using Selenium in PHP.

  • Selector Order: Choosing the right selector order speeds up your tests, as it determines which web locator should have the maximum effect on your test. The ideal order is ID > Name > CSSSelector > XPath.
  • Page Object Model: Here every web page is treated as an object class which eases the maintenance of code. So, when you make changes to the web page, the changes you make in the test code will be even less.
  • Directory Structure: There is no mandatory method while structuring your files. However, it is recommended to separate test implementation from its framework while structuring the folders.
  • Avoid code duplication: If you avoid code duplication, it will ultimately increase the code’s maintainability and decrease the size of the code, which will further increase the test’s performance.
  • Setup reports: The idea behind implementing setup reports is to analyze your test, at which time it fails, test status, and other parameters.

PHP is an efficient server-side programming language that helps in creating dynamic and interactive web pages. Alongside this, it is widely used in automation testing. It also supports the Selenium framework, which altogether is a great combination for performing automation testing. 

Also, using the Selenium framework offers so many advantages, one of them being an open-source tool. Selenium is backed by a strong community, which makes it more efficient with each regular update. 

 In this Selenium PHP example, you would have understood how to execute PHP Selenium testing on different browsers. However, if you want to broaden your Automation testing, you should use Selenium Grid. Selenium Grid helps in performing automation testing in multiple operating systems, browsers, and devices simultaneously. 

BrowserStack allows you to execute Selenium tests seamlessly using different languages like Java Script, Java, PHP, Python, C#, Ruby, etc.

Try Selenium Testing for Free

Tags
Automation Testing Selenium

Featured Articles

Cross Browser Testing in Selenium : Tutorial

How to run your first Selenium test script

Curated for all your Testing Needs

Actionable Insights, Tips, & Tutorials delivered in your Inbox
By subscribing , you agree to our Privacy Policy.
thank you illustration

Thank you for Subscribing!

Expect a curated list of guides shortly.