Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
Fix Promotional items (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Crissal1995 committed Aug 20, 2023
1 parent 7978ba0 commit 85a7398
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
34 changes: 22 additions & 12 deletions automsr/browser/browser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging
from pathlib import Path
from typing import Any
from typing import Any, Optional

from attr import define
from selenium.common import WebDriverException
Expand Down Expand Up @@ -223,9 +223,13 @@ def change_window(self, handle_name: str) -> None:
window = windows[window_index]
self.driver.switch_to.window(window)

def open_promotion(self, promotion: Promotion) -> None:
def open_promotion(
self, promotion: Promotion, element_id: Optional[str] = None
) -> None:
"""
Open correctly a Promotion found in the Rewards homepage.
If the `element-id` is provided, use that to retrieve the WebElement from the page.
"""

current_url: str = self.driver.current_url
Expand All @@ -235,13 +239,22 @@ def open_promotion(self, promotion: Promotion) -> None:
logger.debug("Changing current page to Rewards homepage.")
self.go_to_rewards()

css_selector_value = f'.rewards-card-container[data-bi-id="{promotion.name}"]'
logger.debug(
"Looking for promotion using the css selector: %s", css_selector_value
)
promotion_element = self.driver.find_element(
by=By.CSS_SELECTOR, value=css_selector_value
)
if element_id is not None:
logger.debug("Searching for promotion with id: %s", element_id)
if not element_id:
raise ValueError("Empty element id!")
promotion_element = self.driver.find_element(by=By.ID, value=element_id)
else:
css_selector_value = (
f'.rewards-card-container[data-bi-id="{promotion.name}"]'
)
logger.debug(
"Searching for promotion with css selector: %s", css_selector_value
)
promotion_element = self.driver.find_element(
by=By.CSS_SELECTOR, value=css_selector_value
)

logger.debug("Promotion found! Clicking it to trigger the promotion start.")

# store window handles before the click
Expand All @@ -253,8 +266,5 @@ def open_promotion(self, promotion: Promotion) -> None:
# store window handles after the click
handles_after = self.driver.window_handles

logger.debug("Window handles before click: %s", handles_before)
logger.debug("Window handles after click: %s", handles_after)

handle = set(handles_after).difference(handles_before).pop()
self.change_window(handle_name=handle)
7 changes: 3 additions & 4 deletions automsr/datatypes/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,15 @@ def get_promotions(self) -> List[Promotion]:
# show up in the Rewards homepage, it is better to not include it
# in the parsed promotions.

# Then, take the optional promotion, if available
if self.promotionalItem is not None:
promotions.append(self.promotionalItem)

# Then add the extra promotions
promotions.extend(self.morePromotions)

# Then returns a copy of the list
return copy.deepcopy(promotions)

def get_promotional_item(self) -> Optional[Promotion]:
return self.promotionalItem

def get_completable_promotions(self) -> List[Promotion]:
"""
Get the list of all completable promotions in the current dashboard.
Expand Down
19 changes: 19 additions & 0 deletions automsr/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,25 @@ def execute_promotions(self, dashboard: Dashboard) -> None:
Execute all completable promotions.
"""

# handle promotional items differently, since there is another way
# to retrieve the corresponding WebElement
promotional_item = dashboard.get_promotional_item()
if promotional_item is not None:
try:
self.browser.open_promotion(
promotion=promotional_item, element_id="promo-item"
)
except Exception as e:
logger.error("Exception caught: %s", e)
logger.warning("Promotion '%s' will be skipped", promotional_item)
finally:
# simulate navigation
logger.debug("Re-opening Rewards homepage to simulate navigation.")
self.browser.go_to_rewards()
logger.debug("Sleeping to simulate a real user behavior.")
time.sleep(2)

# handle all other promotions
promotions: List[Promotion] = dashboard.get_completable_promotions()

for promotion in promotions:
Expand Down

0 comments on commit 85a7398

Please sign in to comment.