dptrmt4366 2015-12-04 15:29
浏览 43

PHP Behat / mink + selenium2 webdriver意外启动新的实例/会话

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.

Original session starting

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?