InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Applying a Unit Test to a Web Page in PHP
PHP

Applying a Unit Test to a Web Page in PHP

InfinityCoder December 23, 2016

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.

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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:

1
2
3
4
5
6
7
PHPUnit 3.7.24 by Sebastian Bergmann.
 
..
 
Time: 9.05 seconds, Memory: 3.50Mb
 
OK (2 tests, 2 assertions)

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, crossplatform 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_Ex tensions_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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Checking If an Object Is an Instance of a Specific Class in PHP
You want to check if an object is an instance …

Checking If an Object Is an Instance of a Specific Class in PHP

Running PHP Code on Every Line of an Input File in  PHP
You want to read an entire file and execute PHP …

Running PHP Code on Every Line of an Input File in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap