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

web2driver

v3.0.5

Published

Pure-JS, browser-based WebDriver (+ Appium) client

Downloads

1,059

Readme

web2driver

A pure-JS webdriver client that runs in the browser (but also Node!). Since it runs in the browser it depends on XHR requests to speak to the automation server. Because of CORS, the automation server must send the appropriate access control headers for requests from web2driver to the automation server to work. Appium does this if you run it with the --allow-cors flag, but Selenium does not.

This means that, unless you put a proxy in front of Selenium, web2driver currently works only with Appium.

Install

On npm as the web2driver package, but bundled and exported for importing into browsers. Use webpack or some other tool to incorporate it into your app.

Usage

First, get your Appium server up and running on a host and port that you know. Then, use Web2Driver to initiate sessions on that server and do automation with those sessions.

import Web2Driver from 'web2driver';
// or if you want it with Node:
// import Web2Driver from 'web2driver/node';

async function automation() {
    const serverOpts = {hostname: "localhost", port: 4723};
    // we could also specify 'protocol' and 'path', which default to
    // 'http' and '/wd/hub' respectively

    const capabilities = {browserName: "Safari"}; //...

    // initialize a session
    const driver = await Web2Driver.remote(serverOpts, capabilities);

    // do stuff with a session
    const el = await driver.findElement('xpath', '//foo');
    await el.click();
    // ...

    // end the session
    await driver.quit();
}

Core API

TBD. Web2Driver uses the WebDriverIO's base protocol layer basically without change, so command names are the same as are listed in the various available protocols.

Additional Features

Explicit Waits

You can wait for an element up to a timeout in milliseconds:

const el = await driver.waitForElement(10000, 'accessibility id', 'foo');

There is also the ability to wait for multiple elements (which will retry until the list of elements is non-empty, and throw after the timeout if it's still empty):

const els = await driver.waitForElements(10000, 'class name', 'android.widget.EditText');

Direct Connect URLs

If your Selenium/Appium server decorates the new session capabilities response with the following keys:

  • directConnectProtocol
  • directConnectHost
  • directConnectPort
  • directConnectPath

Then web2driver will switch its endpoint to the one specified by the values of those keys. This is useful for load-balanced servers to be a single point of entry for starting sessions, but to reply with more detailed information that allows web2driver to speak directly to the host running the session.

Attach to Session

If you have an existing session ID corresponding to a session running on a host, you can attach to it:

import Web2Driver from 'web2driver';

async function automation() {
    const serverOpts = {hostname: "localhost", port: 4723}; // same opts as in the basic example
    const sessionId = "1234567890134"; // we would get this externally somehow

    // attach to a session
    const driver = await Web2Driver.attachToSession(sessionId, serverOpts);

    // now we can do stuff with the session

    // we could also pass in whether or not the session is or isn't W3C (default is true):
    const driver = await Web2Driver.attachToSession(sessionId, serverOpts, false);

    // we could also pass in capabilities to store them on the session
    const driver = await Web2Driver.attachToSession(sessionId, serverOpts, false, {browserName: 'foo'});
}

Other Webdriver Params

Since web2driver is based on WebdriverIO's core library, many WebdriverIO options also work with web2driver (for example, you could set connectionRetryCount to 0 to disable connection retries):

import Web2Driver from 'web2driver';

async function automation() {
    const serverOpts = {hostname: "localhost", port: 4723, connectionRetryCount: 0};
    const capabilities = {browserName: "Safari"}; //...

    // initialize a session
    const driver = await Web2Driver.remote(serverOpts, capabilities);
}

Dev / Test

npm run build # use webpack to build the JS
npm run build-test # build the test bundle
npm run test-server # build the test bundle and host it using webpack-dev-server
npm run test-open # open up the test bundle in a browser, causing the tests to launch

At the moment, tests assume an Appium server running on port 4723, with iOS support (so running on a Mac).

Credits

web2driver is maintained by HeadSpin. HeadSpin is the collaborative platform for real edge debugging and testing.