SimpleTest


SimpleTest\WebTestCase
web_tester.php at line 477

Class WebTestCase

SimpleTestCase
└─WebTestCase

public class WebTestCase
extends SimpleTestCase

Test case for testing of web pages. Allows fetching of pages, parsing of HTML and submitting forms.

Subpackage:
WebTester
Package SimpleTest.
Subpackage UnitTester.

Constructor Summary

WebTestCase(string label)

Creates an empty test case.

Method Summary
void

addHeader(string header)

Adds a header to every fetch.

void

ageCookies(integer interval)

Moves cookie expiry times back into the past.

boolean

assertAuthentication(string authentication, string message)

Attempt to match the authentication type within the security realm we are currently matching.

boolean

assertCookie(string name, string expected, string message)

Checks that a cookie is set for the current page and optionally checks the value.

boolean

assertField(string name, mixed expected, string message)

Confirms that the form element is currently set to the expected value.

boolean

assertFieldById(string/integer id, mixed expected, string message)

Confirms that the form element is currently set to the expected value.

boolean

assertHeader(string header, string value, mixed message)

Checks each header line for the required value.

boolean

assertHeaderPattern(string header, string pattern, mixed message)

Checks each header line for the required pattern.

boolean

assertLink(string label, string message)

Tests for the presence of a link label.

boolean

assertLinkById(string id, string message)

Tests for the presence of a link id attribute.

boolean

assertMime(array types, string message)

Checks the mime type against a list of possible values.

boolean

assertNoAuthentication(string message)

Checks that no authentication is necessary to view the desired page.

boolean

assertNoCookie(string name, string message)

Checks that no cookie is present or that it has been successfully cleared.

boolean

assertNoLink(string/integer label, string message)

Tests for the non-presence of a link label.

boolean

assertNoLinkById(string id, string message)

Tests for the non-presence of a link label.

boolean

assertNoUnwantedHeader(string header, mixed message)

Confirms that the header type has not been received.

boolean

assertNoUnwantedPattern(string pattern, string message)

Will trigger a pass if the perl regex pattern is not present in raw content.

boolean

assertNoUnwantedText(string text, string message)

Will trigger a pass if the text is not found in the plain text form of the page.

boolean

assertRealm(string realm, string message)

Attempts to match the current security realm.

boolean

assertResponse(array responses, string message)

Checks the response code against a list of possible values.

boolean

assertTitle(string title, string message)

Tests the text between the title tags.

boolean

assertWantedPattern(string pattern, string message)

Will trigger a pass if the Perl regex pattern is found in the raw content.

boolean

assertWantedText(string text, string message)

Will trigger a pass if the text is found in the plain text form of the page.

boolean

authenticate(string username, string password)

Retries a request after setting the authentication for the current realm.

boolean

back()

Equivalent to hitting the back button on the browser.

void

clearFrameFocus()

Clears the frame focus.

boolean/string

clickImage(string label, integer x, integer y, hash additional)

Clicks the submit image by some kind of label.

boolean/string

clickImageById(integer/string id, integer x, integer y, hash additional)

Clicks the submit image by ID attribute.

boolean/string

clickImageByName(string name, integer x, integer y, hash additional)

Clicks the submit image by the name.

boolean/string

clickLink(string label, integer index)

Follows a link by name.

boolean/string

clickLinkById(string id)

Follows a link by id attribute.

boolean/string

clickSubmit(string label, hash additional)

Clicks the submit button by label.

boolean/string

clickSubmitById(string id, hash additional)

Clicks the submit button by ID attribute.

boolean/string

clickSubmitByName(string name, hash additional)

Clicks the submit button by name attribute.

TestBrowser

createBrowser()

Creates a new default web browser object.

SimpleInvoker

createInvoker()

Sets the invoker to one that restarts the browser on each request.

boolean

forward()

Equivalent to hitting the forward button on the browser.

boolean

get(string url, hash parameters)

Fetches a page into the page buffer.

SimpleBrowser

getBrowser()

Gets a current browser reference for setting special expectations or for detailed examination of page fetches.

string

getCookie(string name)

Gets the cookie value for the current browser context.

