2013-03-08 68 views
2

我的一般測試設置看起來像:PHPUnit和Selenium - 如何獲取測試使用的驅動程序?

class MySeleniumTest extends PHPUnit_Extensions_SeleniumTestCase{ 

    public static $browsers = array(
     array(
      'name' => 'Mozilla - Firefox', 
      'browser' => '*firefox', 
      'host' => 'localhost', 
      'port' => 4444, 
      'timeout' => 30000, 
     ), 
     array(
      'name' => 'Google - Chrome', 
      'browser' => '*googlechrome', 
      'host' => 'localhost', 
      'port' => 4444, 
      'timeout' => 30000, 
     ) 
    ); 

    //etc 
} 

,從這裏單獨的測試文件看起來像:

class MyTest extends MySeleniumTest{ 
    public function setUp(){ 
     parent::setUp(); 
     $this->setUser(1); 
    } 

    public function testPageTitle(){ 
     //Login and open the test page. 
     $this->login(8); 
     $this->open('/test/page'); 
     //Check the title. 
     $this->assertTitle('Test Page'); 
    } 
} 

從這裏,當我運行MyTest.php PHPUnit的,PHPUnit的自動運行的每個測試用例在MyTest.php。此外,它會分別在每個指定的瀏覽器上運行每個測試。我想要做的就是獲取有關驅動程序在該測試用例中運行特定測試用例的信息。所以像這樣:

public function testPageTitle(){ 
    //Login and open the test page. 
    $this->login(8); 
    $this->open('/test/page'); 
    //Check the title. 
    $this->assertTitle('Test Page'); 

    $driver = $this->getDriver(); 
    print($driver['browser']); //or something. 
} 

但是,這是行不通的。而$this->getDrivers()只是在測試中增加了更多的驅動程序,並且僅供設置使用。有任何想法嗎?謝謝!

+0

你應該看一下它的源代碼,如果你需要確定這樣的事情。試試'$ this-> drivers [0] - > getBrowser()' – 2013-03-08 17:19:32

+0

@ColinMorelli:我正在瀏覽源代碼。我曾看過'$ this-> drivers',但是,這段代碼是不是讓我成爲第一個驅動程序?問題是我想獲得當前測試使用的驅動程序。有關如何獲得這項工作的任何建議? – 2013-03-08 17:28:13

回答

1

即使$this->drivers是一個數組,它總是隻有一個元素。您可以檢查here。因此, $this->drivers[0]包含有關當前正在運行的瀏覽器的信息,您可以使用$this->drivers[0]->getBrowser()來輸出瀏覽器名稱。

例子:

require_once 'MySeleniumTest.php'; 

class MyTest extends MySeleniumTest{ 
    public function setUp(){ 
     parent::setUp(); 
     $this->setBrowserUrl('http://www.google.com/'); 
    } 

    public function testPageTitle(){ 
     $this->open('http://google.com'); 

     echo "{$this->drivers[0]->getBrowser()}\n"; 
    } 
} 

輸出:

PHPUnit 3.7.18 by Sebastian Bergmann. 

.*firefox 
.*googlechrome 


Time: 7 seconds, Memory: 3.50Mb 

OK (2 tests, 0 assertions) 
+0

哦!我認爲它只包含一個驅動程序列表。大。 – 2013-03-09 18:10:23

相關問題