Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phpunit selenium usage

My question is about phpunit+selenium usage.
The standard usage of this union is

class BlaBlaTest extends PHPUnit_Extensions_SeleniumTestCase
{... }  

OR

class BlaBlaTest extends PHPUnit_Extensions_Selenium2TestCase  
{...}  

The first one (PHPUnit_Extensions_SeleniumTestCase) is not very convinient to use (e.g. there is no such thing as $this->elements('xpath')).
Second(PHPUnit_Extensions_Selenium2TestCase) also has limited functionality (e.g. there is no such functions as waitForPageToLoad() or clickAndWait(), and using something like $this->timeouts()->implicitWait(10000) looks for me like complete nonsense).

Is it possible to use the functional

PHPUnit_Extensions_SeleniumTestCase + PHPUnit_Extensions_Selenium2TestCase

in one test class? Maybe smb knows good alternatives to phpunit+selenium?

like image 887
tarnum26 Avatar asked Sep 11 '26 20:09

tarnum26


2 Answers

Inspired by Dan I've written this for use in PHPUnit_Extensions_Selenium2TestCase and it seems to work ok:

/** 
 * @param string $id - DOM id
 * @param int $wait - maximum (in seconds)
 * @retrn element|false - false on time-out
 */ 
protected function waitForId($id, $wait=30) { 
  for ($i=0; $i <= $wait; $i++) { 
    try{ 
      $x = $this->byId($id); 
      return $x; 
    } 
    catch (Exception $e) { 
      sleep(1); 
    } 
  } 
  return false; 
} 
like image 126
ErichBSchulz Avatar answered Sep 14 '26 14:09

ErichBSchulz


Sorry for resurrecting this but I'd like to hopefully clear up some confusion for anyone stumbling across this.

You're saying that you wanted functionality from RC and WebDriver combined where there are workarounds to it, but I wouldn't recommend it. Firstly you'll need to understand the difference between both frameworks.

My brief definitions...

  • Selenium RC (PHPUnit_Extensions_SeleniumTestCase) is script oriented. By that I mean it will run your tests exactly how you expect the page to respond. This often will require more explicit commands such as the waitForPageToLoad() that you have mentioned when waiting for elements to appear and/or pages to loads.
  • Selenium WebDriver (PHPUnit_Extensions_Selenium2TestCase) uses a more native approach. It cuts off 'the middle-man' and runs your tests through your chosen browsers driver. Using the waitForPageToLoad() example, you wont need to explicitly put that wherever you open a page in your code because the WebDriver already knows when the page is loading and will resume the test when the page load request is complete.

If you need to define an implicit timeout in WebDriver, you will only need to place that in the setUp() method within a base Selenium class that will be extended in your test classes;

class BaseSelenium extends PHPUnit_Extensions_Selenium2TestCase {

     protected function setUp() {

           // Whatever else needs to be in here like setting
           // the host url and port etc.

           $this->setSeleniumServerRequestsTimeout( 100 ); // <- seconds
     }
}

That will happily span across all of your tests and will timeout whenever a page takes longer than that to load.

Although I personally prefer WebDriver over RC (mainly because it's a lot faster!) there is a big difference between the methods available. Whenever I got stuck when recently converting a lot a RC tests to WebDriver I always turned to this first. It's a great reference to nearly every situation.

I hope that helps?

like image 21
Paradiddley Avatar answered Sep 14 '26 13:09

Paradiddley