2014-12-31 16 views
1

我正在用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']得到它

  1. 通行證的說法。每次我想爲任何其他瀏覽器運行測試時,我仍然需要輸入。
  2. 我在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; 
    } 
} 
+0

就行:陣列(\ WebDriverCa pabilityType :: BROWSER_NAME => WebDriverBrowserType :: FIREFOX);是「\」語法錯誤嗎? –

+0

這是一個錯字寫在這裏 –

+1

我試圖編輯這個更有價值的職位。作爲旁註,由於您每隔一行切換約定,因此很難遵循您的代碼。爲了可讀性,您可能會考慮選擇一些編碼約定並堅持使用。 –

回答

0

我是不會在正確的方向,以實現這個場景我不應該使用testdecorator類,但一個更良好,簡單的方法將是。

  1. 經由CMD線設置的環境變量/終端等 export BROWSER_REQUESTED=chrome爲(UNIX)和set BROWSER_REQUESTED=chrome爲(視窗),也可以用下面的代碼 export BROWSER_REQUESTED=chrome && phpunit創建.sh文件並經由CMD-運行它線。

  2. 創建一個BrowserFatory類,該類監聽環境變量併爲所請求的瀏覽器類型啓動驅動程序。

創造儘可能多的.sh文件儘可能多的瀏覽器,你需要運行的測試,我已經添加下面的代碼更具描述。

WebTest.php

class WebTest extends PHPUnit_Framework_TestCase 
{ 
    /** 
    * @var \RemoteWebDriver 
    */ 
    protected $driver; 
    protected $url='http://example.com'; 
    protected $screenShotDirectoryPath = '/screenshots/'; 


    /** 
    * @BeforeMethod 
    */ 
    protected function setUp() 
    { 
     try{ 
      $this->driver = BrowserFactory::drivers(); 
     } catch (Exception $e){ 
      echo $e->getMessage(); 
     } 
    } 
} 

BrowserFactory.php

class BrowserFactory 
{ 
    public static function drivers() 
    { 
     switch ($_SERVER['BROWSER_REQUESTED']) { 
      case 'chrome': 
       return self::createChrome(); 
       break; 
      case "ie": 
       throw new Exception('Not implemented'); 
       break; 

      case 'firefox': 
      default: 
       return self::createFirefox(); 
       break; 
     } 
    } 

    public static function createChrome() 
    { 
     putenv("webdriver.chrome.driver=/path/to/chromedriver"); 

     $service = ChromeDriverService::createDefaultService(); 
     $service->start(); 

     return ChromeDriver::start(DesiredCapabilities::chrome(), $service); 
    } 

    public static function createFirefox() 
    { 
     // these are just constants defined in bootstrap.php 
     $seleniumUrl = isset($_SERVER['JENKINS_URL']) ? TEST_ENV_SELENIUM_SERVER : LOCAL_ENV_SELENIUM_SERVER; 

     return RemoteWebDriver::create(
      $seleniumUrl, DesiredCapabilities::firefox() 
     ); 
    } 
} 

command-line.sh

export BROWSER_REQUESTED='chrome' && phpunit 
+0

不錯的工作我從日子裏尋找同樣的東西,最終在這裏。賓果 –

+1

很高興你能從中受益 –

相關問題