jrobchin / casperjs-php

phpcasperjs/phpcasperjs 的扩展,包括额外功能

dev-master / 1.0.x-dev 2017-06-07 20:34 UTC

This package is not auto-updated.

Last update: 2024-09-20 21:29:54 UTC


README

casperjs-php 是 CasperJS 库的简单 PHP 封装,旨在自动化针对网页的用户测试。它是 alwex/php-casperjs 的扩展,包括额外功能,使您更容易添加自己的代码段。

它很容易集成到 PHPUnit 测试用例中。

制作网络爬虫从未如此简单!

安装

在使用 casperjs-php 之前,您需要安装这两个库

1 - PhantomJS http://phantomjs.org/download.html

2 - CasperJS http://casperjs.org/installation.html

使用 composer require jrobchin/casperjs-php

用法

<?php

use jrobchin\casperjsphp\Casper;

$casper = new Casper('/path/to/capserjs/bin/dir/');

// forward options to phantomJS
// for exemple to ignore ssl errors
$casper->setOptions(array(
    'ignore-ssl-errors' => 'yes'
));

// Setup User Agent
$casper->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36');

// Create the casper javascript object
// use '$skip_urls' to pass an array of urls to block requests
// parameters: $skip_urls, $loadImages='true', $javascriptEnabled='true', $loadPlugins='true'
$skip_urls = ['www.youtube.com', 'www.googleadservices.com'];
$casper->create($skip_urls, $loadImages='false');

// navigate to google web page
$casper->start('http://www.google.com');

// Set the page viewport
$casper->setViewPort(1280, 1024);

// fill the search form and submit it
$casper->fillForm(
        'form[action="/search"]', [
            'q' => 'search'
        ], true);

// wait for 5 seconds
$casper->wait(5000);

// wait for text if needed for 3 seconds
$casper->waitForText('Yahoo', 3000);

// or wait for selector
$casper->waitForSelector('.gbqfb', 3000);

// make a screenshot of the google logo
$casper->captureSelector('/tmp/logo.png', '#hplogo');

// or take a screenshot of a custom area
$casper->capture('/tmp/custom-capture.png', [
        'top' => 0,
        'left' => 0,
        'width' => 800,
        'height' => 600
]);

// click the first result
$casper->click('h3.r a');

// switch to the first iframe
$casper->switchToChildFrame(0);

// make some stuff inside the iframe
$casper->fillForm('#myForm', array(
    'search' => 'my search',
));

// get back to parent
$casper->switchToParentFrame();

// run the casper script
$casper->run();

// check the urls casper get throught
var_dump($casper->getRequestedUrls());

// need to debug? just check the casper output
var_dump($casper->getOutput());

您也可以创建自己的代码片段来使用

$step = <<<FRAGMENT
casper.then(function () {
    this.echo(this.fetchText('h3'));
});
FRAGMENT;

$casper->addStep($step);

或者您可以使用 then 函数

$thenCode = <<<FRAGMENT;
    this.echo(this.fetchText('h3'));
FRAGMENT;

$casper->then($thenCode);

想要存储数据,以便在运行后访问?

$pageCountVarName = '[PAGE_COUNT]';
$casper->setCustomVar($pageCountVarName);

$step = <<<FRAGMENT
var pageCount = this.evaluate(function () {
    return document.getElementById('page_count').innerHTML;
});
this.echo("$pageCountVarName" + pageCount);
FRAGMENT;

$casper->then($step);
$casper->run();

$pageCount = $casper->getCustomVar($pageCountVarName);