integer/string/boolean

getFrameFocus()

Accessor for current frame focus.

string

getTransportError()

Gets the last response error.

string

getUrl()

Accessor for the currently selected URL.

boolean

head(string url, hash parameters)

Does a HTTP HEAD fetch, fetching only the page headers.

void

ignoreFrames()

Disables frames support.

boolean

post(string url, hash parameters)

Fetches a page by POST into the page buffer.

void

restart(string/integer date)

Simulates the closing and reopening of the browser.

boolean

retry()

Equivalent to hitting the retry button on the browser.

void

setBrowser(SimpleBrowser browser)

Gets a current browser reference for setting special expectations or for detailed examination of page fetches.

void

setConnectionTimeout(integer timeout)

Sets the socket timeout for opening a connection and receiving at least one byte of information.

void

setCookie(string name, string value, string host, string path, string expiry)

Sets a cookie in the current browser.

boolean

setField(string name, string value)

Sets all form fields with that name.

boolean

setFieldById(string/integer id, string value)

Sets all form fields with that name.

boolean

setFrameFocus(string name)

Sets the focus by name.

boolean

setFrameFocusByIndex(integer choice)

Sets the focus by index.

void

setMaximumRedirects(integer max)

Sets the maximum number of redirects before the web page is loaded regardless.

void

showHeaders()

Dumps the current HTTP headers for debugging.

void

showRequest()

Dumps the current request for debugging.

void

showSource()

Dumps the current HTML source for debugging.

boolean/string

submitFormById(string id)

Submits a form by the ID.

void

unsetBrowser()

Clears the current browser reference to help the PHP garbage collector.

void

useProxy(string proxy, string username, string password)

Sets proxy to use on all requests for when testing from behind a firewall.

Methods inherited from SimpleTest\SimpleTestCase
assertExpectation, assertFalse, assertTrue, createInvoker, dump, error, fail, getAssertionLine, getLabel, getSize, pass, run, sendMessage, setUp, signal, swallowErrors, tearDown

Constructor Detail

web_tester.php at line 487

WebTestCase

public WebTestCase(string label)

Creates an empty test case. Should be subclassed with test methods for a functional test case.

Parameters:
label - Name of test case. Will use the class name if none specified.

Method Detail

web_tester.php at line 628

addHeader

public void addHeader(string header)

Adds a header to every fetch.

Parameters:
header - Header line to add to every request until cleared.

web_tester.php at line 609

ageCookies

public void ageCookies(integer interval)

Moves cookie expiry times back into the past. Useful for testing timeouts and expiries.

Parameters:
interval - Amount to age in seconds.

web_tester.php at line 1126

assertAuthentication

public boolean assertAuthentication(string authentication, string message)

Attempt to match the authentication type within the security realm we are currently matching.

Parameters:
authentication - Usually basic.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1303

assertCookie

public boolean assertCookie(string name, string expected, string message)

Checks that a cookie is set for the current page and optionally checks the value.

Parameters:
name - Name of cookie to test.
expected - Expected value as a string or false if any value will do.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1044

assertField

public boolean assertField(string name, mixed expected, string message)

Confirms that the form element is currently set to the expected value. A missing form will always fail. If no value is given then only the existence of the field is checked.

Parameters:
name - Name of field in forms.
expected - Expected string/array value or false for unset fields.
message - Message to display. Default can be embedded with %s.
Returns:
True if pass.

web_tester.php at line 1071

assertFieldById

public boolean assertFieldById(string/integer id, mixed expected, string message)

Confirms that the form element is currently set to the expected value. A missing form will always fail. If no ID is given then only the existence of the field is checked.

Parameters:
id - Name of field in forms.
expected - Expected string/array value or false for unset fields.
message - Message to display. Default can be embedded with %s.
Returns:
True if pass.

web_tester.php at line 1179

assertHeader

public boolean assertHeader(string header, string value, mixed message)

Checks each header line for the required value. If no value is given then only an existence check is made.

Parameters:
header - Case insensitive header name.
value - Case sensitive trimmed string to match against.
Returns:
True if pass.

web_tester.php at line 1193

assertHeaderPattern

