Package omeroweb :: Package webgateway :: Package tests :: Module seleniumbase
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.webgateway.tests.seleniumbase

  1  # 
  2  # webgateway/tests/seleniumbase.py - Helpers to implement selenium tests 
  3  #  
  4  # Copyright (c) 2010 Glencoe Software, Inc. All rights reserved. 
  5  #  
  6  # This software is distributed under the terms described by the LICENCE file 
  7  # you can find at the root of the distribution bundle, which states you are 
  8  # free to use it only for non commercial purposes. 
  9  # If the file is missing please request a copy by contacting 
 10  # jason@glencoesoftware.com. 
 11  # 
 12  # Author: Carlos Neves <carlos(at)glencoesoftware.com> 
 13  """  
 14  Base class and utils to ease implementation of selenium tests. 
 15   
 16  To use this, create a file called C{seleniumtests.py} inside your app C{tests} folder, 
 17  and include the following template:: 
 18   
 19    from omeroweb.webgateway.tests.seleniumbase import SeleniumTestBase, Utils 
 20     
 21    class MyTests (SeleniumTestBase): 
 22        def runTest (self): 
 23            " Implement your tests here " 
 24     
 25    if __name__ == "__main__": 
 26        Utils.runAsScript('MyDjangoAppURLPrefix') 
 27   
 28  Of course you'll need to replace C{MyDjangoAppURLPrefix} with a real value, and you 
 29  can implement tests in any way you want, so as long as unittest can run them. 
 30   
 31  By extending L{SeleniumTestBase} you get an instance variable names C{selenium} which 
 32  has the selenium RC client connected. 
 33   
 34  After all this is done, and assuming you have a standard C{Omero} installation, you'll 
 35  be able to issue: 
 36   
 37    C{omero web seleniumtest MyDjangoApp seleniumserver.host http://omeroweb.host:port firefox} 
 38   
 39  The values above are just examples. 
 40  """  
 41  __docformat__='epytext'  
 42   
 43  from selenium import selenium 
 44  import unittest, time, urllib2, cookielib 
45 46 -class SeleniumTestServer (object):
47 - def __init__ (self, base='webgateway', url='http://localhost:8000', host='localhost', port=4444, browser='*firefox'):
48 self.base = base 49 self.host = host 50 self.port = port 51 self.browser = browser 52 self.url = url 53 self.selenium = None
54
55 - def __del__ (self):
56 if self.selenium is not None: 57 self.selenium.stop()
58
59 - def getSelenium (self):
60 if self.selenium is None: 61 if self.host.find(':') >= 0: 62 host = self.host.split(':') 63 try: 64 self.port = int(host[1]) 65 if host[0] != '': 66 self.host = host[0] 67 except: 68 pass 69 self.selenium = selenium(self.host, self.port, self.browser, self.url) 70 self.selenium.start() 71 self.selenium.open("/%s" % self.base) 72 script = urllib2.urlopen("%s/appmedia/webgateway/js/3rdparty/jquery-1.3.2.js" % (self.url)) 73 r = script.read() 74 self.selenium.run_script(r) 75 script.close() 76 self.selenium.add_location_strategy("jquery", 77 ''' 78 if (inWindow.jQuery.expr[':'].containsExactly === undefined) { 79 inWindow.jQuery.expr[':'].containsExactly = function (a,i,m) { return inWindow.jQuery(a).text() == m[3];}; 80 } 81 var found = inWindow.jQuery(inDocument).find(locator); 82 if(found.length >= 1 ){ 83 return found.get(0); 84 }else{ 85 return null; 86 } 87 ''') 88 return self.selenium
89
90 -class Utils:
91 @staticmethod
92 - def flattenHTMLStyleColor (color):
93 """ Takes a color in rgb() or #ABC or #AABBCC format and returns #AABBCC. 94 There may be a prefix from the style attribute. 95 TODO: support multiple style attrs, find the correct one.""" 96 if color.endswith(';'): 97 color = color[:-1] 98 if color.find(':') >= 0: 99 color = color.split(':')[-1].strip() 100 if color.startswith('rgb('): 101 r,g,b = [int(x) for x in color[4:-1].split(',')] 102 else: 103 if color.startswith('#'): 104 color = color[1:] 105 if len(color) == 3: 106 r,g,b = color[0]*2,color[1]*2,color[2]*2 107 elif len(color) == 6: 108 r,g,b = color[0:2],color[2:4],color[4:6] 109 else: 110 return '#' + color 111 r,g,b = [int(x, 16) for x in (r,g,b)] 112 return '#%0.2X%0.2X%0.2X' % (r,g,b)
113 114 @staticmethod
115 - def runAsScript (base='webgateway'):
116 import sys 117 SeleniumTestBase.SERVER = SeleniumTestServer(base) 118 #print sys.argv 119 if len(sys.argv) > 1: 120 SeleniumTestBase.SERVER.host = sys.argv.pop(1) 121 if len(sys.argv) > 1: 122 SeleniumTestBase.SERVER.url = sys.argv.pop(1) 123 if len(sys.argv) > 1: 124 SeleniumTestBase.SERVER.browser = sys.argv.pop(1) 125 unittest.main()
126
127 -class SeleniumTestBase (unittest.TestCase):
128 """ 129 The base class for selenium tests. 130 131 All tests will have the C{self.selenium} attr with a selenium client ready for usage. 132 """ 133 SERVER = None 134
135 - def setUp (self):
136 self.verificationErrors = [] 137 if self.SERVER is None: 138 self.SERVER = SeleniumTestServer() 139 self.selenium = self.SERVER.getSelenium()
140
141 - def waitForElementPresence (self, element, present=True):
142 """ 143 Waits for 60 seconds for a particular element to be present (or not). 144 Also exits if an alert box comes up. 145 """ 146 for i in range(60): 147 try: 148 if self.selenium.is_element_present(element) == present: break 149 except: pass 150 time.sleep(1) 151 else: self.fail("time out")
152
153 - def waitForElementVisibility (self, element, visible):
154 """ 155 Waits for 60 seconds for a particular element to be visible (or not). 156 """ 157 for i in range(60): 158 try: 159 if self.selenium.is_visible(element) == visible: break 160 except: 161 if not visible: 162 break 163 time.sleep(1) 164 else: self.fail("time out")
165
166 - def tearDown(self):
167 self.selenium.stop() 168 self.assertEqual([], self.verificationErrors)
169