问题
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?
回答1:
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;
}
回答2:
For functions such as waitForPageToLoad() and clickAndWait(), which are unavailable in Selenium2, you can reproduce those functions by using try catch blocks, in conjunction with implicit waits and explicit sleeps.
So, for a function like clickAndWait(), you can define what element you are waiting for, and then check for that element's existence for a set amount of seconds. If the element doesn't exist, the try catch block will stop the error from propagating. If the element does exist, you can continue. If the element doesn't exist after the set amount of time, then bubble up the error.
I would recommend using Selenium2 and then reproducing any functionality that you feel is missing from within your framework.
EXAMPLE:
def wait_for_present(element, retry = 10, seconds = 2)
for i in 0...retry
return true if element.present?
sleep(seconds)
end
return false
end
回答3:
you can try use traits to extend two different classes http://php.net/manual/en/language.oop5.traits.php
class PHPUnit_Extensions_SeleniumTestCase{
...
}
change PHPUnit_Extensions_Selenium2TestCase class to trait:
trait PHPUnit_Extensions_Selenium2TestCase {
...
}
class blabla extends PHPUnit_Extensions_SeleniumTestCase {
use PHPUnit_Extensions_Selenium2TestCase;
your tests here..
}
回答4:
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 thewaitForPageToLoad()
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 thewaitForPageToLoad()
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?
来源:https://stackoverflow.com/questions/15746217/phpunit-selenium-usage