/** * Get phantomjs port. * * @TODO: extract by settings! * * @return int * Port. */publicstaticfunctiongetPort(){return9000;}/** * @BeforeFeature */publicstaticfunctionensurePhantomjs(){if(!self::isPhantomjsActive()){self::startPhantomjs();}}/** * @AfterScenario @javascript */publicfunctionrestartPhantomjs(){//Stopdriver.$driver=$this->getSession()->getDriver();$driver->stop();//Restartphantomjs.self::stopPhantomjs();self::startPhantomjs();sleep(1);//Startdriveragain.$driver->setWebdriver(newWebDriver('http://127.0.0.1:9000/wd/hub'));$driver->start();}/** * Start phantomjs */publicstaticfunctionstartPhantomjs(){$port=self::getPort();$command='phantomjs2 --webdriver='.$port.' --load-images=false --ignore-ssl-errors=true > /dev/null 2>/dev/null &';exec($command);}/** * @AfterSuite * * We need register shutdown function to stop phantomjs * because other processes will require it after this hook implementation. */phantomjs-behatpublicstaticfunctionregisterPhantomjsStopAfterTests(){register_shutdown_function(array('FeatureContext','stopPhantomjs'));}/** * Kill phantomjs if it was launched. */publicstaticfunctionstopPhantomjs(){$pid=self::getPhantomjsPid();if($pid){exec('kill '.$pid);}}/** * Get phantomjs pid. * * We use ps aux and grep to extract the process id. * * @return int * Phantomjs pid. */publicstaticfunctiongetPhantomjsPid(){$port=self::getPort();$command="ps aux | grep 'phantomjs2 --webdriver=$port' | grep -v grep | awk '{ print $2 }' | head -1";exec($command,$out);return!empty($out[0])?$out[0]:NULL;}/** * Check phantomjs is active. * * @return bool * Phantomjs is active. */publicstaticfunctionisPhantomjsActive(){$pid=self::getPhantomjsPid();return!empty($pid);}
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.