#! /usr/bin/env python
#Author: Xie Tian
#coding=utf-8
import time
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import os.path
import logging
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
#basepage class contains basic methods for Browser UI Testing
class BasePage(object):
#Initial the driver maybe IE ,Firefox, Chrome
def __init__(self, driver):
self.driver = driver
# quit browser and end testing
def quit_browser(self):
self.driver.quit()
def launchURL(self,url):
self.driver.get(url)
# close the browser but didn't quit the driver
def close(self):
try:
self.driver.close()
logging.info("Closing and quit the browser.")
except NameError as e:
logging.error("Failed to quit the browser with %s" % e)
def forward(self):
self.driver.forward()
logging.info("Click forward on current page.")
def back(self):
self.driver.back()
logging.info("Click back button on current page.")
def refresh(self):
self.driver.refresh()
logging.info("Click the refresh button on current page.")
def maximize_window(self):
driver.maximize_window()
logging.info("maximize the window")
#wait method, sometimes we need to waith the action complete with specified seconds
def wait(self, seconds):
self.driver.implicitly_wait(seconds)
logging.info("wait for %d seconds." % seconds)
def capture_browser_screenshot(self,file_path):
self.file_path = file_path
log_time = time.strftime('%Y%m%d%H%M', time.localtime(time.time()))
screen_name = file_path + log_time + '.png'
try:
self.driver.get_screenshot_as_file(screen_name)
logging.info("The captured screenshot saved in "%screen_name)
except NameError as e:
logging.error("Failed to take screenshot! %s" % e)
self.get_windows_img()
# find element with different conditions , e.g. search by ID, name, CSS ,XPath
def find_element(self, selector):
element = ''
if '=>' not in selector:
return self.driver.find_element_by_id(selector)
selector_by = selector.split('=>')[0]
selector_value = selector.split('=>')[1]
for case in switch(selector_by.lower()):
if case('id'):
try:
element = self.driver.find_element_by_id(selector_value)
logging.info("Find the element \' %s \' successful "
"by %s via value: %s " % (element.text, selector_by, selector_value))
except NoSuchElementException as e:
logging.error("NoSuchElementException: %s" % e)
self.get_windows_img() # take screenshot
break
if case('name'):
element = self.driver.find_element_by_name(selector_value)
break
if case('xpath'):
try:
element = self.driver.find_element_by_xpath(selector_value)
logging.info("Find the element \' %s \' successful "
"by %s via value: %s " % (element.text, selector_by, selector_value))
except NoSuchElementException as e:
logging.error("NoSuchElementException: %s" % e)
self.get_windows_img()
break
if case('css'):
element = self.driver.find_element_by_css_selector(selector_value)
break
if case('classname'):
element = self.driver.find_element_by_class_name(selector_value)
break
if case('linktext'):
element = self.driver.find_element_by_link_text(selector_value)
break
if case('linkname'):
element = self.driver.find_element_link_name(selector_value)
break
if case('partiallinktext'):
element = self.driver.find_element_by_partial_link_text(selector_value)
break
if case('tagname'):
element = self.driver.find_element_by_tag_name(selector_value)
break
if case(): # default, could also just omit condition or 'if True'
print "something else: the type input is incocrect !"
# No need to break here, it'll stop anyway
return element
# type method to input something
def type(self, selector, text):
el = self.find_element(selector)
el.clear()
try:
el.send_keys(text)
logging.info("Type \' %s \' in editbox" % text)
except NameError as e:
logging.error("Failed to type in the editbox with %s" % e)
self.get_windows_img()
# clear the content in the editbox
def clear(self, selector):
el = self.find_element(selector)
try:
el.clear()
logging.info("Clear text in editbox before typing.")
except NameError as e:
logging.error("Failed to clear content in editbox with %s" % e)
self.get_windows_img()
#delete_all_cookies()
def delete_all_cookies(self):
driver.delete_all_cookies()
#delete specify cookie
def delete_specify_cookies(self,my_cookie_name):
driver.delete_cookie(my_cookie_name)
# perform the click action
def click(self, selector):
el = self.find_element(selector)
try:
el.click()
logging.info("The element \' %s \' was clicked." % el.text)
except NameError as e:
logging.error("Failed to click the element with %s" % e)
# get the page title
def get_page_title(self):
logging.info("Current page title is %s" % self.driver.title)
return self.driver.title
@staticmethod
def sleep(seconds):
time.sleep(seconds)
logging.info("Sleep for %d seconds" % seconds)