|
# -*- coding: utf-8 -*- |
|
from datetime import datetime |
|
from selenium import webdriver |
|
from selenium.webdriver.common.by import By |
|
from selenium.webdriver.common.keys import Keys |
|
from selenium.webdriver.support.ui import Select |
|
from selenium.common.exceptions import NoSuchElementException |
|
from selenium.common.exceptions import NoAlertPresentException |
|
import unittest, time, re |
|
from users import ZiffitUser as user |
|
|
|
class Ziffit(unittest.TestCase): |
|
def setUp(self): |
|
self.driver = webdriver.Chrome() |
|
self.driver.implicitly_wait(30) |
|
self.base_url = "https://www.ziffit.com/" |
|
self.verificationErrors = [] |
|
self.accept_next_alert = True |
|
self.barcode_filename = 'barcodes.txt' |
|
self.log_prefix = 'ziffit' |
|
self.save_order = True |
|
|
|
def test_ziffit(self): |
|
driver = self.driver |
|
driver.get(self.base_url + "/") |
|
for i in range(60): |
|
try: |
|
if self.is_element_present(By.CSS_SELECTOR, "a.login.button > strong"): break |
|
except: pass |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
driver.find_element_by_css_selector("a.login.button > strong").click() |
|
for i in range(60): |
|
try: |
|
if self.is_element_present(By.NAME, "USERNAME"): break |
|
except: pass |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
for i in range(60): |
|
try: |
|
if self.is_element_present(By.NAME, "PASSWORD"): break |
|
except: pass |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
for i in range(60): |
|
try: |
|
if self.is_element_present(By.CSS_SELECTOR, "input.loginFormButton"): break |
|
except: pass |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
driver.find_element_by_name("USERNAME").clear() |
|
driver.find_element_by_name("USERNAME").send_keys(user.username) |
|
driver.find_element_by_name("PASSWORD").clear() |
|
driver.find_element_by_name("PASSWORD").send_keys(user.password) |
|
driver.find_element_by_css_selector("input.loginFormButton").click() |
|
|
|
time.sleep(5) |
|
driver.get(self.base_url+'sell-my-cds') |
|
for i in range(60): |
|
try: |
|
if self.is_element_present(By.CSS_SELECTOR, "#search > #eanSearchForm > fieldset > #ean"): break |
|
except: pass |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
|
|
timestamp = datetime.strftime(datetime.now(), "%Y%m%d-%H%M%S") |
|
db_filename = '%s_%s.tsv' % (timestamp, self.log_prefix) |
|
err_filename = '%s_%s_log.txt' % (timestamp, self.log_prefix) |
|
|
|
# do the first one, it's |
|
driver.find_element_by_css_selector("#search > #eanSearchForm > fieldset > #ean").clear() |
|
driver.find_element_by_css_selector("#search > #eanSearchForm > fieldset > #ean").send_keys("test") |
|
driver.find_element_by_css_selector("#search > #eanSearchForm > fieldset > input[type=\"submit\"]").click() |
|
|
|
with open(self.barcode_filename, 'r') as barcodes: |
|
for code in barcodes.readlines(): |
|
code = code.strip() if code else None |
|
if code is None or code == '': |
|
continue |
|
print "Code: %s" % code |
|
|
|
old_scan_text = driver.find_element_by_css_selector("p.scan-response-message").text |
|
# Sending the barcode |
|
try: |
|
ean = driver.find_element_by_id("ean") |
|
ean.click() |
|
ean.send_keys(code) |
|
except Exception as e: |
|
print "Exception while eaning" |
|
print e |
|
driver.find_element_by_css_selector("#basketEanSearchForm > img").click() |
|
time.sleep(1) |
|
|
|
for i in range(60): |
|
db = open(db_filename+'.tsv', 'a') |
|
log = open(err_filename+'.txt', 'a') |
|
try: |
|
scan_text = driver.find_element_by_css_selector("p.scan-response-message").text |
|
if scan_text != old_scan_text: |
|
# it's updated |
|
if 'added to your trade' in scan_text: |
|
trs = driver.find_elements_by_css_selector('#basketTable > tbody tr') |
|
if len(trs) < 1: |
|
break |
|
tds = trs[0].find_elements_by_tag_name('td') |
|
l = "%s\t%s\t%s\n" % (tds[1].text.encode('ascii', 'ignore'), |
|
tds[0].text.encode('ascii', 'ignore'), |
|
tds[3].text.encode('ascii', 'ignore')) |
|
db.write(l) |
|
log.write(l) |
|
break |
|
else: |
|
log.write(scan_text) |
|
log.write('\n') |
|
break |
|
else: |
|
break |
|
except Exception as e: |
|
print "Exception" |
|
print e |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
|
|
|
|
if self.save_order: |
|
for i in range(60): |
|
try: |
|
if self.is_element_present(By.CSS_SELECTOR, "#basket-operations-container > a > img"): break |
|
except: pass |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
driver.find_element_by_css_selector("#basket-operations-container > a > img").click() |
|
for i in range(60): |
|
try: |
|
if self.is_element_present(By.CSS_SELECTOR, "p"): break |
|
except: pass |
|
time.sleep(1) |
|
else: self.fail("time out") |
|
|
|
print "Finished!" |
|
|
|
def is_element_present(self, how, what): |
|
try: self.driver.find_element(by=how, value=what) |
|
except NoSuchElementException, e: return False |
|
return True |
|
|
|
def is_alert_present(self): |
|
try: self.driver.switch_to_alert() |
|
except NoAlertPresentException, e: return False |
|
return True |
|
|
|
def close_alert_and_get_its_text(self): |
|
try: |
|
alert = self.driver.switch_to_alert() |
|
alert_text = alert.text |
|
if self.accept_next_alert: |
|
alert.accept() |
|
else: |
|
alert.dismiss() |
|
return alert_text |
|
finally: self.accept_next_alert = True |
|
|
|
def tearDown(self): |
|
self.driver.quit() |
|
self.assertEqual([], self.verificationErrors) |
|
|
|
if __name__ == "__main__": |
|
unittest.main() |
Hi getting a problem with musicmagpie.py and ziffit.py
I have previously added brackets around the print commands that were showing errors,
On both commas, it comes up as invalid syntax.