Back to index

plone3  3.1.7
seleniumtestcase.py
Go to the documentation of this file.
00001 import os
00002 import unittest
00003 import time
00004 
00005 from selenium import selenium
00006 try:
00007     browser = os.environ["SELENIUMBROWSER"]
00008 except:
00009     browser = "*firefox"
00010 try:
00011     target = os.environ["SELENIUMTARGET"]
00012 except:
00013     target = "http://localhost:8080"
00014 
00015 
00016 def trymanytimes(func):
00017     '''Decorate a test method with this to make it try for ten seconds'''
00018     def newfunc(*args, **kwargs):
00019         exc = None
00020         for i in range(10):
00021             try:
00022                 func(*args, **kwargs)
00023             except Exception, e:
00024                 exc = e
00025                 time.sleep(1)
00026             else:
00027                 return
00028         raise exc
00029     return newfunc
00030 
00031 class SeleniumTestCase(unittest.TestCase):
00032     def setUp(self):
00033         self.verificationErrors = []
00034         self.selenium = selenium("localhost", 4444, browser, target)
00035         self.selenium.start()
00036         self.storedvars = {}
00037 
00038     def tearDown(self):
00039         self.selenium.stop()
00040         self.assertEqual([], self.verificationErrors)
00041 
00042     def __getattr__(self, name):
00043         try:
00044             return getattr(self.__dict__['selenium'], name)
00045         except KeyError:
00046             return super(SeleniumTestCase, self).__getattr__(name)
00047 
00048     def open(self, url):
00049         self.selenium.open(url)
00050 
00051     def click(self, target):
00052         self.selenium.click(target)
00053 
00054     @trymanytimes
00055     def waitForText(self, target, text):
00056         self.failUnless(text in self.selenium.get_text(target))
00057 
00058     @trymanytimes
00059     def waitForNotText(self, target, text):
00060         self.failIf(text in self.selenium.get_text(target))
00061 
00062     @trymanytimes
00063     def waitForTextPresent(self, text):
00064         self.failUnless(self.selenium.is_text_present(text))
00065 
00066     @trymanytimes
00067     def waitForElementNotPresent(self, target):
00068         self.failIf(self.selenium.is_element_present(target))
00069 
00070     @trymanytimes
00071     def waitForElementPresent(self, target):
00072         self.failUnless(self.selenium.is_element_present(target))
00073 
00074     @trymanytimes
00075     def waitForValue(self, target, value):
00076         self.failUnless(self.selenium.get_value(target))
00077 
00078     @trymanytimes
00079     def waitForAttribute(self, target, value):
00080         self.assertEqual(self.selenium.get_attribute(target), value)
00081 
00082     def assertText(self, target, text=''):
00083         self.assertEqual(self.selenium.get_text(target), text)
00084 
00085     def assertNotText(self, target, text=''):
00086         self.failIfEqual(self.selenium.get_text(target), text)
00087 
00088     def assertValue(self, target, value=''):
00089         self.assertEqual(self.selenium.get_value(target), value)
00090 
00091     def assertElementPresent(self, target):
00092         self.failUnless(self.selenium.is_element_present(target))
00093 
00094     def assertElementNotPresent(self, target):
00095         self.failIf(self.selenium.is_element_present(target))
00096 
00097     def pause(self, seconds):
00098         time.sleep(float(seconds)/1000)
00099 
00100     def assertTextPresent(self, text):
00101         self.failUnless(self.selenium.is_text_present(text))
00102 
00103     def assertTextNotPresent(self, text):
00104         self.failIf(self.selenium.is_text_present(text))
00105         
00106     def assertAttribute(self, target, value):
00107         self.assertEqual(self.selenium.get_attribute(target), value)
00108 
00109     def storeText(self, target, varname):
00110         self.storedvars[varname] = self.selenium.get_text(target)
00111 
00112     def getVar(self, varname):
00113         return self.storedvars[varname]