Home > Article > Backend Development > Manageable and maintainable automated testing using PHP WebDriver
In the process of software development, automated testing has gradually become a standard feature of the development team. It can help the development team find and fix bugs faster, ensuring software quality and reliability. However, writing and maintaining automated tests is often a tedious and complex task. This article will introduce how to use PHP WebDriver to implement manageable and maintainable automated testing.
PHP WebDriver is a PHP library. It is an implementation of the WebDriver protocol and can be used to control the browser. The WebDriver protocol is a standard used to control browsers and can interact between different languages and platforms.
PHP WebDriver can handle multiple browser types, provides a stable platform for testing, and provides an easy-to-use API. Here is an example written using PHP WebDriver:
use FacebookWebDriverRemoteRemoteWebDriver; use FacebookWebDriverWebDriverBy; $host = 'http://localhost:4444/wd/hub'; // Selenium server 的地址 $driver = RemoteWebDriver::create($host, DesiredCapabilities::chrome()); // 访问页面 $driver->get("https://www.baidu.com/"); // 输入搜索词 $input = $driver->findElement(WebDriverBy::id('kw')); $input->sendKeys('php webDriver'); // 点击搜索按钮 $button = $driver->findElement(WebDriverBy::id('su')); $button->click(); // 等待页面加载完毕 $driver->wait()->until( WebDriverExpectedCondition::titleContains('php webDriver') ); // 获取搜索结果 $results = $driver->findElements(WebDriverBy::cssSelector('.result h3')); // 打印搜索结果 foreach ($results as $result) { echo $result->getText() . " "; } // 关闭浏览器 $driver->quit();
In the above code, we first create a RemoteWebDriver object and specify the URL of the Selenium service. Then we visited the Baidu homepage, entered "php WebDriver" in the search box, and clicked the search button. Next we wait for the page to load, obtain the search results, and output the search results. Finally, we closed the browser.
You can see that it is easy to write test scripts using PHP WebDriver. Let's explore how to use PHP WebDriver to write a manageable and maintainable automated test.
Page Object pattern is a design pattern that abstracts the browser page into an object. By encapsulating the page's elements and behavior into this object, you make test code easier to write and maintain. We can create a BasePage class to implement some basic operations for all pages.
<?php namespace AppPage; use FacebookWebDriverRemoteRemoteWebDriver; class BasePage { protected $driver; public function __construct(RemoteWebDriver $driver) { $this->driver = $driver; } public function open($url) { $this->driver->get($url); } public function close() { $this->driver->quit(); } }
The above example demonstrates the basic structure of a BasePage class, which contains some basic operations, such as opening a website and closing the browser.
Next we create a SearchPage class, which is used to search Baidu pages. In this class, we define a search method that can search for specified keywords and return search results.
<?php namespace AppPage; use FacebookWebDriverRemoteRemoteWebDriver; use FacebookWebDriverWebDriverBy; use FacebookWebDriverWebDriverExpectedCondition; class SearchPage extends BasePage { private $url = 'https://www.baidu.com/'; public function __construct(RemoteWebDriver $driver) { parent::__construct($driver); } public function search($keyword) { $this->open($this->url); $input = $this->driver->findElement(WebDriverBy::id('kw')); $input->clear(); $input->sendKeys($keyword); $input->submit(); $this->driver->wait()->until( WebDriverExpectedCondition::titleContains($keyword) ); return $this->getResults(); } private function getResults() { $elements = $this->driver->findElements(WebDriverBy::cssSelector('.result h3')); $results = []; foreach ($elements as $element) { $results[] = $element->getText(); } return $results; } }
As mentioned above, the SearchPage class encapsulates the search operation and provides a search method that will pass in keywords and perform the search. It uses WebDriver's API to locate page elements and manipulate actions. The getResults method is used to obtain search results.
Data-driven testing is a testing technique that uses different input data to check multiple aspects of the same feature. In our example, we can use data-driven testing to check the search results for different inputs.
<?php namespace AppTest; use AppPageSearchPage; use FacebookWebDriverRemoteRemoteWebDriver; class SearchTest extends BaseTest { /** * @dataProvider keywordProvider */ public function testSearch($keyword) { $searchPage = new SearchPage($this->driver); $results = $searchPage->search($keyword); $this->assertGreaterThan(0, count($results), "Search for '$keyword' returned no results"); $this->assertContains($keyword, implode('', $results), "Search for '$keyword' did not return relevant results"); } public function keywordProvider() { return [ ['php WebDriver'], ['facebook WebDriver'], ['selenium WebDriver'], ]; } }
As you can see, we use PHPUnit's dataProvider annotation to generate the data provider for the test method. In our example, we passed in three test data: 'php WebDriver', 'facebook WebDriver' and 'selenium WebDriver'. In the testSearch method, we instantiate a SearchPage object and run the search method with each test data. We then assert the correctness of the search results.
Running tests using PHPUnit is very simple. We only need to execute the following command:
phpunit SearchTest.php
Then PHPUnit will use the test classes and methods we provided and report the results of the test.
Summary
In this article, we learned how to use PHP WebDriver to achieve manageable and maintainable automated testing. We introduced the basic concepts of Page Object pattern and data-driven testing, and demonstrated through sample code how to use PHP WebDriver to implement these methods. I hope this article can help you better understand the principles and practices of automated testing.
The above is the detailed content of Manageable and maintainable automated testing using PHP WebDriver. For more information, please follow other related articles on the PHP Chinese website!