我试图帮助朋友使用behat/mink
+ selenium2
自动执行某个流程。它正在工作,但不知何故在进程的中间,selenium2
启动与原始并行运行的新实例/会话。
有没有办法防止此行为并且只保留一个实例/会话?
这是我的代码(laravel控制器类):
class testBehat extends Controller
{
protected $session;
public function __construct()
{
set_time_limit(300);
$driver = new Selenium2Driver('firefox');
$capabilities = $driver->getDefaultCapabilities();
$driver->setWebDriver(new WebDriver('http://10.99.0.11:5555/wd/hub'));
$this->session = new Session($driver);
}
public function run()
{
$admin = User::find(0);
$simulator = Simulator::find(0);
$this->session->start();
$this->login($admin->username, $admin->pass);
if ($this->isElementExist('id', 'profilemenu')) {
$this->session->visit('http://dar*******.com/simulator/new/');
}
$this->upload($simulator);
file_put_contents(public_path('screenshoot/' . $admin->user . '-' . $simulator->name . '-success.jpg'), $this->session->getScreenshot());
$this->session->stop();
return 'success';
}
private function login($user, $pass)
{
$this->session->visit('http://dar*******.com/signin');
$this->handlePages('id', 'username', 'text', $user);
$this->handlePages('id', 'password', 'text', $pass);
$this->handlePages('id', 'submit', 'click');
file_put_contents(public_path('screenshoot/' . $user . '-' . '-loggedin.jpg'), $this->session->getScreenshot());
}
private function isElementExist($selector, $locator)
{
if ($selector === 'css')
{
while (!$this->session->getPage()->has($selector, $locator)) {
sleep(1);
file_put_contents(public_path('public/element.jpg'), $this->session->getScreenshot());
}
$return = true;
}
elseif ($selector === 'id' || $selector === 'name')
{
if ($locator === 'tags') {
$selector = 'id_or_name';
}
while (!$this->session->getPage()->has('named', array($selector, $locator))) {
sleep(1);
file_put_contents(public_path('screenshoot/element.jpg'), $this->session->getScreenshot());
}
$return = true;
}
return $return;
}
public function handlePages($selector, $locator, $action, $data = null)
{
$element = $this->elementFinder($selector, $locator);
switch ($action) {
case 'text':
$element->setValue($data);
break;
case 'file':
$element->attachFile($data);
break;
case 'select':
$element->selectOption($data);
break;
case 'click':
$element->click();
break;
case 'load':
break;
case 'upload':
while ($element->getAttribute('style') !== 'width: 100%;') {
sleep(1);
}
break;
default:
echo 'Action not found.';
exit;
}
return $element;
}
public function elementFinder($selector, $locator)
{
if ($this->isElementExist($selector, $locator)) {
if ($selector === 'css') {
return $this->session->getPage()->find('css', $locator);
} else {
if ($locator === 'tags') {
$selector = 'id_or_name';
}
return $this->session->getPage()->find('named', array($selector, $locator));
}
} else {
echo 'Element not Found.';
}
return null;
}
private function upload($simulator)
{
$this->handlePages('id', 'fileupload', 'file', '~/simulator/'. $simulator->name);
sleep(4);
file_put_contents(public_path('screenshoot/' . $simulator->name . '-simulator-file.jpg'), $this->session->getScreenshot());
$this->handlePages('id', 'sim_name', 'text', $simulator->name);
file_put_contents(public_path('screenshoot/' . $simulator->name . '-name-field.jpg'), $this->session->getScreenshot());
$this->handlePages('id', 'sim_category', 'select', $simulator->category);
$this->handlePages('id', 'sim_remark', 'text', $simulator->remark));
$this->handlePages('id', 'save', 'click');
}
}
我在网格模式下运行selenium服务器,集线器和节点通过内部网络(LAN)连接。这是我用来启动selenium服务器的命令(我将jar文件重命名为selenium-server.jar):
root@10.99.0.10:~/selenium2# java -jar selenium-server.jar -role hub -timeout 12000 -browserTimeout 12000
root@10.99.0.11:~/selenium2# java -jar selenium-server.jar -role node -host 10.99.0.11 -hub http://10.99.0.10:4444/grid/register/ -timeout 12000 -browserTimeout 12000
我使用laravel 5.1,behat / mink,behat / mink-selenium2-driver和selenium standalone 2.48.2
这是我的问题,在第一次/原始会话运行时,selenium2意外启动了新会话。
Unexpectedly started another session while the original running
如何告诉selenium2服务器一次只启动/运行1个会话?
谢谢。