SimpleTest


SimpleTest\SimpleBrowser
browser.php at line 172

Class SimpleBrowser

SimpleBrowser

public class SimpleBrowser

Simulated web browser. This is an aggregate of the user agent, the HTML parsing, request history and the last header set.

Subpackage:
WebTester

Constructor Summary

SimpleBrowser()

Starts with a fresh browser with no cookie or any other state information.

Method Summary
void

addHeader(string header)

Adds a header to every fetch.

void

ageCookies(integer interval)

Ages the cookies by the specified time.

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.

string/boolean

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

Clicks the submit image by some kind of label.

string/boolean

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

Clicks the submit image by ID attribute.

string/boolean

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

Clicks the submit image by the name.

string/boolean

clickLink(string label, integer index)

Follows a link by label.

string/boolean

clickLinkById(string id)

Follows a link by id attribute.

string/boolean

clickSubmit(string label, hash additional)

Clicks the submit button by label.

string/boolean

clickSubmitById(string id, hash additional)

Clicks the submit button by ID attribute of the button itself.

string/boolean

clickSubmitByName(string name, hash additional)

Clicks the submit button by name attribute.

boolean

forward()

Equivalent to hitting the forward button on the browser.

string

get(string/SimpleUrl url, hash/SimpleFormEncoding parameters)

Fetches the page content with a simple GET request.

array

getAbsoluteUrls()

Accessor for a list of all fixed links in current page.

string

getAuthentication()

Accessor for last Authentication type.

string

getContent()

Accessor for raw page information.

string

getContentAsText()

Accessor for plain text version of the page.

string

getCookieValue(string host, string path, string name)

Reads the most specific cookie value from the browser cookies.

string

getCurrentCookieValue(string name)

Reads the current cookies for the current URL.

string/boolean

getField(string name)

Accessor for a form element value within the page.

string/boolean

getFieldById(string/integer id)

Accessor for a form element value within the page.

integer/string/boolean

getFrameFocus()

Accessor for current frame focus.

array

getFrames()

Accessor for a breakdown of the frameset.

string

getHeaders()

Accessor for raw header information.

string

getMimeType()

Accessor for current MIME type.

string

getRealm()

Accessor for last Authentication realm.

array

getRelativeUrls()

Accessor for a list of all relative links.

string

getRequest()

Accessor for raw bytes sent down the wire.

integer

getResponseCode()

Accessor for last response code.

string

getTitle()

Accessor for parsed title.

string

getTransportError()

Accessor for last error.

string

getUrl()

Accessor for current URL of page or frame if focused.

boolean

head(string/SimpleUrl url, hash/SimpleFormEncoding parameters)

Fetches the page content with a HEAD request.

void

ignoreFrames()

Disables frames support.

boolean

isLink(string label)

Tests to see if a link is present by label.

boolean

isLinkById(string id)

Tests to see if a link is present by ID attribute.

string

post(string/SimpleUrl url, hash/SimpleFormEncoding parameters)

Fetches the page content with a POST request.

void

restart(string/integer date)

Removes expired and temporary cookies as if the browser was closed and re-opened.

string/boolean

retry()

Equivalent to hitting the retry button on the browser.

void

setConnectionTimeout(integer timeout)

Sets the socket timeout for opening a connection.

void

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

Sets an additional cookie.

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 id attribute.

boolean

setFrameFocus(string name)

Sets the focus by name.

boolean

setFrameFocusByIndex(integer choice)

Sets the focus by index.

void

setMaximumNestedFrames(integer max)

Sets the maximum number of nesting of framed pages within a framed page to prevent loops.

void

setMaximumRedirects(integer max)

Sets the maximum number of redirects before a page will be loaded anyway.

string/boolean

submitFormById(string id)

Submits a form by the ID.

void

useFrames()

Enables frames support.

void

useProxy(string proxy, string username, string password)

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

Constructor Detail

browser.php at line 186

SimpleBrowser

public SimpleBrowser()

