2013-03-08 69 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

相关问题