public boolean assertHeaderPattern(string header, string pattern, mixed message)

Checks each header line for the required pattern.

Parameters:
header - Case insensitive header name.
pattern - Pattern to match value against.
Returns:
True if pass.

web_tester.php at line 958

assertLink

public boolean assertLink(string label, string message)

Tests for the presence of a link label. Match is case insensitive with normalised space.

Parameters:
label - Text between the anchor tags.
message - Message to display. Default can be embedded with %s.
Returns:
True if link present.

web_tester.php at line 988

assertLinkById

public boolean assertLinkById(string id, string message)

Tests for the presence of a link id attribute.

Parameters:
id - Id attribute value.
message - Message to display. Default can be embedded with %s.
Returns:
True if link present.

web_tester.php at line 1110

assertMime

public boolean assertMime(array types, string message)

Checks the mime type against a list of possible values.

Parameters:
types - Possible mime types for a pass.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1149

assertNoAuthentication

public boolean assertNoAuthentication(string message)

Checks that no authentication is necessary to view the desired page.

Parameters:
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1324

assertNoCookie

public boolean assertNoCookie(string name, string message)

Checks that no cookie is present or that it has been successfully cleared.

Parameters:
name - Name of cookie to test.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 974

assertNoLink

public boolean assertNoLink(string/integer label, string message)

Tests for the non-presence of a link label. Match is case insensitive with normalised space.

Parameters:
label - Text between the anchor tags or ID attribute.
message - Message to display. Default can be embedded with %s.
Returns:
True if link missing.

web_tester.php at line 1003

assertNoLinkById

public boolean assertNoLinkById(string id, string message)

Tests for the non-presence of a link label. Match is case insensitive with normalised space.

Parameters:
id - Id attribute value.
message - Message to display. Default can be embedded with %s.
Returns:
True if link missing.

web_tester.php at line 1209

assertNoUnwantedHeader

public boolean assertNoUnwantedHeader(string header, mixed message)

Confirms that the header type has not been received. Only the landing page is checked. If you want to check redirect pages, then you should limit redirects so as to capture the page you want.

Parameters:
header - Case insensitive header name.
Returns:
True if pass.

web_tester.php at line 1286

assertNoUnwantedPattern

public boolean assertNoUnwantedPattern(string pattern, string message)

Will trigger a pass if the perl regex pattern is not present in raw content.

Parameters:
pattern - Perl regex to look for including the regex delimiters.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1254

assertNoUnwantedText

public boolean assertNoUnwantedText(string text, string message)

Will trigger a pass if the text is not found in the plain text form of the page.

Parameters:
text - Text to look for.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1162

assertRealm

public boolean assertRealm(string realm, string message)

Attempts to match the current security realm.

Parameters:
realm - Name of security realm.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1094

assertResponse

public boolean assertResponse(array responses, string message)

Checks the response code against a list of possible values.

Parameters:
responses - Possible responses for a pass.
message - Message to display. Default can be embedded with %s.
Returns:
True if pass.

web_tester.php at line 1224

assertTitle

public boolean assertTitle(string title, string message)

Tests the text between the title tags.

Parameters:
title - Expected title or empty if expecting no title.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1270

assertWantedPattern

public boolean assertWantedPattern(string pattern, string message)

Will trigger a pass if the Perl regex pattern is found in the raw content.

Parameters:
pattern - Perl regex to look for including the regex delimiters.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 1239

assertWantedText

public boolean assertWantedText(string text, string message)

Will trigger a pass if the text is found in the plain text form of the page.

Parameters:
text - Text to look for.
message - Message to display.
Returns:
True if pass.

web_tester.php at line 759

authenticate

public boolean authenticate(string username, string password)

Retries a request after setting the authentication for the current realm.

Parameters:
username - Username for realm.
password - Password for realm.
Returns:
True if successful fetch. Note that authentication may still have failed.

web_tester.php at line 734

back

public boolean back()

Equivalent to hitting the back button on the browser.

Returns:
True if history entry and fetch succeeded.

web_tester.php at line 823

clearFrameFocus

public void clearFrameFocus()

Clears the frame focus. All frames will be searched for content.


