Best Python code snippet using SeleniumBase
avlslideshow.py
Source:avlslideshow.py
...36 def display(self):37 print(util.util.display(self, True))38 def add_root(self, key, style=""):39 self.root = AVL_Slideshow.AVL_Slideshow_Node(key, self, style=style)40 def add_slide(self, title="", pause=1):41 def copy_children(original, copy):42 if original.l:43 copy.add_node(original.l.key, "l", original.l.style)44 copy_children(original.l, copy.l)45 if original.r:46 copy.add_node(original.r.key, "r", original.r.style)47 copy_children(original.r, copy.r)48 tree = AVL_Slideshow()49 if self.root:50 tree.add_root(self.root.key, style=self.root.style)51 copy_children(self.root, tree.root)52 self.slides.enqueue({"tree": tree, "title": title, "pause": pause})53 def get_slides(self):54 while len(self.slides) > 0:55 yield self.slides.dequeue()56 def add_slideshow(self, key):57 title = f"Add -> {key}\n"58 self.add_slide(title, 2)59 def process_node(node, side):60 node.style = style.BOLD + style.CYAN61 child = None62 if side == "l":63 self.add_slide(title + f"{key} < {node.key}")64 child = node.l65 if side == "r":66 self.add_slide(title + f"{key} > {node.key}")67 child = node.r68 new_node, return_case, pause = rec_add(child, key)69 node.attach_node(new_node, side)70 if return_case == "balanced_link":71 new_node.style = style.BOLD + style.GREEN72 self.add_slide(title + f"Balanced node {new_node.key}", 2)73 new_node.style = ""74 if return_case == "link":75 new_node.style = style.BOLD + style.GREEN76 self.add_slide(77 title + f"Node {new_node.key} is already balanced", pause78 )79 new_node.style = ""80 node.style = ""81 def found_leaf(node, side):82 node.style = style.BOLD + style.GREEN83 self.add_slide(title + f"Found leaf {node.key}")84 node.add_node(key, side)85 if side == "l":86 node.l.style = style.BOLD + style.PURPLE87 self.add_slide(title + f"Added node {key}")88 node.l.style = ""89 if side == "r":90 node.r.style = style.BOLD + style.PURPLE91 self.add_slide(title + f"Added node {key}")92 node.r.style = ""93 node.style = ""94 def rec_add(node, key):95 return_case = "link"96 if key == node.key:97 raise Exception("Duplicates not allowed in binary search tree")98 if key < node.key:99 if node.l:100 process_node(node, "l")101 else:102 found_leaf(node, "l")103 if key > node.key:104 if node.r:105 process_node(node, "r")106 else:107 found_leaf(node, "r")108 bal_fac = node.get_balance_factor()109 pause = 1110 if bal_fac < -1 or bal_fac > 1:111 node.style = style.BOLD + style.RED112 self.add_slide(title + f"Balancing node {node.key}", 2)113 node.style = ""114 return_case = "balanced_link"115 pause = 2116 return node.balance(), return_case, pause117 if not self.root:118 self.add_root(key, self)119 else:120 new_node, return_case, pause = rec_add(self.root, key)121 if return_case == "found":122 new_node.style = style.BOLD + style.GREEN123 self.add_slide(title + f"Found leaf {new_node.key}", 2)124 new_node.style = ""125 if return_case == "balanced_link":126 new_node.style = style.BOLD + style.GREEN127 self.add_slide(title + f"Balanced node {new_node.key}", 2)128 new_node.style = ""129 if return_case == "link":130 new_node.style = style.BOLD + style.GREEN131 self.add_slide(132 title + f"Node {new_node.key} is already balanced", pause133 )134 new_node.style = ""135 self.root = new_node136 self.add_slide("Finish\n", 2)137 def delete_slideshow(self, key):138 title = f"Delete -> {key}\n"139 self.add_slide(title, 2)140 def process_node(node, side):141 node.style = style.BOLD + style.CYAN142 pause = 1143 return_case = None144 del_node = None145 if side == "l":146 self.add_slide(title + f"{key} < {node.key}")147 (del_node, return_case), pause = rec_del(node.l)148 if side == "r":149 self.add_slide(title + f"{key} > {node.key}")150 (del_node, return_case), pause = rec_del(node.r)151 node.attach_node(del_node, side)152 if return_case == "delete_leaf":153 self.add_slide(title + f"Deleted {key}", 2)154 if return_case in ["one_child", "one_successor", "successor"]:155 del_node.style = style.BOLD + style.GREEN156 self.add_slide(title + f"Replaced {key} with {del_node.key}", 2)157 del_node.style = ""158 if return_case == "balanced_link":159 del_node.style = style.BOLD + style.GREEN160 self.add_slide(title + f"Balanced node {del_node.key}", 2)161 del_node.style = ""162 if return_case == "link":163 del_node.style = style.BOLD + style.GREEN164 self.add_slide(165 title + f"Node {del_node.key} is already balanced", pause166 )167 del_node.style = ""168 node.style = ""169 def rec_del(node):170 if key == node.key:171 node.style = style.BOLD + style.RED172 self.add_slide(title + f"{key} found", 2)173 return get_replacement(node), 2174 elif node.l and key < node.key:175 process_node(node, "l")176 elif node.r and key > node.key:177 process_node(node, "r")178 else:179 raise Exception("Attempted to delete missing key from tree:", key)180 bal_fac = node.get_balance_factor()181 if bal_fac < -1 or bal_fac > 1:182 node.style = style.BOLD + style.RED183 self.add_slide(title + f"Balancing node {node.key}", 2)184 node.style = ""185 return (node.balance(), "balanced_link"), 2186 else:187 return (node.balance(), "link"), 1188 def get_replacement(node):189 # Determine which children are present190 kids = ""191 if node.l:192 kids += "l"193 if node.r:194 kids += "r"195 # No children196 if kids == "":197 return None, "delete_leaf"198 # One child199 if kids == "l":200 return node.l, "one_child"201 if kids == "r":202 return node.r, "one_child"203 # Two children -> Find successor204 # Successor is node.r205 if not node.r.l:206 node.r.style = style.BOLD + style.GREEN207 self.add_slide(title + f"Found successor {node.r.key}", 2)208 node.r.style = ""209 node.r.attach_node(node.l, "l")210 return node.r.balance(), "one_successor"211 # Successor is not node.r212 node.r.style = style.BOLD + style.CYAN213 self.add_slide(title + f"Looking for successor {node.r.key}")214 link, successor = get_successor(node.r)215 node.r.style = ""216 successor.attach_node(node.l, "l")217 successor.attach_node(link, "r")218 return successor.balance(), "successor"219 def get_successor(parent):220 successor = parent.l221 if successor.l:222 successor.style = style.BOLD + style.CYAN223 self.add_slide(title + f"Looking for successor {successor.key}")224 link, got = get_successor(successor)225 successor.style = ""226 parent.attach_node(link, "l")227 return (parent.balance(), got)228 else:229 successor.style = style.BOLD + style.GREEN230 self.add_slide(title + f"Found successor {successor.key}", 2)231 parent.attach_node(successor.r, "l")232 successor.style = ""233 return (parent.balance(), successor)234 (del_node, return_case), pause = rec_del(self.root)235 old_root = self.root236 self.root = del_node237 if return_case == "delete_leaf":238 self.add_slide(title + f"Deleted root", 2)239 if return_case in ["one_child", "one_successor", "successor"]:240 del_node.style = style.BOLD + style.GREEN241 self.add_slide(title + f"Replaced {old_root.key} with {del_node.key}", 2)242 del_node.style = ""243 if return_case == "balanced_link":244 del_node.style = style.BOLD + style.GREEN245 self.add_slide(title + f"Balanced node {del_node.key}", 2)246 del_node.style = ""247 if return_case == "link":248 del_node.style = style.BOLD + style.GREEN249 self.add_slide(title + f"Node {del_node.key} is already balanced", pause)250 del_node.style = ""251 self.add_slide("Finish\n", 2)252 def show(self, pause_factor=1, wait=False):253 for slide in self.get_slides():254 for _ in range(slide["pause"]):255 clear()256 if slide["title"]:257 print(slide["title"])258 slide["tree"].display()259 print("\n".join(log))260 if wait:261 input()262 else:263 sleep(1 * pause_factor)264 def play(self, size=15, initial=5, pause_factor=1):265 numbers = [x for x in range(1000)]...
py_virtual_envs.py
Source:py_virtual_envs.py
1from seleniumbase import BaseCase2class PythonVirtualEnvs(BaseCase):3 def test_py_virtual_envs(self):4 self.create_presentation(theme="serif", transition="slide")5 self.add_slide(6 "<h2>Python Virtual Environments:</h2><br />\n"7 "<h2>What, Why, and How</h2><hr /><br />\n"8 "<h3>Presented by <b>Michael Mintz</b></h3>\n"9 "<p>Granite State Code Camp - Sat, Nov 14, 2020</p>"10 )11 self.add_slide(12 "<p><b>About me:</b></p>\n"13 "<ul>"14 "<li>I created the <b>SeleniumBase</b> framework.</li>"15 "<li>I'm currently the DevOps Lead at <b>iboss</b>.</li>"16 "</ul>\n",17 image="https://seleniumbase.io/other/iboss_booth.png",18 )19 self.add_slide(20 "<p><b>Topics & tools covered by this presentation:</b></p>"21 "<hr /><br />\n"22 "<ul>"23 "<li>Overview of Virtual Environments</li>"24 "<li>Python package management</li>"25 '<li>Python 3 "venv"</li>'26 "<li>virtualenv / virtualenvwrapper</li>"27 '<li>pip / "pip install"</li>'28 "<li>requirements.txt files</li>"29 "<li>setup.py files</li>"30 "</ul>"31 )32 self.add_slide(33 "<p><b>Topics & tools that are NOT covered here:</b></p><hr />\n"34 "<br /><div><ul>"35 '<li>"conda"</li>'36 '<li>"pipenv"</li>'37 '<li>"poetry"</li>'38 '<li>"pipx"</li>'39 "</ul></div><br />"40 "<p>(Other Python package management tools)</p>"41 )42 self.add_slide(43 "<p><b>What is a Python virtual environment?</b></p><hr /><br />\n"44 "<p>A Python virtual environment is a partitioned directory"45 " where a Python interpreter, libraries/packages, and scripts"46 " can be installed and isolated from those installed in other"47 " virtual environments or the global environment.</p>"48 )49 self.add_slide(50 "<p><b>Why should we use Python virtual environments?</b>"51 "</p><hr /><br />\n"52 "<p>We should use Python virtual environments because different"53 " Python projects can have conflicting Python dependencies that"54 " cannot coexist in the same env.</p>"55 )56 self.add_slide(57 "<p><b>Why? - continued</b></p><hr /><br />\n"58 "<p>Example: Project A and Project B both depend on"59 " different versions of the same Python library!</p>"60 "<p>Therefore, installing the second project requirements"61 " would overwrite the first one, causing it to break.</p>",62 code=(63 "# Project A requirement:\n"64 "urllib3==1.25.3\n\n"65 "# Project B requirement:\n"66 "urllib3==1.26.2"67 ),68 )69 self.add_slide(70 "<p><b>Why? - continued</b></p><hr /><br />\n"71 "<p>It is also possible that Project A and Project B"72 " require different versions of Python installed!</p>",73 code=(74 "# Project A requirement:\n"75 "Python-3.8\n\n"76 "# Project B requirement:\n"77 "Python-2.7"78 ),79 )80 self.add_slide(81 "<p><b>How do we create and use Python virtual envs?</b>"82 "</p><hr /><br />\n"83 "<div><b>There are tools/scripts we can use:</b></div><br />"84 "<ul>"85 '<li>The Python 3 "venv" command</li>'86 "<li>virtualenv / virtualenvwrapper</li>"87 "</ul>"88 )89 self.add_slide(90 '<p><b>Python 3 "venv"</b></p><hr /><br />\n'91 '"venv" creates virtual environments in the location where run'92 " (generally with Python projects).",93 code=(94 "# Mac / Linux\n"95 "python3 -m venv ENV_NAME\n"96 "source ENV_NAME/bin/activate\n\n"97 "# Windows\n"98 "py -m venv ENV_NAME\n"99 "call ENV_NAME\\Scripts\\activate\n\n"100 '# (Type "deactivate" to leave a virtual environment.)'101 ),102 )103 self.add_slide(104 '<p><b>"mkvirtualenv" (from virtualenvwrapper)</b></p><hr />\n'105 '<br />"mkvirtualenv" creates virtual environments in one place'106 " (generally in your home directory).",107 code=(108 "# Mac / Linux\n"109 "python3 -m pip install virtualenvwrapper\n"110 "export WORKON_HOME=$HOME/.virtualenvs\n"111 "source `which virtualenvwrapper.sh`\n"112 "mkvirtualenv ENV_NAME\n\n"113 "# Windows\n"114 "py -m pip install virtualenvwrapper-win\n"115 "mkvirtualenv ENV_NAME\n\n"116 '# (Type "deactivate" to leave a virtual environment.)'117 ),118 )119 self.add_slide(120 "<p><b>List of commands from virtualenvwrapper</b></p>"121 "<hr /><br />",122 code=(123 "# Create a virtual environment:\n"124 "mkvirtualenv ENV_NAME\n\n"125 "# Exit your virtual environment:\n"126 "deactivate\n\n"127 "# Re-enter a virtual environment:\n"128 "workon ENV_NAME\n\n"129 "# List all virtual environments:\n"130 "workon\n\n"131 "# Delete a virtual environment:\n"132 "rmvirtualenv ENV_NAME"133 ),134 )135 self.add_slide(136 "<p><b>Determining if you are in a virtual env</b></p>"137 "<hr /><br />"138 "<p>When activated, the name of your virtual env"139 " will appear in parentheses on the left side of your"140 " command prompt.</p>",141 code=(142 "# Example of how it may look on a Windows machine:\n"143 "C:\\Users\\Michael\\github> mkvirtualenv my_env\n"144 "(my_env) C:\\Users\\Michael\\github>"145 ),146 )147 self.add_slide(148 '<p><b>Installing packages with "pip install"</b></p><hr /><br />'149 "<p>Once you have created a Python virtual environment and are"150 " inside, it is now safe to install packages from PyPI,"151 " setup.py files, and/or requirements.txt files.</p>\n",152 code=(153 "# Install a package from PyPI:\n"154 "pip install seleniumbase\n\n"155 "# Install packages from a folder with setup.py:\n"156 "pip install . # Normal installation\n"157 "pip install -e . # Editable install\n\n"158 "# Install packages from a requirements.txt file:\n"159 "pip install -r requirements.txt\n"160 ),161 )162 self.add_slide(163 '<p><b>Other useful "pip" commands</b></p><hr /><br />',164 code=(165 "# See which Python packages are installed:\n"166 "pip list\n\n"167 "# See which installed Python packages are outdated:\n"168 "pip list --outdated\n\n"169 "# Create a requirements file from installed packages:\n"170 "pip freeze > my_requirements.txt"171 ),172 )173 self.add_slide(174 "<br /><br /><h2><b>Live Demo Time!</b></h2><hr /><br />",175 image="https://seleniumbase.io/other/python_3d_logo.png",176 )177 self.add_slide(178 "<h2><b>The End. Questions?</b></h2><hr /><br />\n"179 "<h3>Where to find me:</h3>"180 "<ul>"181 '<li><a href="https://github.com/mdmintz">'182 "github.com/mdmintz</a></li>"183 '<li><a href="https://github.com/seleniumbase/SeleniumBase">'184 "github.com/seleniumbase/SeleniumBase</a></li>"185 '<li><a href="https://seleniumbase.io/">'186 "seleniumbase.io</a></li>"187 "</ul>"188 )189 self.begin_presentation(190 filename="py_virtual_envs.html", show_notes=False, interval=0191 )
my_presentation.py
Source:my_presentation.py
1from seleniumbase import BaseCase2class MyPresenterClass(BaseCase):3 def test_presenter(self):4 self.create_presentation(theme="serif", transition="none")5 self.add_slide(6 "<h1>Welcome</h1><br />\n" "<h3>Press the <b>Right Arrow</b></h3>"7 )8 self.add_slide(9 "<h3>SeleniumBase Presenter</h3><br />\n"10 '<img width="240" src="https://seleniumbase.io/img/logo3a.png" />'11 '<span style="margin:144px;" />'12 '<img src="https://seleniumbase.io/other/python_3d_logo.png" />'13 "<br /><br />\n<h4>Create presentations with <b>Python</b></h4>"14 )15 self.add_slide(16 "<h3>Make slides using <b>HTML</b>:</h3><br />\n"17 '<table style="padding:10px;border:4px solid black;font-size:50;">'18 '\n<tr style="background-color:CDFFFF;">\n'19 "<th>Row ABC</th><th>Row XYZ</th></tr>\n"20 '<tr style="background-color:DCFDDC;">'21 "<td>Value ONE</td><td>Value TWO</td></tr>\n"22 '<tr style="background-color:DFDFFB;">\n'23 "<td>Value THREE</td><td>Value FOUR</td></tr>\n"24 "</table><br />\n<h4>(HTML <b>table</b> example)</h4>"25 )26 self.add_slide(27 "<h3>Keyboard Shortcuts:</h3>\n"28 '<table style="padding:10px;border:4px solid black;font-size:30;'29 'background-color:FFFFDD;">\n'30 "<tr><th>Key</th><th>Action</th></tr>\n"31 "<tr><td><b>=></b></td><td>Next Slide (N also works)</td></tr>\n"32 "<tr><td><b><=</b></td><td>Previous Slide (P also works)</td></tr>"33 "\n<tr><td>F</td><td>Full Screen Mode</td></tr>\n"34 "<tr><td>O</td><td>Overview Mode Toggle</td></tr>\n"35 "<tr><td>esc</td><td>Exit Full Screen / Overview Mode</td></tr>\n"36 "<tr><td><b>.</b></td><td>Pause/Resume Toggle</td></tr>\n"37 "<tr><td>space</td><td>Next Slide (alternative)</td></tr></table>"38 )39 self.add_slide(40 "<h3>Add <b>images</b> to slides:</h3>",41 image="https://seleniumbase.io/other/seagulls.jpg",42 )43 self.add_slide(44 "<h3>Add <b>code</b> to slides:</h3>",45 code=(46 "from seleniumbase import BaseCase\n\n"47 "class MyTestClass(BaseCase):\n\n"48 " def test_basics(self):\n"49 ' self.open("https://store.xkcd.com/search")\n'50 ' self.type(\'input[name="q"]\', "xkcd book\\n")\n'51 ' self.assert_text("xkcd: volume 0", "h3")\n'52 ' self.open("https://xkcd.com/353/")\n'53 ' self.assert_title("xkcd: Python")\n'54 " self.assert_element('img[alt=\"Python\"]')\n"55 " self.click('a[rel=\"license\"]')\n"56 ' self.assert_text("free to copy and reuse")\n'57 " self.go_back()\n"58 ' self.click_link("About")\n'59 ' self.assert_exact_text("xkcd.com", "h2")'60 ),61 )62 self.add_slide(63 "<h3>Highlight <b>code</b> in slides:</h3>",64 code=(65 "from seleniumbase import BaseCase\n\n"66 "<mark>class MyTestClass(BaseCase):</mark>\n\n"67 " def test_basics(self):\n"68 ' self.open("https://store.xkcd.com/search")\n'69 ' self.type(\'input[name="q"]\', "xkcd book\\n")\n'70 ' self.assert_text("xkcd: volume 0", "h3")'71 ),72 )73 self.add_slide(74 "<h3>Add <b>iFrames</b> to slides:</h3>",75 iframe="https://seleniumbase.io/demo_page",76 )77 self.add_slide(78 "<h3>Getting started is <b>easy</b>:</h3>",79 code=(80 "from seleniumbase import BaseCase\n\n"81 "class MyPresenterClass(BaseCase):\n\n"82 " def test_presenter(self):\n"83 ' self.create_presentation(theme="serif")\n'84 ' self.add_slide("Welcome to Presenter!")\n'85 " self.add_slide(\n"86 ' "Add code to slides:",\n'87 " code=(\n"88 ' "from seleniumbase import BaseCase\\n\\n"\n'89 ' "class MyPresenterClass(BaseCase):\\n\\n"\n'90 ' " def test_presenter(self):\\n"\n'91 ' " self.create_presentation()\\n"))\n'92 " self.begin_presentation(\n"93 ' filename="demo.html", show_notes=True)'94 ),95 )96 self.add_slide(97 "<h3>Include <b>notes</b> with slides:</h3><br />",98 code=(99 'self.add_slide("[Your HTML goes here]",\n'100 ' code="[Your software code goes here]",\n'101 ' content2="[Additional HTML goes here]",\n'102 ' notes="[Attached speaker notes go here]"\n'103 ' "[Note A! -- Note B! -- Note C! ]")'104 ),105 notes="<h2><ul><li>Note A!<li>Note B!<li>Note C!<li>Note D!</h2>",106 content2="<h4>(Notes can include HTML tags)</h4>",107 )108 self.add_slide(109 "<h3>Multiple <b>themes</b> available:</h3>",110 code=(111 'self.create_presentation(theme="serif")\n\n'112 'self.create_presentation(theme="sky")\n\n'113 'self.create_presentation(theme="simple")\n\n'114 'self.create_presentation(theme="white")\n\n'115 'self.create_presentation(theme="moon")\n\n'116 'self.create_presentation(theme="black")\n\n'117 'self.create_presentation(theme="night")\n\n'118 'self.create_presentation(theme="beige")\n\n'119 'self.create_presentation(theme="league")'120 ),121 )122 self.add_slide(123 "<h2><b>The End</b></h2>",124 image="https://seleniumbase.io/img/sb_logo_10.png",125 )126 self.begin_presentation(127 filename="presenter.html", show_notes=True, interval=0...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!