|
| 1 | +<p><h3 align="center"><a href="https://github.com/seleniumbase/SeleniumBase"><img src="https://cdn2.hubspot.net/hubfs/100006/images/super_logo_sb23.png" alt="SeleniumBase" width="220" /></a></h3></p> |
| 2 | + |
| 3 | +## JS Package Manager |
| 4 | + |
| 5 | +<p>The SeleniumBase JS Package Manager lets you load any JavaScript library into any website from automation scripts.</p> |
| 6 | +<img src="https://seleniumbase.io/img/sb_icon.png" title="SeleniumBase" width="30" /> Here's an example of website-tour libraries loaded into a browser session while visiting Google: |
| 7 | + |
| 8 | +<img src="https://cdn2.hubspot.net/hubfs/100006/google_tour_3.gif" title="SeleniumBase Tour of Google"><br /> |
| 9 | + |
| 10 | +This example from [google_tour.py](https://github.com/seleniumbase/SeleniumBase/blob/master/examples/tour_examples/google_tour.py) can be run with <b>pytest</b> from the SeleniumBase ``examples/tour_examples`` folder with the following command after you've cloned and installed [SeleniumBase from GitHub](https://github.com/seleniumbase/SeleniumBase): |
| 11 | + |
| 12 | +```bash |
| 13 | +pytest google_tour.py |
| 14 | +``` |
| 15 | + |
| 16 | +<div>Website tours are just one way of demonstrating the abilities of the SeleniumBase JS Package Manager.</div> |
| 17 | +<div>Here's the SeleniumBase code for loading any JS package into any website from your web browser:</div> |
| 18 | + |
| 19 | +```python |
| 20 | +self.add_js_link(js_link) |
| 21 | +``` |
| 22 | + |
| 23 | +Here's code that loads the <a href="https://introjs.com/">IntroJS</a> JavaScript library: |
| 24 | + |
| 25 | +```python |
| 26 | +self.add_js_link("https://cdnjs.cloudflare.com/ajax/libs/intro.js/2.9.3/intro.min.js") |
| 27 | +``` |
| 28 | + |
| 29 | +<div>You can load any JS library into a web browser this way as long as you know the URL to it!</div> |
| 30 | + |
| 31 | +If you're wondering how SeleniumBase does this, here's a sneak peak at the code, which uses WebDriver's ``execute_script()`` method to run JavaScript commands: |
| 32 | + |
| 33 | +```python |
| 34 | +def add_js_link(driver, js_link): |
| 35 | + script_to_add_js = ( |
| 36 | + """function injectJS(link) { |
| 37 | + var body = document.getElementsByTagName("body")[0]; |
| 38 | + var script = document.createElement("script"); |
| 39 | + script.src = link; |
| 40 | + script.defer; |
| 41 | + script.type="text/javascript"; |
| 42 | + script.crossorigin = "anonymous"; |
| 43 | + script.onload = function() { null }; |
| 44 | + body.appendChild(script); |
| 45 | + } |
| 46 | + injectJS("%s");""") |
| 47 | + js_link = escape_quotes_if_needed(js_link) |
| 48 | + driver.execute_script(script_to_add_js % js_link) |
| 49 | +``` |
| 50 | + |
| 51 | +<p>Now that you've loaded the JavaScript into the browser, you may also want to load some CSS to go along with it:</p> |
| 52 | + |
| 53 | +```python |
| 54 | +self.add_css_link(css_link) |
| 55 | +``` |
| 56 | + |
| 57 | +<p>Here's code that loads the <a href="https://introjs.com/">IntroJS</a> CSS:</p> |
| 58 | + |
| 59 | +```python |
| 60 | +self.add_css_link("https://cdnjs.cloudflare.com/ajax/libs/intro.js/2.9.3/introjs.css") |
| 61 | +``` |
| 62 | + |
| 63 | +<p>And here's the Python WebDriver code that makes this possible:</p> |
| 64 | + |
| 65 | +```python |
| 66 | +def add_css_link(driver, css_link): |
| 67 | + script_to_add_css = ( |
| 68 | + """function injectCSS(css) { |
| 69 | + var head = document.getElementsByTagName("head")[0]; |
| 70 | + var link = document.createElement("link"); |
| 71 | + link.rel = "stylesheet"; |
| 72 | + link.type = "text/css"; |
| 73 | + link.href = css; |
| 74 | + link.crossorigin = "anonymous"; |
| 75 | + head.appendChild(link); |
| 76 | + } |
| 77 | + injectCSS("%s");""") |
| 78 | + css_link = escape_quotes_if_needed(css_link) |
| 79 | + driver.execute_script(script_to_add_css % css_link) |
| 80 | +``` |
| 81 | + |
| 82 | +-------- |
| 83 | + |
| 84 | +<div>To learn more about SeleniumBase, check out the Docs Site:</div> |
| 85 | +<a href="https://seleniumbase.io"> |
| 86 | +<img src="https://img.shields.io/badge/docs-%20%20SeleniumBase.io-11BBDD.svg" alt="SeleniumBase.io Docs" /></a> |
| 87 | + |
| 88 | +<div>All the code is on GitHub:</div> |
| 89 | +<a href="https://github.com/seleniumbase/SeleniumBase"> |
| 90 | +<img src="https://img.shields.io/badge/✅%20💛%20View%20Code-on%20GitHub%20🌎%20🚀-02A79E.svg" alt="SeleniumBase on GitHub" /></a> |
| 91 | + |
| 92 | +And if you're just interested in creating website tours with SeleniumBase, here's the link to the <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/tour_examples/ReadMe.md">Website Tours ReadMe on GitHub</a>. |
0 commit comments