web_tester.php at line 877

clickImage

public boolean/string clickImage(string label, integer x, integer y, hash additional)

Clicks the submit image by some kind of label. Usually the alt tag or the nearest equivalent. The owning form will be submitted by this. Clicking outside of the boundary of the coordinates will result in a failure.

Parameters:
label - Alt attribute of button.
x - X-coordinate of imaginary click.
y - Y-coordinate of imaginary click.
additional - Additional form values.
Returns:
Page on success.

web_tester.php at line 910

clickImageById

public boolean/string clickImageById(integer/string id, integer x, integer y, hash additional)

Clicks the submit image by ID attribute. The owning form will be submitted by this. Clicking outside of the boundary of the coordinates will result in a failure.

Parameters:
id - ID attribute of button.
x - X-coordinate of imaginary click.
y - Y-coordinate of imaginary click.
additional - Additional form values.
Returns:
Page on success.

web_tester.php at line 894

clickImageByName

public boolean/string clickImageByName(string name, integer x, integer y, hash additional)

Clicks the submit image by the name. Usually the alt tag or the nearest equivalent. The owning form will be submitted by this. Clicking outside of the boundary of the coordinates will result in a failure.

Parameters:
name - Name attribute of button.
x - X-coordinate of imaginary click.
y - Y-coordinate of imaginary click.
additional - Additional form values.
Returns:
Page on success.

web_tester.php at line 935

clickLink

public boolean/string clickLink(string label, integer index)

Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space.

Parameters:
label - Text between the anchor tags.
index - Link position counting from zero.
Returns:
Page on success.

web_tester.php at line 945

clickLinkById

public boolean/string clickLinkById(string id)

Follows a link by id attribute.

Parameters:
id - ID attribute value.
Returns:
Page on success.

web_tester.php at line 836

clickSubmit

public boolean/string clickSubmit(string label, hash additional)

Clicks the submit button by label. The owning form will be submitted by this.

Parameters:
label - Button label. An unlabeled button can be triggered by 'Submit'.
additional - Additional form values.
Returns:
Page on success.

web_tester.php at line 860

clickSubmitById

public boolean/string clickSubmitById(string id, hash additional)

Clicks the submit button by ID attribute. The owning form will be submitted by this.

Parameters:
id - ID attribute of button.
additional - Additional form values.
Returns:
Page on success.

web_tester.php at line 848

clickSubmitByName

public boolean/string clickSubmitByName(string name, hash additional)

Clicks the submit button by name attribute. The owning form will be submitted by this.

Parameters:
name - Name attribute of button.
additional - Additional form values.
Returns:
Page on success.

web_tester.php at line 538

createBrowser

public TestBrowser createBrowser()

Creates a new default web browser object. Will be cleared at the end of the test method.

Returns:
New browser.

web_tester.php at line 497

createInvoker

public SimpleInvoker createInvoker()

Sets the invoker to one that restarts the browser on each request.

Returns:
Invoker for each method.

web_tester.php at line 745

forward

public boolean forward()

Equivalent to hitting the forward button on the browser.

Returns:
True if history entry and fetch succeeded.

web_tester.php at line 679

get

public boolean get(string url, hash parameters)

Fetches a page into the page buffer. If there is no base for the URL then the current base URL is used. After the fetch the base URL reflects the new location.

Parameters:
url - URL to fetch.
parameters - Optional additional GET data.
Returns:
True on success.

web_tester.php at line 508

getBrowser

public SimpleBrowser getBrowser()

Gets a current browser reference for setting special expectations or for detailed examination of page fetches.

Returns:
Current test browser object.

web_tester.php at line 769

getCookie

public string getCookie(string name)

Gets the cookie value for the current browser context.

Parameters:
name - Name of cookie.
Returns:
Value of cookie or false if unset.

web_tester.php at line 794

getFrameFocus

public integer/string/boolean getFrameFocus()

Accessor for current frame focus. Will be false if no frame has focus.

Returns:
Label if any, otherwise the position in the frameset or false if none.

web_tester.php at line 547

getTransportError

public string getTransportError()

Gets the last response error.

