import socket
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, NoAlertPresentException
from selenium.webdriver.common.by import By
class SeleniumTest( ):
def __init__( self, executable_path=None, port=None ):
if not executable_path:
self.driver = webdriver.Firefox() # Development and Debugging
elif port:
self.driver = webdriver.PhantomJS( executable_path=executable_path, port=port )
else:
self.driver = webdriver.Firefox( firefox_binary=executable_path ) # Development and Debugging
self.driver.implicitly_wait( 10 )
@staticmethod
def is_valid_connection( host, port ):
try:
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.settimeout(10)
s.connect( ( host, port ) )
s.close()
except Exception:
return False
else:
return True
def assert_is_equal( self, a, b):
if a != b:
raise AssertionError( a + ' does not equal ' + b )
def is_alert_present( self ):
try:
self.driver.switch_to_alert( )
except NoAlertPresentException:
return False
return True
# a series of helper methods to improve readability and abstract the selenium webdriver implementation
def assert_id_is_present( self, value ):
self.find( By.ID, value )
def assert_name_is_present( self, value ):
self.find( By.NAME, value )
def assert_link_text_is_present( self, value ):
self.find( By.LINK_TEXT, value )
def assert_css_selector_is_present( self, value ):
self.find( By.CSS_SELECTOR, value )
def find_by_id( self, value ):
return self.find( By.ID, value )
def find_by_name( self, value ):
return self.find( By.NAME, value )
def find_by_link_text( self, value ):
return self.find( By.LINK_TEXT, value )
def find_by_css_selector( self, value ):
return self.find( By.CSS_SELECTOR, value )
def find_by_xpath( self, value ):
return self.find( By.XPATH, value )
def find( self, identifier, value ):
""" raising AssertionError when an element is not found allows the test framework to mark the error
"""
try:
return self.driver.find_element( by=identifier, value=value )
except NoSuchElementException:
raise AssertionError( identifier + ' with value: ' + value + ' not found' )
def show_current_url( self ):
return self.driver.current_url
def get_url( self, url ):
self.driver.get( url )
def execute_javascript( self, script ):
self.driver.execute_script( script )
def quit( self ):
self.driver.delete_all_cookies( ) # important to not persist sessions between tests
self.driver.quit( )
def sign_up( self, base_url, group_name, email, password ):
self.get_url( base_url + '/sign_up' )
self.find_by_id( 'name' ).clear( )
self.find_by_id( 'name' ).send_keys( 'admin user' )
self.find_by_id( 'email' ).clear( )
self.find_by_id( 'email' ).send_keys( email )
self.find_by_id( 'title' ).clear( )
self.find_by_id( 'title' ).send_keys( 'admin' )
self.find_by_id( 'password' ).clear( )
self.find_by_id( 'password' ).send_keys( password )
self.find_by_id( 'groupname' ).clear( )
self.find_by_id( 'groupname' ).send_keys( group_name )
self.find_by_id( 'subdomain' ).clear( )
self.find_by_id( 'subdomain' ).send_keys( group_name )
self.find_by_id( 'sign_up' ).click( )
def check_external_email(self):
if new_user_email == 'myexample@mailinator.com':
page.driver.get('mailinator.com/inbox.jsp?to=myexample')
element = page.driver.find_element_by_css_selector("div.subject.ng-binding")
self.assertEqual("You're invited!", element.text)
element.click()
page.driver.switch_to.frame("rendermail")
element = page.driver.find_element_by_css_selector("a.button-link > font")
self.assertEqual("Join by clicking this link!", element.text)
element.click()
# driver.switchTo().defaultContent(); # outside both frames
# driver.switchTo().frame(driver.findElements(By.tagName("iframe").get(0));
# http://selenium-python.readthedocs.org/en/latest/navigating.html#moving-between-windows-and-frames
def sign_in( self, base_url, email, password ):
self.get_url( base_url + '/sign_in' )
self.find_by_id( 'email' ).clear( )
self.find_by_id( 'email' ).send_keys( email )
self.find_by_id( 'password' ).clear( )
self.find_by_id( 'password' ).send_keys( password )
self.find_by_id( 'signin' ).click( )
def sign_out( self, base_url ):
#self.get_url( base_url + '/sign_out' )
#self.assert_is_present( By.ID, 'signout' )
#self.find_by_id( 'signout' ).click( )
self.driver.delete_all_cookies( ) # effectively logging out
def add_user( self, new_user_name, new_user_email ):
self.find_by_id( 'name' ).clear( )
self.find_by_id( 'name' ).send_keys( new_user_name )
self.find_by_id( 'email' ).clear( )
self.find_by_id( 'email' ).send_keys( new_user_email )
self.find_by_id( 'do' ).click( )
def delete_user( self, user_name ):
self.find_by_link_text( user_name ).click()
js_confirm = 'window.confirm = function(){return true;}' # .js function to confirm a popup
self.execute_javascript( js_confirm )
self.find_by_xpath( "(//input[@id='do'])[2]" ).click() # the delete button by xpath
self.execute_javascript( 'return window.confirm' ) # trigger the injected js that returns true (virtually click OK)
#alert_text = selenium_wrapper.close_alert_and_get_its_text() # currently unsupported by phantomjs and webdriver
#print 'DEBUG: alert text: {}'.format( alert_text )
#self.assertEqual( alert_text, 'This account will be deleted and the user will not be able to sign into it. Chat messages sent by this user will remain searchable.' )
def create_room( self, name ):
""" creates a room (using javascript injection)
"""
self.find_by_link_text( 'Lobby' ).click()
self.find_by_id( 'create_room_button' ).click()
self.execute_javascript( 'document.getElementById("new_room_name").value="' + name + '";' )
self.execute_javascript( 'go=document.getElementById("submit_new");' )
self.execute_javascript( 'go.click();' )
def add_emoticon( self, emoticon_shortcut_text, local_file_path ):
self.find_by_id( 'shortcut' ).clear( )
self.find_by_id( 'shortcut' ).send_keys( emoticon_shortcut_text )
self.find_by_id( 'Filedata' ).click( )
self.find_by_id( 'Filedata' ).send_keys( local_file_path )
self.find_by_id( 'submit_emoticon' ).click( )