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

Source Code for Module omeroweb.webgateway.tests.seleniumbase

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