/
uibasetest.py
294 lines (263 loc) · 11.8 KB
/
uibasetest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import logger
import time
import unittest
import os
import urllib2
import commands
import types
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import WebDriverWait, Select
from selenium.common.exceptions import NoSuchElementException
from threading import Thread
import ConfigParser
from TestInput import TestInputSingleton,TestInputParser, TestInputServer
from couchbase.cluster import Cluster
from remote.remote_util import RemoteMachineShellConnection
from membase.api.rest_client import RestConnection
from membase.helper.bucket_helper import BucketOperationHelper
from membase.helper.cluster_helper import ClusterOperationHelper
class BaseUITestCase(unittest.TestCase):
skip_setup_failed = False
# selenium thread
def _start_selenium(self):
host = self.machine.ip
if host in ['localhost', '127.0.0.1']:
os.system("java -jar %sselenium-server-standalone*.jar -Dwebdriver.chrome.driver=%s > selenium.log 2>&1"
% (self.input.ui_conf['selenium_path'], self.input.ui_conf['chrome_path']))
else:
self.shell.execute_command('{0}start-selenium.bat > {0}selenium.log 2>&1 &'.format(self.input.ui_conf['selenium_path']))
def _wait_for_selenium_is_started(self, timeout=10):
if self.machine.ip in ['localhost', '127.0.0.1']:
start_time = time.time()
while (time.time() - start_time) < timeout:
log = open("/tmp/selenium.log")
if log.read().find('Started org.openqa.jetty.jetty.Server') > -1:
log.close()
if self._is_selenium_running():
time.sleep(1)
return
time.sleep(1)
else:
time.sleep(timeout)
def _start_selenium_thread(self):
self.t = Thread(target=self._start_selenium,
name="selenium",
args=())
self.t.start()
def _is_selenium_running(self):
host = self.machine.ip
if host in ['localhost', '127.0.0.1']:
cmd = 'ps -ef|grep selenium-server'
output = commands.getstatusoutput(cmd)
if str(output).find('selenium-server-standalone') > -1:
return True
else:
#cmd = "ssh {0}@{1} 'bash -s' < 'tasklist |grep selenium-server'".format(self.input.servers[0].ssh_username,
# host)
cmd = 'tasklist |grep java'
o, r = self.shell.execute_command(cmd)
#cmd = "ssh {0}@{1} 'bash -s' < 'ps -ef|grep selenium-server'"
if str(o).find('java') > -1:
return True
return False
@unittest.skipIf(skip_setup_failed, "setup was failed")
def setUp(self):
try:
self.log = logger.Logger.get_logger()
self.input = TestInputSingleton.input
self.servers = self.input.servers
self.browser = self.input.ui_conf['browser']
self.replica = self.input.param("replica", 1)
self.case_number = self.input.param("case_number", 0)
self.cluster = Cluster()
self.machine = self.input.ui_conf['server']
self.shell = RemoteMachineShellConnection(self.machine)
#avoid clean up if the previous test has been tear down
if not self.input.param("skip_cleanup", True) \
or self.case_number == 1:
self.tearDown()
self._log_start(self)
#thread for selenium server
if not self._is_selenium_running():
self.log.info('start selenium')
self._start_selenium_thread()
self._wait_for_selenium_is_started()
self.log.info('start selenium session')
if self.browser == 'ff':
self.driver = webdriver.Remote(command_executor='http://{0}:{1}/wd/hub'
.format(self.machine.ip,
self.machine.port),
desired_capabilities=DesiredCapabilities.FIREFOX)
elif self.browser == 'chrome':
self.driver = webdriver.Remote(command_executor='http://{0}:{1}/wd/hub'
.format(self.machine.ip,
self.machine.port),
desired_capabilities=DesiredCapabilities.CHROME)
self.log.info('start selenium started')
self.driver.get("http://{0}:{1}".format(self.servers[0].ip,
self.servers[0].port))
self.driver.maximize_window()
except Exception as ex:
skip_setup_failed = True
self.fail(ex)
@staticmethod
def _log_start(self):
try:
msg = "{0} : {1} started ".format(datetime.datetime.now(),
self._testMethodName)
RestConnection(self.servers[0]).log_client_error(msg)
except:
pass
@staticmethod
def _log_finish(self):
try:
msg = "{0} : {1} finished ".format(datetime.datetime.now(),
self._testMethodName)
RestConnection(self.servers[0]).log_client_error(msg)
except:
pass
def tearDown(self):
try:
path_screen = self.input.ui_conf['screenshots'] or 'logs/screens'
full_path = '{1}/screen_{0}.png'.format(time.time(), path_screen)
self.log.info('screenshot is available: %s' % full_path)
if not os.path.exists(path_screen):
os.mkdir(path_screen)
self.driver.get_screenshot_as_file(os.path.abspath(full_path))
rest = RestConnection(self.servers[0])
if rest._rebalance_progress_status() == 'running':
stopped = rest.stop_rebalance()
self.assertTrue(stopped, msg="unable to stop rebalance")
BucketOperationHelper.delete_all_buckets_or_assert(self.servers, self)
#for server in self.servers:
# ClusterOperationHelper.cleanup_cluster([server])
#ClusterOperationHelper.wait_for_ns_servers_or_assert(self.servers, self)
self.driver.close()
finally:
self.shell.disconnect()
self.cluster.shutdown()
class Control():
def __init__(self, selenium, by=None, web_element=None):
self.selenium = selenium
self.by = by
if by:
try:
self.web_element = self.selenium.find_element_by_xpath(by)
self.present = True
if self.web_element is None:
self.present = False
except NoSuchElementException as ex:
self.present = False
else:
self.web_element = web_element
self.present = True
def highlightElement(self):
if self.by:
self.selenium.execute_script("document.evaluate(\"{0}\",document,null,XPathResult.ANY_TYPE, null).iterateNext().setAttribute('style','background-color:yellow');".format(self.by))
def type_native(self, text):
ActionChains(self.selenium).click(self.web_element).perform()
ActionChains(self.selenium).key_down(Keys.CONTROL).perform()
ActionChains(self.selenium).send_keys('a').perform()
ActionChains(self.selenium).key_up(Keys.CONTROL).perform()
ActionChains(self.selenium).send_keys(Keys.DELETE).perform()
ActionChains(self.selenium).send_keys(text).perform()
def click(self):
self.highlightElement()
self.web_element.click()
def type(self, message):
if message:
self.highlightElement()
self.web_element.clear()
if type(message) == types.StringType and message.find('\\') > -1:
for symb in list(message):
if symb == '\\':
self.web_element.send_keys(Keys.DIVIDE)
else:
self.web_element.send_keys(symb)
else:
self.web_element.send_keys(message)
def check(self, setTrue=True):
if setTrue:
if not self.is_checked():
self.click()
else:
if self.is_checked():
self.click()
def is_present(self):
return self.present
def is_displayed(self):
return self.present and self.web_element.is_displayed()
def is_checked(self):
checked = self.web_element.get_attribute("checked")
return checked is not None
def get_text(self):
self.highlightElement()
return self.web_element.text
def get_attribute(self, atr):
return self.web_element.get_attribute(atr)
def select(self, label):
element = Select(self.web_element)
element.select_by_visible_text(label)
def mouse_over(self):
ActionChains(self.selenium).move_to_element(self.web_element).perform()
class ControlsHelper():
def __init__(self, driver):
self.driver = driver
file = "pytests/uilocators.conf"
config = ConfigParser.ConfigParser()
config.read(file)
self.locators = config
def find_control(self, section, locator, parent_locator=None, text=None):
by = self._find_by(section, locator, parent_locator)
if text:
by = by.format(text)
return Control(self.driver, by=by)
def find_controls(self, section, locator, parent_locator=None):
by = self._find_by(section, locator, parent_locator)
controls = []
elements = self.driver.find_elements_by_xpath(by)
for element in elements:
controls.append(Control(self.driver, web_element=element))
return controls
def find_first_visible(self, section, locator, parent_locator=None, text=None):
by = self._find_by(section, locator, parent_locator)
if text:
by = by.format(text)
controls = []
elements = self.driver.find_elements_by_xpath(by)
for element in elements:
try:
if element.is_displayed():
return Control(self.driver, web_element=element)
except StaleElementReferenceException:
pass
return None
def _find_by(self, section, locator, parent_locator=None):
if parent_locator:
return self.locators.get(section, parent_locator) + \
self.locators.get(section, locator)
else:
return self.locators.get(section, locator)
class BaseHelperControls():
def __init__(self, driver):
helper = ControlsHelper(driver)
self._user_field = helper.find_control('login', 'user_field')
self._user_password = helper.find_control('login', 'password_field')
self._login_btn = helper.find_control('login', 'login_btn')
class BaseHelper():
def __init__(self, tc):
self.tc = tc
self.controls = BaseHelperControls(tc.driver)
def login(self, user = None, password = None):
self.tc.log.info("Try to login to application")
if not user:
user = self.tc.input.membase_settings.rest_username
if not password:
password = self.tc.input.membase_settings.rest_password
self.controls._user_field.type(user)
self.controls._user_password.type(password)
self.controls._login_btn.click()
self.tc.log.info("user %s is logged in" % user)