timatanga/headless

timatanga Headless 包。使用 Chrome/Chromium 在无头模式下的浏览器自动化

v0.1.0 2021-09-29 13:22 UTC

This package is auto-updated.

Last update: 2024-09-29 06:08:41 UTC


README

此包提供了一个流畅的接口用于浏览器自动化。它深受 Laravel Dusk 影响,因此所有荣誉归 Taylor Otwell 及其出色的团队,感谢他们杰出的工作和灵感!无需安装 JDK 或 Selenium,此包需要并使用独立的 ChromeDriver。

安装

要开始,您应该在系统上安装 Google Chrome。通过此包,Google Chrome 将通过 ChromeDriver 以无头模式管理。

请使用 composer 安装此包

composer require dbisapps/headless

此包支持使用静态方法在包目录内安装 ChromeDriver,而不是依赖于 Laravel 的 artisan 命令行界面来安装 ChromeDriver

use dbizapps\Headless\Browser;

$result = Browser::install();

所需的 ChromeDriver 版本将基于您机器上已安装的 Chrome/Chromium 浏览器的主版本进行检测。

浏览器导航

Headless 包提供了一个易于使用的、流畅的接口来组合浏览器操作。所有导航操作都返回浏览器实例本身。

一个很好的起点是通过以下方式访问页面:

use dbizapps\Headless\Browser;

$browser = new Browser;

$browser->visit('laravel.com');

其他可用的导航操作包括

// browse to the blank page.
$browser->blank();

// refresh the page
$browser->refresh();

// navigate back
$browser->back();

// navigate back
$browser->forward();

浏览器大小和移动

即使在无头模式下,有时调整浏览器大小对于后续操作也是有用的

// maximize browser window
$browser->maximize();

// resize browser window
$browser->resize($width, $height);

// make the browser window as large as the content.
$browser->fitContent();

// Move the browser window.
$browser->move($x, $y);

页面

仅仅访问页面并不那么有用。有用的浏览器自动化需要与页面元素交互。

访问页面后,可以启用与页面表单元素的交互

// Click on link with the given selector, e.g. #submit
$browser->click($selector);

// Press on button with the given selector
$browser->press($selector);

// Get (just passing the selector) or set value (passing selector and value) of element with the given selector. 
$browser->value($selector, $value);

// Get text of element with the given selector
$browser->text($selector);

// Clear selected element and type value
$browser->type($selector, $value);

// Append value to element with given selector
$browser->append($selector, $value);

// Clear the given field
$browser->clear($selector);

// Select the given value of element with given selector
$browser->select($selector, $value);

// Select radio option
$browser->radio($selector, $value);

// Check the given checkbox.
$browser->check($selector, $value);

// Uncheck the given checkbox.
$browser->uncheck($selector, $value);

也支持对话框交互

// Accept a JavaScript dialog.
$browser->acceptDialog();

// DismissDialog a JavaScript dialog.
$browser->dismissDialog();

// Type the given value in an open JavaScript prompt dialog.
$browser->typeInDialog($value);

执行脚本

如果您需要通过自定义 JavaScript 扩展现有脚本,只需使用

// Execute JavaScript within the browser.
$browser->script($script);

断言

对于自动化的浏览器交互,您可能想要断言页面上存在特定的内容。

// Assert that the page title matches the given text.
$browser->assertTitle($title);

// Assert that the page title contains the given text.
$browser->assertTitleContains($text);

// Assert that the given text is present on the page.
$browser->assertSee($text);

// Assert that the given text is not present on the page.
$browser->assertDontSee($text);

// Assert that the given text is present within the selector.
$browser->assertSeeIn($selector, $value);

// Assert that the given text is not present within the selector.
$browser->assertDontSeeIn($selector, $value);

// Assert that the given link is present on the page.
$browser->assertSeeLink($link);

// Assert that the given link is not present on the page.
$browser->assertDontSeeLink($link);

// Assert that the given input field does not have the given value.
$browser->assertInputValue($field, $value);

// Assert that the given input field does not have the given value.
$browser->assertInputValueIsNot($field, $value);

// Assert that the given checkbox is checked.
$browser->assertChecked($field, $value);

// Assert that the given checkbox is not checked.
$browser->assertNotChecked($field, $value);

// Assert that the given radio field is selected.
$browser->assertRadioSelected($field, $value);

// Assert that the given radio field is not selected.
$browser->assertRadioNotSelected($field, $value);

// Assert that the given dropdown has the given value selected.
$browser->assertSelected($field, $value);    

// Assert that the given dropdown does not have the given value selected.
$browser->assertNotSelected($field, $value);   

// Assert that the given array of values are available to be selected.
$browser->assertSelectHasOptions($field, $values); 

// Assert that the given array of values are not available to be selected.
$browser->assertSelectMissingOptions($field, $values); 

...还有更多。请参阅 SupportAssertions 特性以获取详细信息。

获取消息、内容或输出

无头浏览器操作需要反馈在任何情况下都是有用的。从计算消息、控制台日志到页面源或页面截图,有许多浏览器操作可用

// Preserve computed or returned messages
$arr = $browser->getMessages()

// set html source into browser
$page = $browser->setHtml($html)  

// returns html page dump 
$page = $browser->getPage()

// dump html page to disk
$path = $browser->savePage($filename)

// takes a screen capture, stores as image
$path = $browser->screenshot($filename)

// takes a screen (visible screen) capture, stores as image
$path = $browser->screenshot($filename)

// takes a page (viewport) capture, stores as image
$path = $browser->pageshot($filename)

// dumps the console log to disk
$path = $browser->consoleLog($filename)