我正在用selenium的facebook的php-web驅動來實現我的網站的集成測試。我使用phphunit運行我的測試,幷包裹着我的phpunitframework_testcase擴展分類中代碼:通過facebook使用phpunit和php-web驅動來測試你的多代瀏覽器的代碼
class WebTest extends PHPUnit_Framework_TestCase
{
/**
* @var \RemoteWebDriver
*/
protected $driver;
protected $url='http://example.com';
protected $screenShotDirectoryPath = '/screenshots/';
/**
* @BeforeMethod
*/
protected function setUp()
{
try{
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME=>WebDriverBrowserType::FIREFOX);
$this->driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities, 5000);
} catch (Exception $e){
echo $e->getMessage();
}
}
/**
* @AfterMethod
*/
public function tearDown()
{
$this->driver->close();
}
public function testImageUpload()
{
$errorSnaps='';
$myBrowserDriver = $this->driver;
//open the url
$myBrowserDriver->get($this->url);
try{
//get email field in login page
$emailField = $myBrowserDriver->findElement(WebDriverBy::id('email'));
//check if the field is displayed
if(!$emailField->isDisplayed()) {
try {
$errorSnaps = $this->takeScreenshot();
$this->errorLogs[] = "The email input Element is not present, a screen-shot of the error has been placed here --> " . $errorSnaps;
} catch (Exception $e){
$this->errorLogs[] = $e->getMessage();
}
}
} catch (NoSuchElementException $nse){
try {
$errorSnaps = $this->TakeScreenshot();
$this->errorLogs[] = "The email field on ".$this->driver->getCurrentURL()." not found , a screen-shot for the error has been placed here -->" . $errorSnaps;
}catch (Exception $e){
$this->errorLogs[]=$e->getMessage();
}
}
}
PHP-webdrivers文檔推薦這種方式初始化瀏覽器的驅動
$capabilities=array(
\WebDriverCapabilityType::BROWSER_NAME=>WebDriverBrowserType::FIREFOX
);
$this->driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities, 5000);
,但不提供機制來啓動多個瀏覽器驅動程序來運行我的測試只有一個測試文件意味着考慮上面的代碼我必須爲所有這些瀏覽器做不同的副本,並且只有一行代碼差異意味着如果我想運行上面的測試chrome然後我必須從
更改該行$capabilities=array(\WebDriverCapabilityType::BROWSER_NAME=>WebDriverBrowserType::FIREFOX);
到
$capabilities=array(\WebDriverCapabilityType::BROWSER_NAME=>WebDriverBrowserType::CHROME);
和如上保存代碼,其餘全部相同的代碼在不同的文件並運行我的測試套裝。正如你所看到的,這不是實現我的測試和爲了代碼重用性的最佳方式。
我碰到2個選項:從終端與參數發送瀏覽器的名稱一樣phpunit brName=chrome
,並通過$_SERVER['brName']
得到它
- 通行證的說法。每次我想爲任何其他瀏覽器運行測試時,我仍然需要輸入。
- 我在phpunit網站上遇到了下面的
TestDecorator
類,它看起來像一個更常規的方式來實現我正在做的事情,但無法弄清楚我將如何使用它來運行我的測試。
我應該在哪裏放置代碼以便檢測並運行它?每次我嘗試運行下面的示例代碼時,都會說沒有執行測試。如果我有下面的樣本測試函數,我可以如何使用testdecorator作爲基類來運行它4次?
樣品測試:
public function sampleTest(){
$this->assertTrue(TRUE);
}
測試裝飾類:
require_once 'PHPUnit/Extensions/TestDecorator.php';
class PHPUnit_Extensions_RepeatedTest extends PHPUnit_Extensions_TestDecorator
{
private $timesRepeat = 1;
public function __construct(PHPUnit_Framework_Test $test, $timesRepeat = 1)
{
parent::__construct($test);
if (is_integer($timesRepeat) &&
$timesRepeat >= 0) {
$this->timesRepeat = $timesRepeat;
}
}
public function count()
{
return $this->timesRepeat * $this->test->count();
}
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
if ($result === NULL) {
$result = $this->createResult();
}
for ($i = 0; $i < $this->timesRepeat && !$result->shouldStop(); $i++) {
$this->test->run($result);
}
return $result;
}
}
就行:陣列(\ WebDriverCa pabilityType :: BROWSER_NAME => WebDriverBrowserType :: FIREFOX);是「\」語法錯誤嗎? –
這是一個錯字寫在這裏 –
我試圖編輯這個更有價值的職位。作爲旁註,由於您每隔一行切換約定,因此很難遵循您的代碼。爲了可讀性,您可能會考慮選擇一些編碼約定並堅持使用。 –