import sys
import time
import unittest
from selenium.webdriver.common.by import By
from SeleniumTest import SeleniumTest # separate out some of the low level driver details
class DeleteUserTest( unittest.TestCase ):
def setUp( self ):
self.verificationErrors = []
def tearDown( self ):
selenium_wrapper.quit()
self.assertEqual( [], self.verificationErrors )
def test_javascript_confirmation( self ):
driver = selenium_wrapper.driver # shorter alias for readability
driver.get( BASE_URL )
"""
# https://github.com/detro/ghostdriver/issues/20
# http://stackoverflow.com/questions/15708518/how-can-i-handle-an-alert-with-ghostdriver-via-python
js = 'window.alert = function(message) { lastAlert = message; } ' # global variable storing the message
driver.execute_script( "%s" % js ) # injects the javascript function into the page
driver.find_element_by_css_selector( 'input[type="button"]' ).click()
result = driver.execute_script("return lastAlert") # retrieve the result from the global variable
print result
"""
js_confirm = 'window.confirm = function(){return true;}' # function to confirm a .js popup
driver.execute_script( '%s'% js_confirm ) # inject the function
driver.find_element_by_css_selector( 'input[type="submit"]' ).click()
driver.execute_script( 'return window.confirm' ) # trigger the injected js that returns true (virtually click OK)
if __name__ == '__main__':
if len( sys.argv ) != 4:
print "usage: python {} /directory-with-binary/phantomjs 9134 example.com".format( sys.argv[0] )
sys.exit( 1 )
phantomjs_binary = sys.argv[1] # '/tmp/phantomjs-1.9.2-linux-x86_64/bin/phantomjs'
phantomjs_port = int( sys.argv[2] ) # 9134
hostname = sys.argv[3]
BASE_URL = hostname # defined in main, available to the whole namespace
del sys.argv[ 1: ] # since sys.argv[0] is the module name, remove everything after
if not SeleniumTest.is_valid_connection( 'localhost', phantomjs_port ):
print "ERROR: unable to connect to localhost on port {}".format( phantomjs_port )
sys.exit( 1 )
#selenium_wrapper = SeleniumTest( executable_path=phantomjs_binary, port=phantomjs_port)
selenium_wrapper = SeleniumTest() # Firefox
unittest.main( )