Skip to content

Commit b854c70

Browse files
committed
Fixed log path / screen names
1 parent 02e32b0 commit b854c70

File tree

4 files changed

+25
-18
lines changed

4 files changed

+25
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
__pycache__
44
examples
55
*.log
6+
.DS_Store

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ wasting your precious time waiting!
1313

1414
# Usage
1515

16+
*Requires python 3*
17+
1618
- Install requirements: `pip install -r requirements.txt`
1719

1820
- 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.

buyKim.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ def main(timeout_conn, interval, product_family, ref_product, ref_zones, quantit
4545
url_availability = "https://ws.ovh.com/dedicated/r2/ws.dispatcher/getAvailability2"
4646
not_available_terms = ['unknown', 'unavailable']
4747

48-
time_run = datetime.now().strftime("%y-%m-%d %H-%M-%f")
49-
50-
screenshot_dir = os.getenv("SCREENSHOT_DIR", os.path.abspath("screens"))
51-
log_dir = os.getenv("LOG_DIR", os.getcwd())
48+
script_dir = os.path.dirname(os.path.realpath(__file__))
49+
# Get env var if any, or the script directory as a fallback
50+
# Then in any case the "screens" folder to the path.
51+
screenshot_dir = os.path.join(os.getenv("SCREENSHOT_DIR", script_dir), "screens")
5252
if not os.path.exists(screenshot_dir):
5353
os.makedirs(screenshot_dir)
54+
print("Saving screenshots in {}".format(screenshot_dir))
55+
56+
# Logs configuration
57+
log_dir = os.getenv("LOG_DIR", script_dir)
5458
if not os.path.exists(log_dir):
5559
os.makedirs(log_dir)
5660
log_filename = os.path.join(log_dir, "buyKim.log")
57-
5861
print("Log filename: {}".format(log_filename))
5962
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', filename=log_filename, level=logging.DEBUG)
6063
logging.getLogger("requests").setLevel(logging.WARNING)
6164

62-
print_and_log("Saving screenshots in {}".format(screenshot_dir))
63-
screen_prefix = screenshot_dir + time_run
64-
6565
available = False
6666
while not available:
6767
success = False
@@ -184,9 +184,9 @@ def main(timeout_conn, interval, product_family, ref_product, ref_zones, quantit
184184
button_existing.click()
185185
print_and_log("Clicked on existing customer.")
186186

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

201-
screenshot_step(driver, screen_prefix, 3)
201+
screenshot_step(driver, screenshot_dir, 3)
202202

203203
# Wait for means of payment to load
204204
css_payment_valid = "div.payment-means-choice div.payment-means-list form span.selected input.custom-radio.ng-valid"
@@ -213,14 +213,14 @@ def main(timeout_conn, interval, product_family, ref_product, ref_zones, quantit
213213

214214
# uncommented after numerous tests
215215
if not debug:
216-
css_button_purchase = ".zone-content section:last button.centered"
216+
css_button_purchase = "div.zone-content div.dedicated-contracts button.centered"
217217
driver.find_element_by_css_selector(css_button_purchase).click()
218218
print_and_log("Clicked on purchase button...")
219219

220220
# Wait to realise what you've done
221-
screenshot_step(driver, screen_prefix, 4)
221+
screenshot_step(driver, screenshot_dir, 4)
222222
time.sleep(30)
223-
screenshot_step(driver, screen_prefix, 5)
223+
screenshot_step(driver, screenshot_dir, 5)
224224
driver.close()
225225

226226

utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
from datetime import datetime
3+
14
from selenium.webdriver.common.by import By
25
from selenium.webdriver.common.keys import Keys
36

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

1316

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

0 commit comments

Comments
 (0)