web_tester.php
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 1,541 行 · 第 1/4 页
PHP
1,541 行
* Sets the reject pattern
* @param string $substring Text to search for.
* @param string $message Customised message on failure.
* @access public
*/
function NoTextExpectation($substring, $message = '%s') {
$this->TextExpectation($substring, $message);
}
/**
* Tests the expectation. False if the substring appears
* in the text.
* @param string $compare Comparison value.
* @return boolean True if correct.
* @access public
*/
function test($compare) {
return ! parent::test($compare);
}
/**
* Returns a human readable test message.
* @param string $compare Comparison value.
* @return string Description of success
* or failure.
* @access public
*/
function testMessage($compare) {
if ($this->test($compare)) {
$dumper = &$this->_getDumper();
return "Text [" . $this->_getSubstring() .
"] not detected in [" .
$dumper->describeValue($compare) . "]";
} else {
return $this->_describeTextMatch($this->_getSubstring(), $compare);
}
}
}
/**
* Test case for testing of web pages. Allows
* fetching of pages, parsing of HTML and
* submitting forms.
* @package SimpleTest
* @subpackage WebTester
*/
class WebTestCase extends SimpleTestCase {
var $_browser;
var $_ignore_errors = false;
/**
* Creates an empty test case. Should be subclassed
* with test methods for a functional test case.
* @param string $label Name of test case. Will use
* the class name if none specified.
* @access public
*/
function WebTestCase($label = false) {
$this->SimpleTestCase($label);
}
/**
* Announces the start of the test.
* @param string $method Test method just started.
* @access public
*/
function before($method) {
parent::before($method);
$this->setBrowser($this->createBrowser());
}
/**
* Announces the end of the test. Includes private clean up.
* @param string $method Test method just finished.
* @access public
*/
function after($method) {
$this->unsetBrowser();
parent::after($method);
}
/**
* Gets a current browser reference for setting
* special expectations or for detailed
* examination of page fetches.
* @return SimpleBrowser Current test browser object.
* @access public
*/
function &getBrowser() {
return $this->_browser;
}
/**
* Gets a current browser reference for setting
* special expectations or for detailed
* examination of page fetches.
* @param SimpleBrowser $browser New test browser object.
* @access public
*/
function setBrowser(&$browser) {
return $this->_browser = &$browser;
}
/**
* Clears the current browser reference to help the
* PHP garbage collector.
* @access public
*/
function unsetBrowser() {
unset($this->_browser);
}
/**
* Creates a new default web browser object.
* Will be cleared at the end of the test method.
* @return TestBrowser New browser.
* @access public
*/
function &createBrowser() {
$browser = &new SimpleBrowser();
return $browser;
}
/**
* Gets the last response error.
* @return string Last low level HTTP error.
* @access public
*/
function getTransportError() {
return $this->_browser->getTransportError();
}
/**
* Accessor for the currently selected URL.
* @return string Current location or false if
* no page yet fetched.
* @access public
*/
function getUrl() {
return $this->_browser->getUrl();
}
/**
* Dumps the current request for debugging.
* @access public
*/
function showRequest() {
$this->dump($this->_browser->getRequest());
}
/**
* Dumps the current HTTP headers for debugging.
* @access public
*/
function showHeaders() {
$this->dump($this->_browser->getHeaders());
}
/**
* Dumps the current HTML source for debugging.
* @access public
*/
function showSource() {
$this->dump($this->_browser->getContent());
}
/**
* Dumps the visible text only for debugging.
* @access public
*/
function showText() {
$this->dump(wordwrap($this->_browser->getContentAsText(), 80));
}
/**
* 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.
* @param string/integer $date Time when session restarted.
* If ommitted then all persistent
* cookies are kept. Time is either
* Cookie format string or timestamp.
* @access public
*/
function restart($date = false) {
if ($date === false) {
$date = time();
}
$this->_browser->restart($date);
}
/**
* Moves cookie expiry times back into the past.
* Useful for testing timeouts and expiries.
* @param integer $interval Amount to age in seconds.
* @access public
*/
function ageCookies($interval) {
$this->_browser->ageCookies($interval);
}
/**
* Disables frames support. Frames will not be fetched
* and the frameset page will be used instead.
* @access public
*/
function ignoreFrames() {
$this->_browser->ignoreFrames();
}
/**
* Switches off cookie sending and recieving.
* @access public
*/
function ignoreCookies() {
$this->_browser->ignoreCookies();
}
/**
* Skips errors for the next request only. You might
* want to confirm that a page is unreachable for
* example.
* @access public
*/
function ignoreErrors() {
$this->_ignore_errors = true;
}
/**
* Issues a fail if there is a transport error anywhere
* in the current frameset. Only one such error is
* reported.
* @param string/boolean $result HTML or failure.
* @return string/boolean $result Passes through result.
* @access private
*/
function _failOnError($result) {
if (! $this->_ignore_errors) {
if ($error = $this->_browser->getTransportError()) {
$this->fail($error);
}
}
$this->_ignore_errors = false;
return $result;
}
/**
* Adds a header to every fetch.
* @param string $header Header line to add to every
* request until cleared.
* @access public
*/
function addHeader($header) {
$this->_browser->addHeader($header);
}
/**
* Sets the maximum number of redirects before
* the web page is loaded regardless.
* @param integer $max Maximum hops.
* @access public
*/
function setMaximumRedirects($max) {
if (! $this->_browser) {
trigger_error(
'Can only set maximum redirects in a test method, setUp() or tearDown()');
}
$this->_browser->setMaximumRedirects($max);
}
/**
* Sets the socket timeout for opening a connection and
* receiving at least one byte of information.
* @param integer $timeout Maximum time in seconds.
* @access public
*/
function setConnectionTimeout($timeout) {
$this->_browser->setConnectionTimeout($timeout);
}
/**
* Sets proxy to use on all requests for when
* testing from behind a firewall. Set URL
* to false to disable.
* @param string $proxy Proxy URL.
* @param string $username Proxy username for authentication.
* @param string $password Proxy password for authentication.
* @access public
*/
function useProxy($proxy, $username = false, $password = false) {
$this->_browser->useProxy($proxy, $username, $password);
}
/**
* 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.
* @param string $url URL to fetch.
* @param hash $parameters Optional additional GET data.
* @return boolean/string Raw page on success.
* @access public
*/
function get($url, $parameters = false) {
return $this->_failOnError($this->_browser->get($url, $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.
* @param string $url URL to fetch.
* @param hash $parameters Optional additional GET data.
* @return boolean/string Raw page on success.
* @access public
*/
function post($url, $parameters = false) {
return $this->_failOnError($this->_browser->post($url, $parameters));
}
/**
* Does a HTTP HEAD fetch, fetching only the page
* headers. The current base URL is unchanged by this.
* @param string $url URL to fetch.
* @param hash $parameters Optional additional GET data.
* @return boolean True on success.
* @access public
*/
function head($url, $parameters = false) {
return $this->_failOnError($this->_browser->head($url, $parameters));
}
/**
* Equivalent to hitting the retry button on the
* browser. Will attempt to repeat the page fetch.
* @return boolean True if fetch succeeded.
* @access public
*/
function retry() {
return $this->_failOnError($this->_browser->retry());
}
/**
* Equivalent to hitting the back button on the
* browser.
* @return boolean True if history entry and
* fetch succeeded.
* @access public
*/
function back() {
return $this->_failOnError($this->_browser->back());
}
/**
* Equivalent to hitting the forward button on the
* browser.
* @return boolean True if history entry and
* fetch succeeded.
* @access public
*/
function forward() {
return $this->_failOnError($this->_browser->forward());
}
/**
* Retries a request after setting the authentication
* for the current realm.
* @param string $username Username for realm.
* @param string $password Password for realm.
* @return boolean/string HTML on successful fetch. Note
* that authentication may still have
* failed.
* @access public
*/
function authenticate($username, $password) {
return $this->_failOnError(
$this->_browser->authenticate($username, $password));
}
/**
* Gets the cookie value for the current browser context.
* @param string $name Name of cookie.
* @return string Value of cookie or false if unset.
* @access public
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?