npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phantomjs-adapter

v0.0.6

Published

An ES.next promise adapter for PhantomJS for use with ES.next async/await

Downloads

25

Readme

phantomjs-adapter

Build Status

An ES.next promise adapter for PhantomJS for use with ES.next async/await.

Installing

phantomjs-adapter is available as an npm package.

Usage

import PhantomJS from 'phantomjs-adapter';

async function run() {
  const browser = new PhantomJS();
  await browser.open('https://www.google.com');

  const searchBox = await browser.find('input[name="q"]');
  await searchBox.fillIn('Hello World!')

  const searchButton = await browser.find('input[value="Search"]');
  await searchButton.click();

  const body = await browser.find('body');
  console.log(body.textContent);
}
run();

API Documentation

PhantomJS

An Class that mediates communication with a PhantomJS browser instance.

Logs
import PhantomJS from 'phantomjs'

const browser = new PhantomJS();
browser.logs.forEach((log) => {
  console.log(log);
});

An AwaitableObservable that publishes console messages and details about network requests.

Constructor
import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
}
run();

Spawns a PhantomJS process and instruments it to be controlled by method calls on this object.

Exit
import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.exit();
}
run();

Exits the PhantomJS process. Note that that process may not have fully exited by the time the promise is resolved; it is at least guaranteed to exit soon after.

Open
import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');
  await browser.exit();
}
run();

Instructs the PhantomJS process to open a URL using WebPage.open.

Evaluate
import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');
  await browser.evaluate('return document.title');
  await browser.exit();
}
run();

Instructs the PhantomJS process to evaluate a JavaScript function using WebPage#evaluate, given as a string, within the context of the currently open page. It is assumed that PhantomJS#open has been called and its returned promise resolved. The promise returned by PhantomJS#evaluate will be resolved with the return value of the given function body. It is assumed that the return value can be serialized using JSON.stringify.

SendEvent
import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');
  await browser.sendEvent('click', 20, 20);
  await browser.exit();
}
run();

Instructs the PhantomJS process to provide user input to the currently open page using 'WebPage#sendEvent'. It is assumed that PhantomJS#open has been called and its returned promise resolved.

Find
import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const title = await browser.find('h1.jumbotron-title');
  console.log(title.textContent);

  const signUpButton = await browser.find('a', {text: 'Sign up'});
  await signUpButton.click();

  await browser.find('h1', {text: 'Join GitHub', wait: 2000});

  await browser.exit();
}
run();

Finds an element on the currently open page given a CSS selector and optional text content substring. The promise is resolved with an instance of Element if the element is found and null otherwise. The CSS selector and text content substring are converted into XPath and evaluated using document.evaluate. It is assumed that PhantomJS#open has been called and its returned promise resolved.

An optional number of milliseconds to wait can be provided. The XPath selector will be evaluated every 100 milliseconds upto the provided wait time. If an element is found, it is returned immediately. Otherwise, null is returned after the wait time.

Element

A class that represents a snapshot of an element rendered in the currently open page of a PhantomJS browser. It exposes data about the element and provides an interface for sending user actions to that element. Element instances are returned by PhantomJS#find.

Attributes

import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const title = await browser.find('h1.jumbotron-title');
  console.log(title.attributes.class);

  await browser.exit();
}
run();

An object representing the HTML attributes of the element.

BoundingClientRect

import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const title = await browser.find('h1.jumbotron-title');
  console.log(title.boundingClientRect);

  await browser.exit();
}
run();

An object containing the size and position of the element. These values are read using getBoundingClientRect().

TextContent

import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const title = await browser.find('h1.jumbotron-title');
  console.log(title.textContent);

  await browser.exit();
}
run();

The inner text of the element. It is read from the textContent attribute of the underlying element.

InnerHTML

import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const body = await browser.find('body');
  console.log(body.innerHTML);

  await browser.exit();
}
run();

The inner text of the element. It is read from the innerHTML attribute of the underlying element.

Value

import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const title = await browser.find('h1.jumbotron-title');
  console.log(title.value);

  await browser.exit();
}
run();

The value of the element if it is an input element. It is read from the value attribute of the underlying element. If the element is not an input, value is undefined.

Click

import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const signUpButton = await browser.find('a', {text: 'Sign up'});
  await signUpButton.click();

  await browser.exit();
}
run();

Instructs the PhantomJS process to click on the center of the element, at the time PhantomJS#find was executed, using PhantomJS#sendEvent.

FillIn

import PhantomJS from 'phantomjs'

async function run() {
  const browser = new PhantomJS();
  await browser.open('http://github.com');

  const searchInput = await browser.find('input[name="q"]');
  await searchInput.fillIn('awesome code');

  await browser.exit();
}
run();

Instructs the PhantomJS process to focus an element assumed to be a text input using Element#click and then send keypress events via PhantomJS#sendEvent.

Extension

import PhantomJS, {Element as BaseElement} from 'phantomjs-adapter';

export class Element extends BaseElement {
}

export default class extends PhantomJS {
  static Element = Element;
}

phantomjs-adapter exposes a class-based interface and can be extended by subclassing PhantomJS and Element. Note that PhantomJS#find returns an instance of PhantomJS.Element; so, to override Element, it must be assigned to an Element static attribute of a subclass of PhantomJS, as shown above.

Development

Getting Started

The application requires the following external dependencies:

  • Node.js

The rest of the dependencies are handled through:

npm install

Run tests with:

npm test