php - 使用 php webdriver 从 Selenium 获取 console.log

标签 php selenium webdriver

我正在尝试对我们的一些 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 的 PHP 网络驱动程序。我可以取回日志,但它似乎不包含来自 console.log 的任何内容。我如何捕获这些数据?

最佳答案

据我所知,Facebook PHP WebDriver 实现似乎没有实现任何 LoggingPreferences“工具”。然而,因为 PHP 是所谓的弱类型,您可以通过调用以下内容来“作弊”:

$chromeCapabilities->setCapability( 'loggingPrefs', ['browser' => 'ALL'] );

然后,调用(说)

var_dump( $chromeDriver->manage()->getLog( 'browser' ) );

访问控制台日志。 以下是经过大约 30 小时的调查后我用来使它正常工作的工作示例 - 我希望它对某人有所帮助!它可以使用或不使用 Selenium,如果不使用,则直接调用 ChromeDriver。测试站点包含一些 JavaScript 以显式写入 console.log:

<?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, 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 - 使用 php webdriver 从 Selenium 获取 console.log,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40538392/

相关文章:

python - 使用 Selenium 时 Cookie 不起作用

java - 如何通过 Selenium 和 Java 单击“停用”按钮,然后单击“激活”按钮

javascript - Sauce Labs 无法执行 javascript

javascript - 上传图片 python selenium javascript

php - 亚马逊产品广告 API : Get Product Detail URL from ASIN

php - 如何在数据库中管理国家

php - REMI 存储库 RPM 已验证或测试?

javascript - 通过使用 AJAX 或 Cookie 获取 mysql 值来执行/验证 javascript 中的值的最有效方法

python - XPath : is it possible? (Python) 中的累加器

python - 使 selenium 更快地运行网站列表