From c5437b732c4d46441e360e7d1ddc63fab53aef22 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sun, 28 Aug 2022 13:55:42 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- environment.py | 34 +++++++++++++++++----------------- sauce_demo_bdd/browser.py | 4 ++-- steps/inventory.py | 6 ++++-- steps/login.py | 12 +++++++----- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/environment.py b/environment.py index d82f445..16e5950 100644 --- a/environment.py +++ b/environment.py @@ -31,20 +31,20 @@ def before_scenario(context: SauceDemoContext, scenario: Scenario): def after_scenario(context: SauceDemoContext, scenario: Scenario): - if scenario.status == "failed": - scenario_error_dir = Path("logs") - scenario_error_dir.mkdir(exist_ok=True) - base_name = time.strftime( - "%Y-%m-%d_%H%M%S_{}".format(re.sub(r'[/\\:*?"<>#]', "", scenario.name)[:60]) - ) - log_file_path = scenario_error_dir / "{}.txt".format(base_name) - for step in scenario.steps: - if step.status in ["failed", "undefined"]: - log_file_path.write_text( - "Scenario: {}\nStep: {} {}\nError Message: {}".format( - scenario.name, step.keyword, step.name, step.error_message - ) - ) - break - else: - log_file_path.write_text("Scenario: {}\nStep: N/A".format(scenario.name)) + if scenario.status != "failed": + return + scenario_error_dir = Path("logs") + scenario_error_dir.mkdir(exist_ok=True) + base_name = time.strftime( + "%Y-%m-%d_%H%M%S_{}".format(re.sub(r'[/\\:*?"<>#]', "", scenario.name)[:60]) + ) + log_file_path = scenario_error_dir / f"{base_name}.txt" + for step in scenario.steps: + if step.status in ["failed", "undefined"]: + log_file_path.write_text( + f"Scenario: {scenario.name}\nStep: {step.keyword} {step.name}\nError Message: {step.error_message}" + ) + + break + else: + log_file_path.write_text(f"Scenario: {scenario.name}\nStep: N/A") diff --git a/sauce_demo_bdd/browser.py b/sauce_demo_bdd/browser.py index 1b17769..4d0aa83 100644 --- a/sauce_demo_bdd/browser.py +++ b/sauce_demo_bdd/browser.py @@ -23,7 +23,7 @@ def chrome_options(): if not gpu: _chrome_options.add_argument("--disable-gpu") if window_size: - _chrome_options.add_argument("--window-size={}".format(window_size)) + _chrome_options.add_argument(f"--window-size={window_size}") if headless: _chrome_options.add_argument("--headless") _chrome_options.add_argument("--load-images=no") @@ -51,7 +51,7 @@ def __init__(self, driver: WebDriver, base_url: str) -> None: self.base_url = base_url def wait(self, parent_elem: WebElement = None, timeout: int = 10) -> WebDriverWait: - parent = parent_elem if parent_elem else self.driver + parent = parent_elem or self.driver return WebDriverWait(parent, timeout) def open(self, url: str, reopen: bool = False) -> None: diff --git a/steps/inventory.py b/steps/inventory.py index 5eed7b9..9bc4177 100644 --- a/steps/inventory.py +++ b/steps/inventory.py @@ -10,16 +10,18 @@ @when("I sort the products by {sort_order} order") def step_impl_1(context: SauceDemoContext, sort_order: str): sort_option = context.driver.find_element_by_xpath( - "//option[contains(text(),'{}')]".format(sort_order) + f"//option[contains(text(),'{sort_order}')]" ) + sort_option.click() @then("the products page is sorted in {sort_order} order") def step_impl_2(context: SauceDemoContext, sort_order: str): product_sort_container = context.driver.find_element_by_xpath( - "//span[contains(text(),'{}')]".format(sort_order) + f"//span[contains(text(),'{sort_order}')]" ) + expect(product_sort_container.text.lower()).to(equal(sort_order.lower())) diff --git a/steps/login.py b/steps/login.py index 39b93e7..9286a2c 100644 --- a/steps/login.py +++ b/steps/login.py @@ -41,8 +41,9 @@ def step_impl_5(context: SauceDemoContext, page_name: str): # create a dictionary for relative url reference page_dict = {"login": "", "products": "inventory.html", "cart": "cart.html"} expected_url = parse.urljoin( - context.browser_data["url"], "{}".format(page_dict[page_name.lower()]) + context.browser_data["url"], f"{page_dict[page_name.lower()]}" ) + context.wait.until(ec.url_matches(expected_url)) @@ -87,25 +88,26 @@ def step_impl_9(context: SauceDemoContext, sidebar_link: str): ec.visibility_of_element_located( ( By.XPATH, - "//a[@class='bm-item menu-item' " - "and contains(text(),'{}')]".format(sidebar_link), + f"//a[@class='bm-item menu-item' and contains(text(),'{sidebar_link}')]", ) ) ) + selected_sidebar_link.click() @when("I enter an incorrect password") def step_impl_10(context: SauceDemoContext): password = context.driver.find_element_by_xpath("//input[@id='password']") - password.send_keys("{}{}".format(context.user["password"], time.time())) + password.send_keys(f'{context.user["password"]}{time.time()}') @then("this error message is displayed") def step_impl_11(context: SauceDemoContext): error_message = context.wait.until( ec.presence_of_element_located( - (By.XPATH, '//h3[contains(text(),"{}")]'.format(context.text.strip())) + (By.XPATH, f'//h3[contains(text(),"{context.text.strip()}")]') ) ) + expect(error_message.text).to(equal(context.text.strip()))