How to Easily Automate Website Testing - Selenium and Webdriver

2019-07-18 | 283 words

After every major change in a project, or even with every release, it is advisable to test the basic functionality of the application. End-to-End testing directly in the browser is one of the options and, in my opinion, it’s fun.

End-to-end testing is a methodology used to test whether the flow of an application is performing as designed from start to finish.

https://www.techopedia.com/definition/7035/end-to-end-test

What will we need?

We will download Facebook Webdriver via composer, and we will download Selenium and ChromeDriver in the usual way. Our folder will look like this:

Writing the Test

We will create a file called test.php, which may look something like this:

<?php

use Facebook\\WebDriver\\Remote\\RemoteWebDriver;
use Facebook\\WebDriver\\Remote\\DesiredCapabilities;
use Facebook\\WebDriver\\Chrome\\ChromeOptions;
use Facebook\\WebDriver\\WebDriverDimension;
use Facebook\\WebDriver\\WebDriverBy;
use Facebook\\WebDriver\\WebDriverExpectedCondition;

require\_once 'vendor/autoload.php';

// config
$host = 'http://localhost:4444/wd/hub';
$options = new ChromeOptions();
$options->setExperimentalOption('prefs', \['download.default\_directory' => 'c:/temp'\]);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

// create driver and resize window
$driver = RemoteWebDriver::create($host, $capabilities, 5000); 
$driver->manage()->window()->setSize(new WebDriverDimension(1360, 768));

// start testing this URL
$driver->navigate()->to("https://www.skoula.cz/");

// click on some element
$driver->findElement(WebDriverBy::cssSelector('.menu-item-1233 a'))->click();

// check URL if we are there already
$driver->wait(5, 500)->until(
    WebDriverExpectedCondition::urlContains('projekty')
);

// is there some element?
if ($driver->findElements(WebDriverBy::className('entry-title'))) {
    echo "ok";
} else {
    echo "ko";
}

How Does Testing Work?

  1. First, we start the Selenium Server - just run the downloaded .jar file.
  2. Then we run our test.php - either in the browser or via the console.

You can see what it looks like in practice in this video. It’s not sped up :)

https://www.youtube.com/watch?v=djGh4HBszhM