سؤال

I am newbie in PHPunit with SeleniumTest.

http://phpunit.de/manual is not clear for me. there is so many options and I tryed many of them but dont know how to connect to work all together.

I have One test suite, in test suite there are 4 test cases.

I would like to know next thing:

  • do I need to have in each test case script SetUp() and tearDown() ??
  • how to connect with my SQL database ??

this is one of 4 test cases:

<?php
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
  protected function setUp()
  {
    $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://blabla.com");
  }

  public function testMyTestCase()
  {
    $this->open("/");
    $this->type("id=signin_username", "john");
    $this->type("id=signin[password]", "1234567");
    $this->click("id=tsubmit");
    $this->waitForPageToLoad("30000");
    $this->open("/shoes");
    $this->assertEquals("RE", $this->getText("//div[@id='sf_admin_content']/div/table/tbody/tr[7]/td[2]"));
    $this->open("/shoes/7");
    $this->assertEquals("Status: Active", $this->getText("//div[@id='sf_fieldset_account']/div[2]"));
    $this->open("/logout");
  }
}
?>
هل كانت مفيدة؟

المحلول

Selenium test requires URL of site and browser. So you need to write setUp() in test case. But you can write special class extending PHPUnit_Extensions_SeleniumTestCase, put into it setUp() and extend other your test cases.

<?php
class BlablaTestCase extends PHPUnit_Extensions_SeleniumTestCase
{
  protected function setUp()
  {
    $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://blabla.com");
  }
}

In other file:

<?php
require_once 'BlablaTestCase.php';

class ExampleTestCase extends BlablaTestCase
{
  public function testMyTestCase()
  {
    $this->open("/");
    $this->type("id=signin_username", "john");
  }
}

But it is not necessary to devide tests for one site. You can write different tests in one test case:

<?php
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
  protected function setUp()
  {
    $this->setBrowser("*firefox");
    $this->setBrowserUrl("http://blabla.com");
  }

  public function testMyTestCase()
  {
    $this->open("/");
    $this->type("id=signin_username", "john");
    /* ... */
  }

  public function testMyTestCase2(){
    $this->open("/");
    $this->type("id=signin_username", "peter");
    /* ... */
  }

}
?>

tearDown() is optional. You can use it if you need to collect some results or clean up something after tests.

You should not connect to your SQL DB in Selenium test. All Selenium tests run in browser, do UI interactions like user.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top