Returns:
Last low level HTTP error.

web_tester.php at line 557

getUrl

public string getUrl()

Accessor for the currently selected URL.

Returns:
Current location or false if no page yet fetched.

web_tester.php at line 713

head

public boolean head(string url, hash parameters)

Does a HTTP HEAD fetch, fetching only the page headers. The current base URL is unchanged by this.

Parameters:
url - URL to fetch.
parameters - Optional additional GET data.
Returns:
True on success.

web_tester.php at line 618

ignoreFrames

public void ignoreFrames()

Disables frames support. Frames will not be fetched and the frameset page will be used instead.


web_tester.php at line 697

post

public boolean post(string url, hash parameters)

Fetches a page by POST into the page buffer. If there is no base for the URL then the current base URL is used. After the fetch the base URL reflects the new location.

Parameters:
url - URL to fetch.
parameters - Optional additional GET data.
Returns:
True on success.

web_tester.php at line 596

restart

public void restart(string/integer date)

Simulates the closing and reopening of the browser. Temporary cookies will be discarded and timed cookies will be expired if later than the specified time.

Parameters:
date - Time when session restarted. If ommitted then all persistent cookies are kept. Time is either Cookie format string or timestamp.

web_tester.php at line 723

retry

public boolean retry()

Equivalent to hitting the retry button on the browser. Will attempt to repeat the page fetch.

Returns:
True if fetch succeeded.

web_tester.php at line 519

setBrowser

public void setBrowser(SimpleBrowser browser)

Gets a current browser reference for setting special expectations or for detailed examination of page fetches.

Parameters:
browser - New test browser object.

web_tester.php at line 652

setConnectionTimeout

public void setConnectionTimeout(integer timeout)

Sets the socket timeout for opening a connection and receiving at least one byte of information.

Parameters:
timeout - Maximum time in seconds.

web_tester.php at line 782

setCookie

public void setCookie(string name, string value, string host, string path, string expiry)

Sets a cookie in the current browser.

Parameters:
name - Name of cookie.
value - Cookie value.
host - Host upon which the cookie is valid.
path - Cookie path if not host wide.
expiry - Expiry date.

web_tester.php at line 1016

setField

public boolean setField(string name, string value)

Sets all form fields with that name.

Parameters:
name - Name of field in forms.
value - New value of field.
Returns:
True if field exists, otherwise false.

web_tester.php at line 1027

setFieldById

public boolean setFieldById(string/integer id, string value)

Sets all form fields with that name.

Parameters:
id - Id of field in forms.
value - New value of field.
Returns:
True if field exists, otherwise false.

web_tester.php at line 814

setFrameFocus

public boolean setFrameFocus(string name)

Sets the focus by name.

Parameters:
name - Chosen frame.
Returns:
True if frame exists.

web_tester.php at line 804

setFrameFocusByIndex

public boolean setFrameFocusByIndex(integer choice)

Sets the focus by index. The integer index starts from 1.

Parameters:
choice - Chosen frame.
Returns:
True if frame exists.

web_tester.php at line 638

setMaximumRedirects

public void setMaximumRedirects(integer max)

Sets the maximum number of redirects before the web page is loaded regardless.

Parameters:
max - Maximum hops.

web_tester.php at line 573

showHeaders

public void showHeaders()

Dumps the current HTTP headers for debugging.


web_tester.php at line 565

showRequest

public void showRequest()

Dumps the current request for debugging.


web_tester.php at line 581

showSource

public void showSource()

Dumps the current HTML source for debugging.


web_tester.php at line 921

submitFormById

public boolean/string submitFormById(string id)

Submits a form by the ID.

Parameters:
id - Form ID. No button information is submitted this way.
Returns:
Page on success.

web_tester.php at line 528

unsetBrowser

public void unsetBrowser()

Clears the current browser reference to help the PHP garbage collector.


web_tester.php at line 665

useProxy

public void useProxy(string proxy, string username, string password)

Sets proxy to use on all requests for when testing from behind a firewall. Set URL to false to disable.

Parameters:
proxy - Proxy URL.
username - Proxy username for authentication.
password - Proxy password for authentication.

SimpleTest