Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
""" This test fails on purpose to demonstrate
the logging capabilities of SeleniumBase.
>>> pytest test_fail.py --html=report.html
This creates ``report.html`` with details.
(Also find log files in ``latest_logs/``) """
import pytest
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
@pytest.mark.expected_failure
def test_find_army_of_robots_on_xkcd_desert_island(self):
self.open("https://xkcd.com/731/")
print("\n(This test fails on purpose)")
self.assert_element("div#ARMY_OF_ROBOTS", timeout=1)
from seleniumbase import BaseCase
class PdfTestClass(BaseCase):
def test_get_pdf_text(self):
pdf = ("https://nostarch.com/download/"
"Automate_the_Boring_Stuff_sample_ch17.pdf")
pdf_text = self.get_pdf_text(pdf, page=1)
print("\n" + pdf_text)
"""
Uses the SeleniumBase implementation of HTML-Inspector to inspect the HTML.
See https://github.com/philipwalton/html-inspector for more details.
(Only works on Chrome and Chromium-based browsers.)
"""
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_html_inspector(self):
self.open("https://xkcd.com/1144/")
self.inspect_html()
'''
You can use this as a boilerplate for your test framework.
Define your customized library methods in a master class like this.
Then have all your test classes inherit it.
BaseTestCase will inherit SeleniumBase methods from BaseCase.
'''
from seleniumbase import BaseCase
class BaseTestCase(BaseCase):
def setUp(self):
super(BaseTestCase, self).setUp()
# <<< Add custom setUp code for tests AFTER the super().setUp() >>>
def tearDown(self):
self.save_teardown_screenshot()
# <<< Add custom tearDown code BEFORE the super().tearDown() >>>
super(BaseTestCase, self).tearDown()
def login(self):
# <<< Placeholder. Add your code here. >>>
# Reduce duplicate code in tests by having reusable methods like this.
# If the UI changes, the fix can be applied in one place.
pass
'''
Demonstration of parsing data from files.
In this example, login information is pulled for tests.
'''
from seleniumbase import BaseCase
class ParseTestCase(BaseCase):
def setUp(self):
super(ParseTestCase, self).setUp()
def get_login_credentials(self, user_type):
# Example of parsing data from a file
f = open('qa_login_example.txt', "r")
file_data = f.read()
file_lines = file_data.split('\n')
for line in file_lines:
line_items = line.split(',')
if line_items[0] == user_type:
return line_items[1], line_items[2]
f.close()
def get_all_login_credentials(self):
from seleniumbase import BaseCase
class MyTourClass(BaseCase):
def test_google_tour(self):
self.open('https://google.com/ncr')
self.wait_for_element('input[title="Search"]')
# Create a website tour using the ShepherdJS library with "dark" theme
# Same as: self.create_shepherd_tour(theme="dark")
self.create_tour(theme="dark")
self.add_tour_step("Welcome to Google!", title="SeleniumBase Tours")
self.add_tour_step("Type in your query here.", 'input[title="Search"]')
self.play_tour()
self.highlight_update_text('input[title="Search"]', "Google")
self.wait_for_element('[role="listbox"]') # Wait for autocomplete
# Create a website tour using the ShepherdJS library with "light" theme
from seleniumbase.config import settings
from seleniumbase.fixtures import js_utils
LATEST_REPORT_DIR = settings.LATEST_REPORT_DIR
ARCHIVE_DIR = settings.REPORT_ARCHIVE_DIR
RESULTS_PAGE = settings.HTML_REPORT
BAD_PAGE_LOG = settings.RESULTS_TABLE
DEFAULT_VALIDATION_MESSAGE = settings.MASTERQA_DEFAULT_VALIDATION_MESSAGE
WAIT_TIME_BEFORE_VERIFY = settings.MASTERQA_WAIT_TIME_BEFORE_VERIFY
START_IN_FULL_SCREEN_MODE = settings.MASTERQA_START_IN_FULL_SCREEN_MODE
MAX_IDLE_TIME_BEFORE_QUIT = settings.MASTERQA_MAX_IDLE_TIME_BEFORE_QUIT
# This tool allows testers to quickly verify pages while assisted by automation
class __MasterQATestCase__(BaseCase):
def get_timestamp(self):
return str(int(time.time() * 1000))
def manual_check_setup(self):
self.manual_check_count = 0
self.manual_check_successes = 0
self.incomplete_runs = 0
self.page_results_list = []
self.clear_out_old_logs(archive_past_runs=False)
def clear_out_old_logs(self, archive_past_runs=True, get_log_folder=False):
abs_path = os.path.abspath('.')
file_path = abs_path + "/%s" % LATEST_REPORT_DIR
if not os.path.exists(file_path):
os.makedirs(file_path)
from seleniumbase import BaseCase
class MyTestClass(BaseCase):
def test_basic(self):
self.open('https://xkcd.com/1117/')
self.assert_element('img[alt="My Sky"]')
self.create_shepherd_tour()
self.add_tour_step("Welcome to XKCD!")
self.add_tour_step("This is the XKCD logo.", "#masthead img")
self.add_tour_step("Here's the daily webcomic.", "#comic img")
self.add_tour_step("This is the title.", "#ctitle", alignment="top")
self.add_tour_step("Click here for the next comic.", 'a[rel="next"]')
self.add_tour_step("Click here for the previous one.", 'a[rel="prev"]')
self.add_tour_step("Learn about the author here.", 'a[rel="author"]')
self.add_tour_step("Click here for the license.", 'a[rel="license"]')
self.add_tour_step("Click for a random comic.", 'a[href*="/random/"]')
self.add_tour_step("Thanks for taking this tour!")
self.export_tour(filename="xkcd_tour.js") # Exports the tour
from masterqa import settings
from seleniumbase.fixtures import js_utils
LATEST_REPORT_DIR = settings.LATEST_REPORT_DIR
ARCHIVE_DIR = settings.REPORT_ARCHIVE_DIR
RESULTS_PAGE = settings.HTML_REPORT
BAD_PAGE_LOG = settings.RESULTS_TABLE
DEFAULT_VALIDATION_MESSAGE = settings.DEFAULT_VALIDATION_MESSAGE
WAIT_TIME_BEFORE_VERIFY = settings.WAIT_TIME_BEFORE_VERIFY
START_IN_FULL_SCREEN_MODE = settings.START_IN_FULL_SCREEN_MODE
MAX_IDLE_TIME_BEFORE_QUIT = settings.MAX_IDLE_TIME_BEFORE_QUIT
# This tool allows testers to quickly verify pages while assisted by automation
class __MasterQATestCase__(BaseCase):
def get_timestamp(self):
return str(int(time.time() * 1000))
def manual_check_setup(self):
self.manual_check_count = 0
self.manual_check_successes = 0
self.incomplete_runs = 0
self.page_results_list = []
self.clear_out_old_logs(archive_past_runs=False)
def clear_out_old_logs(self, archive_past_runs=True, get_log_folder=False):
abs_path = os.path.abspath('.')
file_path = abs_path + "/%s" % LATEST_REPORT_DIR
if not os.path.exists(file_path):
os.makedirs(file_path)
import pytest
from parameterized import parameterized
from seleniumbase import BaseCase
class SwagLabsTests(BaseCase):
def login(self, user="standard_user"):
""" Login to Swag Labs and assert that the login was successful. """
if user not in (["standard_user", "problem_user"]):
raise Exception("Invalid user!")
self.open("https://www.saucedemo.com/")
self.update_text("#user-name", user)
self.update_text("#password", "secret_sauce")
self.click('input[type="submit"]')
self.assert_element("#inventory_container")
self.assert_text("Products", "div.product_label")
@parameterized.expand([
["standard_user"],
["problem_user"],
])