JS Package Manager

JS Package Manager and Code Generators

🕹ī¸ SeleniumBase lets you load JavaScript packages from any CDN link into any website.

🎨 The following SeleniumBase solutions utilize this feature:

đŸ—ēī¸ Website Tours - 🎞ī¸ Presenter - 📊 Chart Maker - 🛂 Dialog Boxes

🕹ī¸ In addition to loading JS packages, SeleniumBase also lets you generate JS code from Python so that you can use these packages more easily.

đŸ—ēī¸ Here's an example of loading a website-tour library into the browser for a Google Maps tour:

đŸ—ēī¸ This example is from maps_introjs_tour.py. (The --interval=1 makes the tour go automatically to the next step after 1 second.)

cd examples/tour_examples
pytest maps_introjs_tour.py --interval=1

🕹ī¸ SeleniumBase includes powerful JS code generators for converting Python into JavaScript for using the supported JS packages. A few lines of Python in your tests might generate hundreds of lines of JavaScript.

đŸ—ēī¸ Here is some tour code in Python from maps_introjs_tour.py that expands into a lot of JavaScript.

self.add_tour_step("Welcome to Google Maps!", title="SeleniumBase Tours")
self.add_tour_step("Enter Location", "#searchboxinput", title="Search Box")
self.add_tour_step("See it", "#searchbox-searchbutton", alignment="bottom")
self.add_tour_step("Thanks for using Tours!", title="End of Guided Tour")

🕹ī¸ For existing features, SeleniumBase already takes care of loading all the necessary JS and CSS files into the web browser. To load other packages, here are a few useful methods that you should know about:


🕹ī¸ This example loads the IntroJS JavaScript library:

🕹ī¸ You can load any JS package this way as long as you know the URL.

🕹ī¸ If you're wondering how SeleniumBase does this, here's the full Python code from js_utils.py, which uses WebDriver's execute_script() method for making JS calls after escaping quotes with backslashes as needed:

def add_js_link(driver, js_link):
    script_to_add_js = (
        """function injectJS(link) {
              var body = document.getElementsByTagName("body")[0];
              var script = document.createElement("script");
              script.src = link;
              script.crossorigin = "anonymous";
              script.onload = function() { null };
    js_link = escape_quotes_if_needed(js_link)
    driver.execute_script(script_to_add_js % js_link)

🕹ī¸ Now that you've loaded JavaScript into the browser, you may also want to load some CSS to go along with it:


🕹ī¸ Here's code that loads the IntroJS CSS:


🕹ī¸ And here's the Python WebDriver code that makes this possible:

def add_css_link(driver, css_link):
    script_to_add_css = (
        """function injectCSS(css) {
              var head = document.getElementsByTagName("head")[0];
              var link = document.createElement("link");
              link.rel = "stylesheet";
              link.type = "text/css";
              link.href = css;
              link.crossorigin = "anonymous";
    css_link = escape_quotes_if_needed(css_link)
    driver.execute_script(script_to_add_css % css_link)
🕹ī¸ Website tours are just one of the many uses of the JS Package Manager.

🛂 The following example shows the JqueryConfirm package loaded into a website for creating fancy dialog boxes:


↕ī¸ (Example: dialog_box_tour.py) ↕ī¸


Here's how to run that example:

cd examples/dialog_boxes
pytest test_dialog_boxes.py

(Example from the Dialog Boxes ReadMe)

🕹ī¸ Since packages are loaded directly from a CDN link, you won't need other package managers like NPM, Bower, or Yarn to get the packages that you need into the websites that you want.

To learn more about SeleniumBase, check out the Docs Site:

SeleniumBase.io Docs

All the code is on GitHub:

SeleniumBase on GitHub

đŸ—ēī¸ If you're only interested in creating website tours with SeleniumBase, here's the link to the Website Tours ReadMe.

Back to top