Changing payment tests to use webdriver, updating to use explicit waits instead of sleep, adding logging granularoty while running tests

pull/1/head
icellama21 2012-01-26 11:21:49 -05:00
parent e9d293199c
commit a51d4c14e0
1 changed files with 63 additions and 63 deletions

View File

@ -9,85 +9,84 @@ from django.test import TestCase
from django.utils import unittest from django.utils import unittest
from django.conf import settings from django.conf import settings
from regluit.payment.manager import PaymentManager from regluit.payment.manager import PaymentManager
from regluit.payment.paypal import IPN, IPN_PAY_STATUS_ACTIVE, IPN_PAY_STATUS_COMPLETED, IPN_TXN_STATUS_COMPLETED, IPN_PAY_STATUS_COMPLETED, IPN_TXN_STATUS_COMPLETED
from noseselenium.cases import SeleniumTestCaseMixin
from regluit.payment.models import Transaction from regluit.payment.models import Transaction
from regluit.core.models import Campaign, Wishlist, Work from regluit.core.models import Campaign, Wishlist, Work
from django.contrib.auth.models import User
from regluit.payment.parameters import * from regluit.payment.parameters import *
from regluit.payment.paypal import *
import traceback import traceback
from django.core.validators import URLValidator from django.core.validators import URLValidator
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
import time import time
from selenium import selenium, webdriver from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import logging
from decimal import Decimal as D from decimal import Decimal as D
import datetime import datetime
def set_test_logging():
# Setup debug logging to our console so we can watch
defaultLogger = logging.getLogger('')
defaultLogger.addHandler(logging.StreamHandler())
defaultLogger.setLevel(logging.DEBUG)
# Set the selenium logger to info
sel = logging.getLogger("selenium")
sel.setLevel(logging.INFO)
def loginSandbox(test, selenium): def loginSandbox(test, selenium):
print "LOGIN SANDBOX" print "LOGIN SANDBOX"
try: try:
selenium.open('https://developer.paypal.com/') selenium.get('https://developer.paypal.com/')
time.sleep(5) login_email = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("login_email"))
test.failUnless(selenium.is_text_present('Member Log In')) login_email.click()
selenium.type('login_email', settings.PAYPAL_SANDBOX_LOGIN) login_email.send_keys(settings.PAYPAL_SANDBOX_LOGIN)
selenium.type('login_password', settings.PAYPAL_SANDBOX_PASSWORD)
time.sleep(2) login_password = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("login_password"))
selenium.click('css=input[class=\"formBtnOrange\"]') login_password.click()
time.sleep(5) login_password.send_keys(settings.PAYPAL_SANDBOX_PASSWORD)
test.failUnless(selenium.is_text_present('Test Accounts'))
submit_button = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_css_selector("input[class=\"formBtnOrange\"]"))
submit_button.click()
except: except:
traceback.print_exc() traceback.print_exc()
def authorizeSandbox(test, selenium, url): def paySandbox(test, selenium, url, authorize=False):
print "AUTHORIZE SANDBOX" if authorize:
print "AUTHORIZE SANDBOX"
else:
print "PAY SANDBOX"
try: try:
selenium.open(url) selenium.get(url)
time.sleep(5) login_element = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("loadLogin"))
test.failUnless(selenium.is_text_present('Your preapproved payment summary')) login_element.click()
selenium.click('loadLogin')
time.sleep(5) email_element = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("login_email"))
selenium.type('id=login_email', settings.PAYPAL_BUYER_LOGIN) email_element.click()
selenium.type('id=login_password', settings.PAYPAL_BUYER_PASSWORD) email_element.send_keys(settings.PAYPAL_BUYER_LOGIN)
time.sleep(2)
selenium.click('submitLogin') password_element = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("login_password"))
time.sleep(5) password_element.click()
test.failUnless(selenium.is_text_present('Review your information')) password_element.send_keys(settings.PAYPAL_BUYER_PASSWORD)
selenium.click('submit.x')
time.sleep(10) submit_button = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("submitLogin"))
selenium.click('returnToMerchant') submit_button.click()
time.sleep(15)
final_submit = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("submit.x"))
final_submit.click()
return_button = WebDriverWait(selenium, 10).until(lambda d : d.find_element_by_id("returnToMerchant"))
return_button.click()
except: except:
traceback.print_exc() traceback.print_exc()
def paySandbox(test, selenium, url):
print "PAY SANDBOX"
try:
selenium.open(url)
time.sleep(5)
test.failUnless(selenium.is_text_present('Your payment summary'))
selenium.click('loadLogin')
time.sleep(5)
selenium.type('id=login_email', settings.PAYPAL_BUYER_LOGIN)
selenium.type('id=login_password', settings.PAYPAL_BUYER_PASSWORD)
time.sleep(2)
selenium.click('submitLogin')
time.sleep(5)
test.failUnless(selenium.is_text_present('Review your information'))
selenium.click('submit.x')
time.sleep(10)
selenium.click('returnToMerchant')
time.sleep(15)
except:
traceback.print_exc()
class PledgeTest(TestCase): class PledgeTest(TestCase):
@ -96,9 +95,8 @@ class PledgeTest(TestCase):
# This is an empty array where we will store any verification errors # This is an empty array where we will store any verification errors
# we find in our tests # we find in our tests
self.selenium = selenium("localhost", 4444, "*firefox", self.selenium = webdriver.Firefox()
"http://www.google.com/") set_test_logging()
self.selenium.start()
def validateRedirect(self, t, url, count): def validateRedirect(self, t, url, count):
@ -107,7 +105,7 @@ class PledgeTest(TestCase):
self.assertEqual(t.receiver_set.all().count(), count) self.assertEqual(t.receiver_set.all().count(), count)
self.assertEqual(t.receiver_set.all()[0].amount, t.amount) self.assertEqual(t.receiver_set.all()[0].amount, t.amount)
self.assertEqual(t.receiver_set.all()[0].currency, t.currency) self.assertEqual(t.receiver_set.all()[0].currency, t.currency)
# self.assertNotEqual(t.reference, None) # self.assertNotEqual(t.ref1Gerence, None)
self.assertEqual(t.error, None) self.assertEqual(t.error, None)
self.assertEqual(t.status, IPN_PAY_STATUS_CREATED) self.assertEqual(t.status, IPN_PAY_STATUS_CREATED)
@ -131,6 +129,9 @@ class PledgeTest(TestCase):
loginSandbox(self, self.selenium) loginSandbox(self, self.selenium)
paySandbox(self, self.selenium, url) paySandbox(self, self.selenium, url)
# sleep to make sure the transaction has time to complete
time.sleep(10)
# by now we should have received the IPN # by now we should have received the IPN
# right now, for running on machine with no acess to IPN, we manually update statuses # right now, for running on machine with no acess to IPN, we manually update statuses
@ -181,7 +182,7 @@ class PledgeTest(TestCase):
self.validateRedirect(t, url, 1) self.validateRedirect(t, url, 1)
def tearDown(self): def tearDown(self):
self.selenium.stop() self.selenium.quit()
class AuthorizeTest(TestCase): class AuthorizeTest(TestCase):
@ -190,9 +191,8 @@ class AuthorizeTest(TestCase):
# This is an empty array where we will store any verification errors # This is an empty array where we will store any verification errors
# we find in our tests # we find in our tests
self.selenium = selenium("localhost", 4444, "*firefox", self.selenium = webdriver.Firefox()
"http://www.google.com/") set_test_logging()
self.selenium.start()
def validateRedirect(self, t, url): def validateRedirect(self, t, url):
@ -221,7 +221,7 @@ class AuthorizeTest(TestCase):
self.validateRedirect(t, url) self.validateRedirect(t, url)
loginSandbox(self, self.selenium) loginSandbox(self, self.selenium)
authorizeSandbox(self, self.selenium, url) paySandbox(self, self.selenium, url, authorize=True)
# stick in a getStatus to update statuses in the absence of IPNs # stick in a getStatus to update statuses in the absence of IPNs
p.checkStatus() p.checkStatus()
@ -231,7 +231,7 @@ class AuthorizeTest(TestCase):
self.assertEqual(t.status, IPN_PAY_STATUS_ACTIVE) self.assertEqual(t.status, IPN_PAY_STATUS_ACTIVE)
def tearDown(self): def tearDown(self):
self.selenium.stop() self.selenium.quit()
class TransactionTest(TestCase): class TransactionTest(TestCase):
def setUp(self): def setUp(self):