1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
58 if self.selenium is not None:
59 self.selenium.stop()
60
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
93 @staticmethod
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
128
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
142
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
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
169 self.selenium.stop()
170 self.assertEqual([], self.verificationErrors)
171