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

selenium-engine-js

v3.2.1

Published

Selenium-like methods useful for automating SPAs (Single Page Application) where content is changed dynamically.

Downloads

31

Readme

GitHub release

SeleniumEngineJS

Selenium-like methods useful for automating SPAs (Single Page Application) where content is changed dynamically.

Introduction

This little script written in vanilla JS tries to emulate some of the Selenium most useful features, such as the Expected Conditions. It was originally written to be included into Tampermonkey/Greasemonkey scripts to bring the power of selenium into user scripts and be able to automate even Single Page Applications (where usually content and elements are loaded dynamically and URLs don't change, making automation really hard).

The script itself uses Promises but the real deal is the use of async/await in your code to chain Promises in a cleaner way so that async code looks as if it was syncronous (but IT IS NOT, don't get confused).

As of February 2022, async/await keywords are supported everywhere EXCEPT Internet Explorer, which does not even support Promises thus making this script completely useless (but i think almost nobody is still using it :smile:).

Even without async/await you could still chain the operations by using Promise.then(), but it is just another way of going down the good old callback hell :worried:.

How to use

  • Simply copy and paste the SeleniumEngine constant into your code and use :smile:

  • In your HTML file, by using a CDN:

    • JSDelivr

      <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/selenium-engine-js/src/SeleniumEngine.js"></script>
    • UnPkg

      <script type="text/javascript" src="https://unpkg.com/selenium-engine-js/src/SeleniumEngine.js"></script>
  • Include directly the file (so that updates will be reflected on your code):

    • Javascript (eval):
    fetch("https://raw.githubusercontent.com/LukeSavefrogs/SeleniumEngineJS/main/src/SeleniumEngine.js").then(data => data.text()).then(body => eval(body))
    • Javascript (import):
    (async () => {
    	// ...
    	await import("https://raw.githubusercontent.com/LukeSavefrogs/SeleniumEngineJS/main/src/SeleniumEngine.js")
    	// ...
    })()
    • NodeJS (CJS)
    const SeleniumEngine = require("selenium-engine-js");
    • NodeJS (ESModules):
    import SeleniumEngine from "selenium-engine-js";

Important

All the methods MUST be used inside an async function, as for the examples below (or using Promise.then(), which, again, is discouraged since it kinda destroys the whole concept behind this project).

Methods

SeleniumEngine.waitUntil(testCondition, timeout_ms, checkInterval_ms)

Used internally by SeleniumEngine.waitForElementPresent() and SeleniumEngine.waitForElementNotPresent(). Pauses the execution of the current function until the provided function testCondition is truthy (the function is executed every checkInterval_ms ms, default is 1000).

Throws an error if wait time exceeds timeout_ms (default is 30000).

(async () => {
    console.log("Operation 1");
    
	try {
		let test_switch = false;
		window.setTimeout(() => { test_switch = true}, 4500);


    	// Example 1 - Returns after the variable `test_switch` has become true
	    let resp = await SeleniumEngine.waitUntil(() => test_switch == true)
		console.log("Now we can continue with the example. We have waited for %d ms", resp.time)


    	// Example 2 - Throws an error like `Timeout of 30000ms exceeded (30016 real)`
	    await SeleniumEngine.waitUntil(() => false)
		console.log("This won't be executed")

	} catch (err) {
		console.log("As expected, the second example has returned an exception: %o", err)
	}
    
	
    // Example 3 - Will wait forever, because timeout is disabled and the expected condition is NEVER met
    console.warn("The next expression will wait forever, because timeout is disabled")
	await SeleniumEngine.waitUntil(() => false, 0);


    console.log("Operation 2");
})()

See it live

SeleniumEngine.waitForElementPresent(cssSelector, timeout_ms)

Pauses the execution of the current function until an element matching the provided CSS selector is found.

Throws an exception if it isn't found before timeout_ms ms

// 1. Here we create an element with ID=test and we append to the body after 5000ms
window.setTimeout(() => {
    const test_element = document.createElement("span");
    test_element.id = "test";
    
    document.body.appendChild(test_element);
}, 5000);


// 2. Here we wait until the element is present in the page
(async () => {
    console.log("Operation 1");
    
    // Pause function for 5 seconds, then continues
    await SeleniumEngine.waitForElementPresent("#test", 8000)

    console.log("Operation 2");
})()

SeleniumEngine.waitForElementNotPresent(cssSelector, timeout_ms)

Pauses the execution of the current function until an element matching the provided CSS selector is no longer found

Throws an exception if it isn't found before timeout_ms ms

// 1. Here we remove the element with ID=test after 5000ms
(() => {
    const test_element = document.createElement("span");
    test_element.id = "test";
    document.body.appendChild(test_element);

    // Then after 5 seconds we remove it
    window.setTimeout(() => {
        document.getElementById("test").remove()
    }, 5000);
})()


// 2. Here we wait until the element is not present anymore in the page
(async () => {
    console.log("Operation 1");
    
    // Pause function for 5 seconds, then continues
    await SeleniumEngine.waitForElementNotPresent("#test", 8000)

    console.log("Operation 2");
})()

SeleniumEngine.sleep(ms)

Pauses the execution of the current function for the number of milliseconds passed as parameter.

(async () => {
    console.log("Operation 1");
    
    // Pause function for 2 seconds
    await SeleniumEngine.sleep(2000);

    console.log("Operation 2");
})()

See it live