【问题标题】:PHPUnit & Selenium - How to get driver being used by a test?PHPUnit & Selenium - 如何让驱动程序被测试使用?
【发布时间】:2013-02-24 07:50:15
【问题描述】:

我的一般测试设置如下所示:

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');
    }
}

从这里开始,当我使用 PHPUnit 运行 MyTest.php 时,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() 只是在测试中添加了更多的驱动程序,并且只假设被设置使用。有任何想法吗?谢谢!

【问题讨论】:

  • 如果您需要确定这样的事情,您应该查看它的来源。试试$this->drivers[0]->getBrowser()
  • @ColinMorelli:我正在查看源代码。我看过$this->drivers,但是,这段代码不是让我成为第一个驱动程序吗?问题是我想获取测试当前正在使用的驱动程序。关于如何让它发挥作用的任何建议?

标签: php selenium phpunit driver


【解决方案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)

【讨论】:

  • 哦!我以为它只包含一个驱动程序列表。太好了。
猜你喜欢
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 2023-04-09
  • 2013-12-24
相关资源
最近更新 更多