承接 timatanga/headless 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

timatanga/headless

Composer 安装命令:

composer require timatanga/headless

包简介

The timatanga Headless package. Browser automation with chrome/chromium in headless mode

README 文档

README

This package provides a fluent interface for browser automation. It is heavily influenced by Laravel Dusk so all credits go to Taylor Otwell and his amazing team for there incredible work and inspiration! Instead of installing JDK or Selenium, this package requires and uses a standalone ChromeDriver.

Installation

To get started, you should install Google Chrome on your system. Google Chrome will be managed in headless mode via this package through the ChromeDriver.

Please make use of composer to install this package

composer require dbisapps/headless

Instead of relying on Laravel's artisan command line interface to install the ChromeDriver this package supports a static method to get the driver installed within the package directory:

use dbizapps\Headless\Browser;

$result = Browser::install();

The required ChromeDriver version is detected based on the major version of the installed Chrome/Chromium browser on your machine.

Browser Navigation

The Headless packages provides an easy to use, fluent interface to concat browser operations. Navigation operations all return the browser instance itself.

A good starting point is to visit a page by:

use dbizapps\Headless\Browser;

$browser = new Browser;

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

Other available navigation operations are:

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

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

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

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

Browser Sizing & Moving

Even in headless mode with non-visible operations, it is sometimes useful for further operations to adjust the browser size

// 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);

Page

Just visiting a page isn't that useful. A useful browser automation needs to interact with page elements.

Interacting with page form elements is enabled after visiting a page

// 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);

Dialog interactions are supported as well

// 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);

Execute Scripts

If you need to extend existing scripts with custom javascripts just use:

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

Assertions

For automated browser interactions you may want to assert that a specific content is on the page.

// 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); 

... and many more. Please refer to the SupportAssertions Trait for details.

Get Messages, Content or Outputs

Headless browser operations require feedback to be useful at any kind. From computed messages, console logs to page source or page caputures are numerous browser operations available:

// 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)

timatanga/headless 适用场景与选型建议

timatanga/headless 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「webdriver」 「headless」 「chromium」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 timatanga/headless 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 timatanga/headless 我们能提供哪些服务?
定制开发 / 二次开发

基于 timatanga/headless 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-29