2016-03-21 49 views
0

我需要同时使用两个浏览器窗口进行测试。我使用硒和phpunit。如何在php中使用selenium中的两个浏览器

实施例:

  1. 打开browser1并导航到某些URL
  2. 复印一些dinamyc内容
  3. 打开browser2,导航到其它的url,填充形式与步骤2的内容和提交表单。

我无法从浏览器1导航到第3步中的网址,因为那样它就无法工作。

现在我无法打开browser2,我做的每一个尝试都会使用browser1。

任何想法? 谢谢。

回答

0

感谢Keith Tyler。我玩了一下代码,最后我能够做到。

我会把代码放在这里,因为它可能对某人有用。

第一件事情就是创建一个类扩展PHPUnit_Extensions_Selenium2TestCase:

class Browser extends PHPUnit_Extensions_Selenium2TestCase 
{ 
    public function __construct(){ 
     parent::__construct(); 
     $this->setHost("127.0.0.1"); 
     $this->setPort(4444); 
     $this->setBrowser("firefox"); 
     $this->setBrowserUrl("url"); 
     $this->prepareSession(); // this does the trick 
    } 
} 

然后你可以使用它像这样:

$this->url("url1"); // $this will be the default browser 
$browser2 = new Browser(); // $browser2 is the new browser and has all the functions from phpunit and selenium available 
$browser2->url("url2"); 

希望这将节省时间的人。

1

我已经这样做了。您基本上需要第二个驱动程序对象,并在该对象上使用open()。所以现在你有两个驱动程序对象 - 一个用于浏览器1和一个用于浏览器2.你必须记住哪个驱动程序对象。因为如果你想在浏览器2中触发一个动作,你需要在第二个驱动程序对象上调用所需的功能,而不是默认的。

这不是很直观,因为开箱即用的大多数硒API几乎给你一个单身的驱动程序对象,而没有真正要求。

相关问题