Starts with a fresh browser with no cookie or any other state information. The exception is that a default proxy will be set up if specified in the options.


Method Detail

browser.php at line 340

addHeader

public void addHeader(string header)

Adds a header to every fetch.

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

browser.php at line 349

ageCookies

public void ageCookies(integer interval)

Ages the cookies by the specified time.

Parameters:
interval - Amount in seconds.

browser.php at line 577

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.

browser.php at line 537

back

public boolean back()

Equivalent to hitting the back button on the browser. The browser history is unchanged on failure.

Returns:
True if history entry and fetch succeeded

browser.php at line 640

clearFrameFocus

public void clearFrameFocus()

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


browser.php at line 884

clickImage

public string/boolean 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 - ID attribute of button.
x - X-coordinate of imaginary click.
y - Y-coordinate of imaginary click.
additional - Additional form data.
Returns:
Page on success.

browser.php at line 931

clickImageById

public string/boolean 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 data.
Returns:
Page on success.

browser.php at line 908

clickImageByName

public string/boolean 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 data.
Returns:
Page on success.

browser.php at line 970

clickLink

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

Follows a link by label. Will click the first link found with this link text by default, or a later one if an index is given. The match ignores case and white space issues.

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

browser.php at line 998

clickLinkById

public string/boolean clickLinkById(string id)

Follows a link by id attribute.

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

browser.php at line 822

clickSubmit

public string/boolean 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 data.
Returns:
Page on success.

browser.php at line 860

clickSubmitById

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

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

Parameters:
id - Button ID.
additional - Additional form data.
Returns:
Page on success.

browser.php at line 841

clickSubmitByName

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

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

Parameters:
name - Button name.
additional - Additional form data.
Returns:
Page on success.

browser.php at line 556

forward

public boolean forward()

Equivalent to hitting the forward button on the browser. The browser history is unchanged on failure.

Returns:
True if history entry and fetch succeeded

browser.php at line 468

get

public string get(string/SimpleUrl url, hash/SimpleFormEncoding parameters)

Fetches the page content with a simple GET request.

Parameters:
url - Target to fetch.
parameters - Additional parameters for GET request.
Returns:
Content of page or false.

browser.php at line 753

getAbsoluteUrls

public array getAbsoluteUrls()

Accessor for a list of all fixed links in current page.

Returns:
List of urls with scheme of http or https and hostname.

browser.php at line 677

getAuthentication

public string getAuthentication()

Accessor for last Authentication type. Only valid straight after a challenge (401).

Returns:
Description of challenge type.

browser.php at line 725

getContent

public string getContent()

Accessor for raw page information.

Returns:
Original text content of web page.

browser.php at line 734

getContentAsText

public string getContentAsText()

Accessor for plain text version of the page.

Returns:
Normalised text representation.

browser.php at line 377

getCookieValue

public string getCookieValue(string host, string path, string name)

Reads the most specific cookie value from the browser cookies.

Parameters:
host - Host to search.
path - Applicable path.
name - Name of cookie to read.
Returns:
False if not present, else the value as a string.

browser.php at line 388

getCurrentCookieValue

public string getCurrentCookieValue(string name)

Reads the current cookies for the current URL.

Parameters:
name - Key of cookie to find.
Returns:
Null if there is no current URL, false if the cookie is not set.

browser.php at line 797

getField

public string/boolean getField(string name)

Accessor for a form element value within the page. Finds the first match.

Parameters:
name - Field name.
Returns:
A string if the field is present, false if unchecked and null if missing.

browser.php at line 809

getFieldById

public string/boolean getFieldById(string/integer id)

Accessor for a form element value within the page.

Parameters:
id - Id of field in forms.
Returns:
A string if the field is present, false if unchecked and null if missing.

browser.php at line 611

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.

browser.php at line 599

getFrames

public array getFrames()

Accessor for a breakdown of the frameset.

Returns:
Hash tree of frames by name or index if no name.

browser.php at line 716

