Advertisement
vojd

selenium_test_example

Nov 15th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. import importlib
  2. import time
  3. from django.core.urlresolvers import reverse
  4. from django.test import LiveServerTestCase
  5. from selenium.webdriver.firefox.webdriver import WebDriver
  6. from selenium.webdriver.support.wait import WebDriverWait
  7. from apps.shop.catalogue.models import Product
  8.  
  9. class BaseTestCase(LiveServerTestCase):
  10.  
  11.     @classmethod
  12.     def setUpClass(cls):
  13.         settings.SESSION_ENGINE = 'django.contrib.sessions.backends.file'
  14.         engine = importlib.import_module('django.contrib.sessions.backends.file')
  15.         store = engine.SessionStore()
  16.         store.save()
  17.         cls.session = store
  18.  
  19.         cls.driver = WebDriver()
  20.         super(BaseTestCase, cls).setUpClass()
  21.  
  22.     @classmethod
  23.     def tearDownClass(cls):
  24.         super(BaseTestCase, cls).tearDownClass()
  25.         cls.driver.quit()
  26.  
  27.     def wait_until(self, callback, timeout=10):
  28.         """ Helper function which blocks test execution until
  29.            specified callback returns a non falsy value.
  30.            Can be called after clicking a link or submitting a form
  31.        """
  32.         WebDriverWait(self.driver, timeout).until(callback)
  33.  
  34.     def wait_loaded_tag(self, tag_name, timeout=10):
  35.         """ Block test execution until element with given tag name is found on page """
  36.         self.wait_until(
  37.             lambda driver : driver.find_element_by_tag_name(tag_name),
  38.             timeout
  39.         )
  40.  
  41.  
  42. class CheckoutFormTestCase(BaseTestCase):
  43.  
  44.     """ Selenium test for checkout procedure """
  45.  
  46.     def test_checkout(self):
  47.         """ Test the checkout form """
  48.  
  49. #        self.driver.get(self.live_server_url + '/cart/checkout/')
  50.         self.driver.get('http://127.0.0.1:8001/cart/checkout/')
  51.         print 'starting test'
  52.         f = self.driver.find_element_by_id
  53.         f('id_email').send_keys('[email protected]')
  54.         f('id_firstname').send_keys('Michail')
  55.         f('id_lastname').send_keys('Bakunin')
  56.         f('id_address').send_keys('Prjamuchino')
  57.         f('id_address_extra').send_keys('Tver Province')
  58.         f('id_post_code').send_keys('12345')
  59.         f('id_city').send_keys('Tver')
  60.         f('id_phone_primary').send_keys('+46123456')
  61.         f('id_phone_secondary').send_keys('+46123456')
  62.  
  63.         # accept terms of agreement
  64.         accept = self.driver.find_elements_by_name('accept')[0]
  65.         accept.click()
  66.  
  67.         # submit the form
  68.         accept.submit()
  69.         ##f('id_submit').click()
  70.  
  71.         print "form submitted, time to sleep for a bit!"
  72.         time.sleep(2)
  73.         windows = self.driver.window_handles
  74.         print 'window_handles'
  75.         print windows
  76.         # switch
  77.         self.driver.switch_to_window(windows[0])
  78.         print 'switching to window', windows[0], 'and sleeping'
  79.         time.sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement