Skip to content

Instantly share code, notes, and snippets.

@redgeoff
Last active March 2, 2022 20:39
Show Gist options
  • Save redgeoff/7a0cd80a31bb25a806934dc5ab94f046 to your computer and use it in GitHub Desktop.
Save redgeoff/7a0cd80a31bb25a806934dc5ab94f046 to your computer and use it in GitHub Desktop.
Grocery Shopping Bot: Buy It Again
*** Variables ***
${SAFEWAY_HOMEPAGE} https://www.safeway.com
${SAFEWAY_BUY_IT_AGAIN_URL} https://www.safeway.com/shop/purchases/buy-it-again.html
*** Keywords ***
Capture List
@{locators}= Get WebElements xpath=//product-item-v2
${items}= Create List
FOR ${locator} IN @{locators}
${title} Get Child WebElements ${locator} //*[contains(@class,'product-title')]
${name}= Get Text ${title}
${id}= Get Element Attribute ${title} data-bpn
${url}= Get Element Attribute ${title} href
${add_button} Get Child WebElements ${locator} //*[@data-qa='addbutton']
${available}= Run Keyword And Return Status Page Should Contain Element ${add_button}
${remove_button} Get Child WebElements ${locator} //*[@data-qa='prdctdcrmntr']
${in_cart}= Run Keyword And Return Status Page Should Contain Element ${remove_button}
${item}= Create Dictionary id=${id} name=${name} url=${url} available=${available} in_cart=${in_cart}
Append To List ${items} ${item}
END
[Return] ${items}
Sort Buy It Again Items
Click Element When Ready //*[@data-qa='srt-ptns-dflt']
Click Link Frequently Purchased
Get Buy It Again Items
[Arguments] ${max_pages}
Go To ${SAFEWAY_BUY_IT_AGAIN_URL}
Dismiss Popups If Needed
Sort Buy It Again Items
Load More Items ${max_pages}
${items}= Capture List
[Return] ${items}
Load More Items
[Arguments] ${max_pages}
${result} ${condition}= Run Keyword And Ignore Error Wait Until Page Contains Load more error=false
FOR ${i} IN RANGE 1 ${max_pages}
Exit For Loop If '${result}'=='FAIL'
Click Button Load more
${result} ${condition}= Run Keyword And Ignore Error Wait Until Page Contains Load more error=false
END
Add To Cart
[Arguments] ${item_id}
Wait Until Element Ready //*[@id='addButton_${item_id}']
# Safeway requires a mouse over before the item can be added to the cart
Set Focus To Element //*[@id='addButton_${item_id}']
Mouse Over //*[@id='addButton_${item_id}']
Click Element When Ready //*[@id='addButton_${item_id}']
# Wait for loading spinner to disappear
Wait Until Page Does Not Contain Element //*[@class='quantity-loading']
# Wait enough time for cart addition to settle
Wait Until Page Does Not Contain Element //*[@id='addButton_${item_id}']
Wait Until Element Ready //*[@id='dec_qtyInfo_${item_id}'] timeout=10s
Buy It Again
[Arguments] ${items_to_buy} ${max_pages}
${buy_it_again_items}= Get Buy It Again Items ${max_pages}
${items_in_cart}= Create List
${items_not_found}= Create List
FOR ${item_to_buy} IN @{items_to_buy}
Set Local Variable ${added_to_cart} False
FOR ${buy_it_again_item} IN @{buy_it_again_items}
${name_matches}= Run Keyword And Return Status Should Contain ${buy_it_again_item.name} ${item_to_buy.name} ignore_case=True
IF '${name_matches}'=='True' and ('${buy_it_again_item.available}'=='True' or '${buy_it_again_item.in_cart}'=='True')
# Look up the availability again as we may have just added the item to the cart so
# we cannot use ${buy_it_again_item.available}
${available}= Run Keyword And Return Status Page Should Contain Element //*[@id='addButton_${buy_it_again_item.id}']
# If the item is already in the cart then we just ignore it as this allows us to
# retry the script without it adding any additional items to the cart
IF '${available}'=='True'
Log To Console Adding to cart: ${buy_it_again_item.name} (Todoist item name: ${item_to_buy.name})
Wait Until Keyword Succeeds 10x 2 sec Add To Cart ${buy_it_again_item.id}
ELSE
Log To Console Already in cart: ${buy_it_again_item.name} (Todoist item name: ${item_to_buy.name})
END
Set To Dictionary ${item_to_buy} name_in_store=${buy_it_again_item.name}
Append To List ${items_in_cart} ${item_to_buy}
Set Local Variable ${added_to_cart} True
Exit For Loop
END
END
IF '${added_to_cart}'=='False'
Append To List ${items_not_found} ${item_to_buy}
END
END
[Return] ${items_in_cart} ${items_not_found} ${buy_it_again_items}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment