Skip to content

xp-framework/webtest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webtest

Build Status on TravisCI XP Framework Module BSD Licence Required PHP 5.4+ Supports PHP 7.0+ Supports HHVM 3.4+ Latest Stable Version

Web tests for the XP Framework

Example

The following web test case consists of opening GitHub's homepage and asserting the title to equal the company's name:

use unittest\web\WebTestCase;
use peer\http\HttpConstants;
use peer\http\HttpConnection;

#[@webtest(url= 'https://github.com/')]
class GitHubTestCase extends WebTestCase {

  #[@test]
  public function home() {
    $this->beginAt('/');
    $this->assertStatus(HttpConstants::STATUS_OK);
    $this->assertTitleEquals('GitHub · Build software better, together.');
  }
}

Running it works as with normal test cases:

$ unittest GitHubTestCase
[.]

✓: 1/1 run (0 skipped), 1 succeeded, 0 failed
Memory used: 1861.12 kB (2474.66 kB peak)
Time taken: 1.225 seconds

To overwrite the default URL specified in the annotation, supply it as command line argument, e.g. unittest GitHubTestCase -a https://github.staging.lan/.

Assertion methods

On top of the assertion methods provided by the unittest library, the following response-related assertions are available:

public void assertStatus(int $status)
public void assertUrlEquals(peer.URL $url)
public void assertContentType(string $ctype)
public void assertHeader(string $header, string $value)
public void assertElementPresent(string $id)
public void assertTextPresent(string $text)
public void assertImagePresent(string $src)
public void assertLinkPresent(string $url)
public void assertLinkPresentWithText(string $text)
public void assertFormPresent(string $name= null)
public void assertTitleEquals($title)

Navigation

To follow links inside a page, a web test can utilize the click methods:

protected void clickLink(string $id);
protected void clickLinkWithText(string $text);

Forms

To work with forms, the getForm() method can be used:

use unittest\web\WebTestCase;
use peer\http\HttpConstants;
use peer\http\HttpConnection;

#[@webtest(url= 'https://github.com/')]
class GitHubTestCase extends WebTestCase {

  #[@test]
  public function search_for() {
    $this->beginAt('/');
    $form= $this->getForm();
    $form->getField('q')->setValue('XP Framework');
    $form->submit();
    $this->assertStatus(HttpConstants::STATUS_OK);
    $this->assertTitleEquals('Search · XP Framework · GitHub');
  }
}

See also

xp-framework/rfc#169