使用php webdriver从Selenium获取console.log
发布时间:2020-12-13 16:06:03 所属栏目:PHP教程 来源:网络整理
导读:我正在尝试对某些AMP页面运行集成测试,看看它们是否有效.如果将#development = 1附加到URL并将结果放在console.log中,AMP将运行验证.我需要能够读取console.log来检查这一点. 这是我到目前为止: $caps = DesiredCapabilities::firefox();$caps-setCapabilit
我正在尝试对某些AMP页面运行集成测试,看看它们是否有效.如果将#development = 1附加到URL并将结果放在console.log中,AMP将运行验证.我需要能够读取console.log来检查这一点.
这是我到目前为止: $caps = DesiredCapabilities::firefox(); $caps->setCapability('loggingPrefs',array('browser'=>'ALL')); //connect to selenium $webdriver = RemoteWebDriver::create('http://127.0.0.1:4444/wd/hub',$caps); $webdriver->get('https://www.example.com/amp/page.html#development=1'); sleep(10); $logs = $webdriver->manage()->getLog('browser'); var_dump($logs); 使用Facebook的webdriver for PHP.我可以恢复日志,但它似乎没有包含来自console.log的任何内容.我该如何捕获这些数据? 解决方法
据我所知,Facebook PHP WebDriver实现似乎没有实现任何LoggingPreferences“工具”.但是,因为PHP是我认为的弱类型,你可以通过调用来“欺骗”:
$chromeCapabilities->setCapability( 'loggingPrefs',['browser' => 'ALL'] ); 然后,打电话(说) var_dump( $chromeDriver->manage()->getLog( 'browser' ) ); 访问控制台日志. <?php require_once (__DIR__.'/../vendor/autoload.php'); use FacebookWebDriverRemoteDesiredCapabilities; use FacebookWebDriverRemoteRemoteWebDriver; use FacebookWebDriverChrome; $javaPath = '"C:Program Files (x86)Common FilesOracleJavajavapathjava.exe"'; $seleniumPath = '"'. __DIR__ . '..selenium-server-standalone-3.141.5.jar"'; $chromeDriverPath = 'C:pathtochromedriver.exe'; $site = 'http://mytestsite'; $seleniumPort = 4445; $useSelenium = true; $chromeDriverPathEnvVar = 'webdriver.chrome.driver'; putenv( $chromeDriverPathEnvVar .'='. $chromeDriverPath ); $chromeOptions = new ChromeChromeOptions(); $chromeOptions->addArguments( array( '--headless' ) ); $chromeCapabilities = DesiredCapabilities::chrome(); $chromeCapabilities->setCapability( ChromeChromeOptions::CAPABILITY,$chromeOptions ); $chromeCapabilities->setCapability( 'loggingPrefs',['browser' => 'ALL'] ); $selenium = null; if ($useSelenium) { $descriptorspec = array( 0 => array('pipe','r'),// stdin is a pipe that the child will read from 1 => array('file',__DIR__ . '/selenium_log-' . date('Ymd-His').'_'. $seleniumPort . '-stdout.txt','a'),// stdout is a pipe that the child will write to 2 => array('file',__DIR__ . '/selenium_log-' . date('Ymd-His').'_'. $seleniumPort . '-stderr.txt','a') // stderr is a file to write to ); $selenium_cmd = $javaPath .' -D'. $chromeDriverPathEnvVar .'="'. $chromeDriverPath .'" -jar '. $seleniumPath .' -port '. $seleniumPort; // If interested,add .' -debug'; $selenium = proc_open( $selenium_cmd,$descriptorspec,$pipes,null,array( 'bypass_shell' => true ) ); $host = 'http://localhost:'. $seleniumPort .'/wd/hub'; // this is the default $chromeDriver = RemoteWebDriver::create($host,$chromeCapabilities ); } else { $chromeDriver = FacebookWebDriverChromeChromeDriver::start( $chromeCapabilities ); } $chromeDriver->get( $site ); var_dump( $chromeDriver->manage()->getLog( 'browser' ) ); $chromeDriver->quit(); sleep(1); $chromeDriver->action(); sleep(1); $chromeDriver->close(); if ($useSelenium) { fclose( $pipes[0] ); proc_terminate( $selenium ); @pclose( $selenium ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |