Skip to content

Sourcery refactored main branch #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Comment on lines -34 to +50
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function after_scenario refactored with the following changes:

4 changes: 2 additions & 2 deletions sauce_demo_bdd/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function chrome_options refactored with the following changes:

if headless:
_chrome_options.add_argument("--headless")
_chrome_options.add_argument("--load-images=no")
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Browser.wait refactored with the following changes:

return WebDriverWait(parent, timeout)

def open(self, url: str, reopen: bool = False) -> None:
Expand Down
6 changes: 4 additions & 2 deletions steps/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')]"
)

Comment on lines -13 to +15
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function step_impl_1 refactored with the following changes:

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}')]"
)

Comment on lines -21 to +24
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function step_impl_2 refactored with the following changes:

expect(product_sort_container.text.lower()).to(equal(sort_order.lower()))


Expand Down
12 changes: 7 additions & 5 deletions steps/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]}"
)

Comment on lines -44 to +46
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function step_impl_5 refactored with the following changes:

context.wait.until(ec.url_matches(expected_url))


Expand Down Expand Up @@ -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}')]",
)
)
)

Comment on lines -90 to +95
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function step_impl_9 refactored with the following changes:

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()}')
Comment on lines -101 to +102
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function step_impl_10 refactored with the following changes:



@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()}")]')
)
)

Comment on lines -108 to +112
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function step_impl_11 refactored with the following changes:

expect(error_message.text).to(equal(context.text.strip()))