getHeaders

public string getHeaders()

Accessor for raw header information.

Returns:
Header block.

browser.php at line 658

getMimeType

public string getMimeType()

Accessor for current MIME type.

Returns:
MIME type as string; e.g. 'text/html'

browser.php at line 687

getRealm

public string getRealm()

Accessor for last Authentication realm. Only valid straight after a challenge (401).

Returns:
Name of security realm.

browser.php at line 762

getRelativeUrls

public array getRelativeUrls()

Accessor for a list of all relative links.

Returns:
List of urls without hostname.

browser.php at line 707

getRequest

public string getRequest()

Accessor for raw bytes sent down the wire.

Returns:
Original text sent.

browser.php at line 667

getResponseCode

public integer getResponseCode()

Accessor for last response code.

Returns:
Last HTTP response code received.

browser.php at line 743

getTitle

public string getTitle()

Accessor for parsed title.

Returns:
Title or false if no title is present.

browser.php at line 649

getTransportError

public string getTransportError()

Accessor for last error.

Returns:
Error from last response.

browser.php at line 697

getUrl

public string getUrl()

Accessor for current URL of page or frame if focused.

Returns:
Location of current page or frame as a string.

browser.php at line 443

head

public boolean head(string/SimpleUrl url, hash/SimpleFormEncoding parameters)

Fetches the page content with a HEAD request. Will affect cookies, but will not change the base URL.

Parameters:
url - Target to fetch as string.
parameters - Additional parameters for HEAD request.
Returns:
True if successful.

browser.php at line 221

ignoreFrames

public void ignoreFrames()

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


browser.php at line 988

isLink

public boolean isLink(string label)

Tests to see if a link is present by label.

Parameters:
label - Text of value attribute.
Returns:
True if link present.

browser.php at line 1012

isLinkById

public boolean isLinkById(string id)

Tests to see if a link is present by ID attribute.

Parameters:
id - Text of id attribute.
Returns:
True if link present.

browser.php at line 488

post

public string post(string/SimpleUrl url, hash/SimpleFormEncoding parameters)

Fetches the page content with a POST request.

Parameters:
url - Target to fetch as string.
parameters - POST parameters.
Returns:
Content of page.

browser.php at line 330

restart

public void restart(string/integer date)

Removes expired and temporary cookies as if the browser was closed and re-opened.

Parameters:
date - Time when session restarted. If omitted then all persistent cookies are kept.

browser.php at line 509

retry

public string/boolean retry()

Equivalent to hitting the retry button on the browser. Will attempt to repeat the page fetch. If there is no history to repeat it will give false.

Returns:
Content if fetch succeeded else false.

browser.php at line 417

setConnectionTimeout

public void setConnectionTimeout(integer timeout)

Sets the socket timeout for opening a connection.

Parameters:
timeout - Maximum time in seconds.

browser.php at line 363

setCookie

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

Sets an additional cookie. If a cookie has the same name and path it is replaced.

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

browser.php at line 773

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.

browser.php at line 784

setFieldById

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

Sets all form fields with that id attribute.

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

browser.php at line 631

setFrameFocus

public boolean setFrameFocus(string name)

Sets the focus by name.

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

browser.php at line 621

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.

browser.php at line 408

setMaximumNestedFrames

public void setMaximumNestedFrames(integer max)

Sets the maximum number of nesting of framed pages within a framed page to prevent loops.

Parameters:
max - Highest depth allowed.

browser.php at line 398

setMaximumRedirects

public void setMaximumRedirects(integer max)

Sets the maximum number of redirects before a page will be loaded anyway.

Parameters:
max - Most hops allowed.

browser.php at line 949

submitFormById

public string/boolean submitFormById(string id)

Submits a form by the ID.

Parameters:
id - The form ID. No submit button value will be sent.
Returns:
Page on success.

browser.php at line 230

useFrames

public void useFrames()

Enables frames support. Frames will be fetched from now on.


browser.php at line 430

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