PHP Software Engineering Applying a Unit Test to a Web Page - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Software Engineering Applying a Unit Test to a Web Page - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, July 6, 2019

PHP Software Engineering Applying a Unit Test to a Web Page

PHP Software Engineering


Applying a Unit Test to a Web Page


Problem

Your application is not broken down into small testable chunks, or you just want to apply unit testing to the website that your visitors see.

Solution

Use PHPUnit’s Selenium Server integration to write tests that make HTTP requests and assert conditions on the responses. These tests make assertions about the structure of www.example.com:

       class ExampleDotComTest extends PHPUnit_Extensions_SeleniumTestCase
       {
              function setUp() {
                     $this->setBrowser('firefox');
                     $this->setBrowserUrl('http://www.example.com');
              }

              // basic homepage loading
              function testHomepageLoading()
              {
                     $this->open('http://www.example.com/');
                     $this->assertTitle('Example Domain');
              }

              // test clicking on a link and getting the right page
              function testClick()
              {
                     $this->open('http://www.example.com/');
                     $this->clickAndWait('link=More information...');
                     $this->assertTitle('IANA — IANA-managed Reserved Domains');
              }
       }

It prints:

       PHPUnit 3.7.24 by Sebastian Bergmann.
       
       ..

       Time: 9.05 seconds, Memory: 3.50Mb

       OK (2 tests, 2 assertions)

Discussion

If you’re dealing with a site that’s driven in whole or in part by procedural PHP code, it is sometimes difficult to write a smaller unit test that tests encapsulated functionality. Instead, you just want to make sure that the website is working; if it isn’t, you’ll debug from there. Additionally, running tests against the real web-server output of your code lets you verify UI elements, proper links, and other user-facing features.

The PHPUnit Selenium extension integrates with Selenium Server, a free, cross-platform tool for doing in-browser testing. Once you download and run Selenium Server (just a single Java .jar file), PHPUnit test cases that extend from the PHPUnit_Extensions_SeleniumTestCase base class can communicate with it via some new methods. In the preceding example, open() retrieves a particular URL, clickAndWait()“clicks” a particular link in the returned page to visit a new page, and assertTitle() makes an assertion about the contents of the <title/> element of a web page.

The Selenium command set is comprehensive, and lets you assert conditions about the contents of web pages, arrangement of elements, links, and much more.

No comments:

Post a Comment

Post Top Ad