Skip to content

Commit

Permalink
Fixed log path / screen names
Browse files Browse the repository at this point in the history
  • Loading branch information
arodrime committed Jan 26, 2019
1 parent 02e32b0 commit 0f2c193
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
__pycache__
examples
*.log
.DS_Store
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ wasting your precious time waiting!

# Usage

*Requires python 3*

- Install requirements: `pip install -r requirements.txt`

- One of the dependency is `Selenium` that depends on drivers: https://github.com/SeleniumHQ/selenium/blob/master/py/docs/source/index.rst#user-content-drivers.
Expand Down
26 changes: 13 additions & 13 deletions buyKim.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ def main(timeout_conn, interval, product_family, ref_product, ref_zones, quantit
url_availability = "https://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2"
not_available_terms = ['unknown', 'unavailable']

time_run = datetime.now().strftime("%y-%m-%d %H-%M-%f")

screenshot_dir = os.getenv("SCREENSHOT_DIR", os.path.abspath("screens"))
log_dir = os.getenv("LOG_DIR", os.getcwd())
script_dir = os.path.dirname(os.path.realpath(__file__))
# Get env var if any, or the script directory as a fallback
# Then in any case the "screens" folder to the path.
screenshot_dir = os.path.join(os.getenv("SCREENSHOT_DIR", script_dir), "screens")
if not os.path.exists(screenshot_dir):
os.makedirs(screenshot_dir)
print("Saving screenshots in {}".format(screenshot_dir))

# Logs configuration
log_dir = os.getenv("LOG_DIR", script_dir)
if not os.path.exists(log_dir):
os.makedirs(log_dir)
log_filename = os.path.join(log_dir, "buyKim.log")

print("Log filename: {}".format(log_filename))
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', filename=log_filename, level=logging.DEBUG)
logging.getLogger("requests").setLevel(logging.WARNING)

print_and_log("Saving screenshots in {}".format(screenshot_dir))
screen_prefix = screenshot_dir + time_run

available = False
while not available:
success = False
Expand Down Expand Up @@ -184,9 +184,9 @@ def main(timeout_conn, interval, product_family, ref_product, ref_zones, quantit
button_existing.click()
print_and_log("Clicked on existing customer.")

screenshot_step(driver, screen_prefix, 1)
screenshot_step(driver, screenshot_dir, 1)
driver.execute_script("arguments[0].scrollIntoView(true);", button_existing)
screenshot_step(driver, screen_prefix, 2)
screenshot_step(driver, screenshot_dir, 2)
# Locate login inputs
input_login = driver.find_element_by_id(id_input_login)
input_pass = driver.find_element_by_id(id_input_pass)
Expand All @@ -198,7 +198,7 @@ def main(timeout_conn, interval, product_family, ref_product, ref_zones, quantit
driver.find_element_by_css_selector(css_button_login).click()
print_and_log("Clicked on login button.")

screenshot_step(driver, screen_prefix, 3)
screenshot_step(driver, screenshot_dir, 3)

# Wait for means of payment to load
css_payment_valid = "div.payment-means-choice div.payment-means-list form span.selected input.custom-radio.ng-valid"
Expand All @@ -218,9 +218,9 @@ def main(timeout_conn, interval, product_family, ref_product, ref_zones, quantit
print_and_log("Clicked on purchase button...")

# Wait to realise what you've done
screenshot_step(driver, screen_prefix, 4)
screenshot_step(driver, screenshot_dir, 4)
time.sleep(30)
screenshot_step(driver, screen_prefix, 5)
screenshot_step(driver, screenshot_dir, 5)
driver.close()


Expand Down
12 changes: 8 additions & 4 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import os
from datetime import datetime

from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

Expand All @@ -11,8 +14,9 @@ def zoom_out(driver):
html.send_keys(Keys.CONTROL, '-')


def screenshot_step(driver, screen_prefix, step_number):
step_filename = screen_prefix + " - step%d.png" % step_number
retval = driver.save_screenshot(step_filename)
print("Saving screenshot %d as %s..." % (step_number, step_filename),
def screenshot_step(driver, screen_path, step_number):
file_name = "{:%Y-%m-%d-%H-%M-%S}-step{}.png".format(datetime.now(), step_number)
screenshot_full_path = os.path.realpath(os.path.join(screen_path, file_name))
retval = driver.save_screenshot(screenshot_full_path)
print("Saving screenshot {} as {}...".format(step_number, screenshot_full_path),
"Success" if retval else "Error")

0 comments on commit 0f2c193

Please sign in to comment.