web_tester_documentation.html.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 585 行 · 第 1/2 页
SVN-BASE
585 行
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Simple Test for PHP web script testing documentation</title><link rel="stylesheet" type="text/css" href="docs.css" title="Styles"></head><body><div class="menu_back"><div class="menu"><a href="index.html">SimpleTest</a> | <a href="overview.html">Overview</a> | <a href="unit_test_documentation.html">Unit tester</a> | <a href="group_test_documentation.html">Group tests</a> | <a href="mock_objects_documentation.html">Mock objects</a> | <a href="partial_mocks_documentation.html">Partial mocks</a> | <a href="reporter_documentation.html">Reporting</a> | <a href="expectation_documentation.html">Expectations</a> | <span class="chosen">Web tester</span> | <a href="form_testing_documentation.html">Testing forms</a> | <a href="authentication_documentation.html">Authentication</a> | <a href="browser_documentation.html">Scriptable browser</a></div></div><h1>Web tester documentation</h1> This page... <ul><li> Successfully <a href="#fetch">fetching a web page</a> </li><li> Testing the <a href="#content">page content</a> </li><li> <a href="#navigation">Navigating a web site</a> while testing </li><li> <a href="#request">Raw request modifications</a> and debugging methods </li></ul><div class="content"> <p><a class="target" name="fetch"><h2>Fetching a page</h2></a></p> <p> Testing classes is all very well, but PHP is predominately a language for creating functionality within web pages. How do we test the front end presentation role of our PHP applications? Well the web pages are just text, so we should be able to examine them just like any other test data. </p> <p> This leads to a tricky issue. If we test at too low a level, testing for matching tags in the page with pattern matching for example, our tests will be brittle. The slightest change in layout could break a large number of tests. If we test at too high a level, say using mock versions of a template engine, then we lose the ability to automate some classes of test. For example, the interaction of forms and navigation will have to be tested manually. These types of test are extremely repetitive and error prone. </p> <p> SimpleTest includes a special form of test case for the testing of web page actions. The <span class="new_code">WebTestCase</span> includes facilities for navigation, content and cookie checks and form handling. Usage of these test cases is similar to the <a href="unit_tester_documentation.html">UnitTestCase</a>...<pre><strong>class TestOfLastcraft extends WebTestCase {}</strong></pre> Here we are about to test the <a href="http://www.lastcraft.com/">Last Craft</a> site itself. If this test case is in a file called <em>lastcraft_test.php</em> then it can be loaded in a runner script just like unit tests...<pre><?phprequire_once('simpletest/autorun.php');<strong>require_once('simpletest/web_tester.php');</strong>SimpleTest::prefer(new TextReporter());class WebTests extends TestSuite { function WebTests() { $this->TestSuite('Web site tests');<strong> $this->addFile('lastcraft_test.php');</strong> }}?></pre> I am using the text reporter here to more clearly distinguish the web content from the test output. </p> <p> Nothing is being tested yet. We can fetch the home page by using the <span class="new_code">get()</span> method...<pre>class TestOfLastcraft extends WebTestCase { <strong> function testHomepage() { $this->assertTrue($this->get('http://www.lastcraft.com/')); }</strong>}</pre> The <span class="new_code">get()</span> method will return true only if page content was successfully loaded. It is a simple, but crude way to check that a web page was actually delivered by the web server. However that content may be a 404 response and yet our <span class="new_code">get()</span> method will still return true. </p> <p> Assuming that the web server for the Last Craft site is up (sadly not always the case), we should see...<pre class="shell">Web site testsOKTest cases run: 1/1, Failures: 0, Exceptions: 0</pre> All we have really checked is that any kind of page was returned. We don't yet know if it was the right one. </p> <p><a class="target" name="content"><h2>Testing page content</h2></a></p> <p> To confirm that the page we think we are on is actually the page we are on, we need to verify the page content.<pre>class TestOfLastcraft extends WebTestCase { function testHomepage() {<strong> $this->get('http://www.lastcraft.com/'); $this->assertText('Why the last craft');</strong> }}</pre> The page from the last fetch is held in a buffer in the test case, so there is no need to refer to it directly. The pattern match is always made against the buffer. </p> <p> Here is the list of possible content assertions... <table><tbody> <tr><td><span class="new_code">assertTitle($title)</span></td><td>Pass if title is an exact match</td></tr> <tr><td><span class="new_code">assertText($text)</span></td><td>Pass if matches visible and "alt" text</td></tr> <tr><td><span class="new_code">assertNoText($text)</span></td><td>Pass if doesn't match visible and "alt" text</td></tr> <tr><td><span class="new_code">assertPattern($pattern)</span></td><td>A Perl pattern match against the page content</td></tr> <tr><td><span class="new_code">assertNoPattern($pattern)</span></td><td>A Perl pattern match to not find content</td></tr> <tr><td><span class="new_code">assertLink($label)</span></td><td>Pass if a link with this text is present</td></tr> <tr><td><span class="new_code">assertNoLink($label)</span></td><td>Pass if no link with this text is present</td></tr> <tr><td><span class="new_code">assertLinkById($id)</span></td><td>Pass if a link with this id attribute is present</td></tr> <tr><td><span class="new_code">assertNoLinkById($id)</span></td><td>Pass if no link with this id attribute is present</td></tr> <tr><td><span class="new_code">assertField($name, $value)</span></td><td>Pass if an input tag with this name has this value</td></tr> <tr><td><span class="new_code">assertFieldById($id, $value)</span></td><td>Pass if an input tag with this id has this value</td></tr> <tr><td><span class="new_code">assertResponse($codes)</span></td><td>Pass if HTTP response matches this list</td></tr> <tr><td><span class="new_code">assertMime($types)</span></td><td>Pass if MIME type is in this list</td></tr> <tr><td><span class="new_code">assertAuthentication($protocol)</span></td><td>Pass if the current challenge is this protocol</td></tr> <tr><td><span class="new_code">assertNoAuthentication()</span></td><td>Pass if there is no current challenge</td></tr> <tr><td><span class="new_code">assertRealm($name)</span></td><td>Pass if the current challenge realm matches</td></tr> <tr><td><span class="new_code">assertHeader($header, $content)</span></td><td>Pass if a header was fetched matching this value</td></tr> <tr><td><span class="new_code">assertNoHeader($header)</span></td><td>Pass if a header was not fetched</td></tr> <tr><td><span class="new_code">assertCookie($name, $value)</span></td><td>Pass if there is currently a matching cookie</td></tr> <tr><td><span class="new_code">assertNoCookie($name)</span></td><td>Pass if there is currently no cookie of this name</td></tr> </tbody></table> As usual with the SimpleTest assertions, they all return false on failure and true on pass. They also allow an optional test message and you can embed the original test message inside using "%s" inside your custom message. </p> <p> So now we could instead test against the title tag with...<pre><strong>$this->assertTitle('The Last Craft? Web developer tutorials on PHP, Extreme programming and Object Oriented development');</strong></pre> ...or, if that is too long and fragile...<pre><strong>$this->assertTitle(new PatternExpectation('/The Last Craft/'));</strong></pre> As well as the simple HTML content checks we can check that the MIME type is in a list of allowed types with...<pre><strong>$this->assertMime(array('text/plain', 'text/html'));</strong></pre> More interesting is checking the HTTP response code. Like the MIME type, we can assert that the response code is in a list of allowed values...<pre>class TestOfLastcraft extends WebTestCase { function testRedirects() { $this->get('http://www.lastcraft.com/test/redirect.php'); $this->assertResponse(200);</strong> }}</pre> Here we are checking that the fetch is successful by allowing only a 200 HTTP response. This test will pass, but it is not actually correct to do so. There is no page, instead the server issues a redirect. The <span class="new_code">WebTestCase</span> will automatically follow up to three such redirects. The tests are more robust this way and we are usually interested in the interaction with the pages rather than their delivery. If the redirects are of interest then this ability must be disabled...<pre>class TestOfLastcraft extends WebTestCase { function testHomepage() {<strong> $this->setMaximumRedirects(0);</strong> $this->get('http://www.lastcraft.com/test/redirect.php'); $this->assertResponse(200); }}</pre> The assertion now fails as expected...
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?