I'm trying to help a friend automate some process using behat/mink
+ selenium2
. It's working but somehow in the middle of the process, selenium2
starts new instance/session running in parallel from the original.
Is there a way to prevent this behavior and keep only one instance/session running?
This is my code (laravel controller class):
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');
}
}
I'm running selenium server in grid mode, hub and node connected via internal network (LAN). This is the command i use to start the selenium server (i renamed the jar file to 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
I'm using laravel 5.1, behat/mink, behat/mink-selenium2-driver and selenium standalone 2.48.2
and this is my problem where the selenium2 unexpectedly start new session while first/original session running.
Unexpectedly started another session while the original running
How to tell the selenium2 server to only start/run 1 session at a time?
Thank you.