使用php webdriver从Selenium获取console.log

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用php webdriver从Selenium获取console.log脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试对某些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' ) );

访问控制台日志.
以下是我在大约30个小时的调查后使用的工作样本 – 我希望它可以帮到某人!它可以使用或不使用Selenium,如果没有,则直接调用ChromeDriver.测试站点包含一些用于显式写入console.log的JavaScript:

<?PHP

require_once (__DIR__.'/../vendor/autoload.PHP');

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Chrome;

$javaPath = '"C:\\Program Files (x86)\\Common Files\\oracle\\Java\\javapath\\java.exe"';
$seleniumPath = '"'. __DIR__ . '\\..\\selenium-server-standalone-3.141.5.jar"';

$chromeDriverPath = 'C:\path\to\chromedriver.exe';
$site = 'http://mytestsite';

$seleniumPort = 4445;
$useSelenium = true;
$chromeDriverPathenvVar = 'webdriver.chrome.driver';

putenv( $chromeDriverPathEnvVar .'='. $chromeDriverPath );

$chromeOptions = new Chrome\ChromeOptions();
$chromeOptions->addarguments( array( '--headless' ) );

$chromeCapabilities = DesiredCapabilities::chrome();
$chromeCapabilities->setCapability( Chrome\ChromeOptions::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 = Facebook\WebDriver\Chrome\ChromeDriver::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 );
}

脚本宝典总结

以上是脚本宝典为你收集整理的使用php webdriver从Selenium获取console.log全部内容,希望文章能够帮你解决使用php webdriver从Selenium获取console.log所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。