@@ -1372,6 +1372,88 @@ def select_option_by_value(self, dropdown_selector, option,
1372
1372
dropdown_by = dropdown_by , option_by = "value" ,
1373
1373
timeout = timeout )
1374
1374
1375
+ def load_html_string (self , html_string , new_page = True ):
1376
+ """ Loads an HTML string into the web browser.
1377
+ If new_page==True, the page will switch to: "data:text/html,"
1378
+ If new_page==False, will load HTML into the current page. """
1379
+
1380
+ soup = self .get_beautiful_soup (html_string )
1381
+ scripts = soup .findAll ("script" )
1382
+ for script in scripts :
1383
+ html_string = html_string .replace (str (script ), "" )
1384
+ soup = self .get_beautiful_soup (html_string )
1385
+
1386
+ found_head = False
1387
+ found_body = False
1388
+ html_head = None
1389
+ html_body = None
1390
+ if soup .head and len (str (soup .head )) > 12 :
1391
+ found_head = True
1392
+ html_head = str (soup .head )
1393
+ html_head = re .escape (html_head )
1394
+ html_head = self .__escape_quotes_if_needed (html_head )
1395
+ html_head = html_head .replace ('\\ ' , ' ' )
1396
+ if soup .body and len (str (soup .body )) > 12 :
1397
+ found_body = True
1398
+ html_body = str (soup .body )
1399
+ html_body = re .escape (html_body )
1400
+ html_body = self .__escape_quotes_if_needed (html_body )
1401
+ html_body = html_body .replace ('\\ ' , ' ' )
1402
+ html_string = re .escape (html_string )
1403
+ html_string = self .__escape_quotes_if_needed (html_string )
1404
+ html_string = html_string .replace ('\\ ' , ' ' )
1405
+
1406
+ if new_page :
1407
+ self .open ("data:text/html," )
1408
+ inner_head = '''document.getElementsByTagName("head")[0].innerHTML'''
1409
+ inner_body = '''document.getElementsByTagName("body")[0].innerHTML'''
1410
+ if not found_body :
1411
+ self .execute_script (
1412
+ '''%s = \" %s\" ''' % (inner_body , html_string ))
1413
+ elif found_body and not found_head :
1414
+ self .execute_script (
1415
+ '''%s = \" %s\" ''' % (inner_body , html_body ))
1416
+ elif found_body and found_head :
1417
+ self .execute_script (
1418
+ '''%s = \" %s\" ''' % (inner_head , html_head ))
1419
+ self .execute_script (
1420
+ '''%s = \" %s\" ''' % (inner_body , html_body ))
1421
+ else :
1422
+ raise Exception ("Logic Error!" )
1423
+
1424
+ for script in scripts :
1425
+ js_code = script .string
1426
+ js_code_lines = js_code .split ('\n ' )
1427
+ new_lines = []
1428
+ for line in js_code_lines :
1429
+ line = line .strip ()
1430
+ new_lines .append (line )
1431
+ js_code = '\n ' .join (new_lines )
1432
+ js_utils .add_js_code (self .driver , js_code )
1433
+
1434
+ def load_html_file (self , html_file , new_page = True ):
1435
+ """ Loads a local html file into the browser from a relative file path.
1436
+ If new_page==True, the page will switch to: "data:text/html,"
1437
+ If new_page==False, will load HTML into the current page.
1438
+ Local images and other local src content WILL BE IGNORED. """
1439
+ if len (html_file ) < 6 or not html_file .endswith (".html" ):
1440
+ raise Exception ('Expecting a ".html" file!' )
1441
+ abs_path = os .path .abspath ('.' )
1442
+ file_path = abs_path + "/%s" % html_file
1443
+ f = open (file_path , 'r' )
1444
+ html_string = f .read ().strip ()
1445
+ f .close ()
1446
+ self .load_html_string (html_string , new_page )
1447
+
1448
+ def open_html_file (self , html_file ):
1449
+ """ Opens a local html file into the browser from a relative file path.
1450
+ The URL displayed in the web browser will start with "file://". """
1451
+ if len (html_file ) < 6 or not html_file .endswith (".html" ):
1452
+ raise Exception ('Expecting a ".html" file!' )
1453
+ abs_path = os .path .abspath ('.' )
1454
+ file_path = abs_path + "/%s" % html_file
1455
+ self .open ("file://" + file_path )
1456
+
1375
1457
def execute_script (self , script ):
1376
1458
return self .driver .execute_script (script )
1377
1459
@@ -1753,6 +1835,7 @@ def activate_jquery(self):
1753
1835
""" If "jQuery is not defined", use this method to activate it for use.
1754
1836
This happens because jQuery is not always defined on web sites. """
1755
1837
js_utils .activate_jquery (self .driver )
1838
+ self .wait_for_ready_state_complete ()
1756
1839
1757
1840
def __are_quotes_escaped (self , string ):
1758
1841
return js_utils .are_quotes_escaped (string )
@@ -1776,7 +1859,7 @@ def bring_to_front(self, selector, by=By.CSS_SELECTOR):
1776
1859
return
1777
1860
selector = re .escape (selector )
1778
1861
selector = self .__escape_quotes_if_needed (selector )
1779
- script = ("""document.querySelector('%s').style.zIndex = '9999 ';"""
1862
+ script = ("""document.querySelector('%s').style.zIndex = '999999 ';"""
1780
1863
% selector )
1781
1864
self .execute_script (script )
1782
1865
@@ -2340,32 +2423,34 @@ def assert_downloaded_file(self, file):
2340
2423
self .driver , messenger_post , self .message_duration )
2341
2424
2342
2425
def assert_true (self , expr , msg = None ):
2426
+ """ Asserts that the expression is True.
2427
+ Will raise an exception if the statement if False. """
2343
2428
self .assertTrue (expr , msg = msg )
2344
- '''if self.demo_mode:
2345
- messenger_post = ("ASSERT TRUE (See code)")
2346
- js_utils.post_messenger_success_message(
2347
- self.driver, messenger_post, self.message_duration)'''
2348
2429
2349
2430
def assert_false (self , expr , msg = None ):
2431
+ """ Asserts that the expression is False.
2432
+ Will raise an exception if the statement if True. """
2350
2433
self .assertFalse (expr , msg = msg )
2351
- '''if self.demo_mode:
2352
- messenger_post = ("ASSERT FALSE (See code)")
2353
- js_utils.post_messenger_success_message(
2354
- self.driver, messenger_post, self.message_duration)'''
2355
2434
2356
2435
def assert_equal (self , first , second , msg = None ):
2436
+ """ Asserts that the two values are equal.
2437
+ Will raise an exception if the values are not equal. """
2357
2438
self .assertEqual (first , second , msg = msg )
2358
- '''if self.demo_mode:
2359
- messenger_post = ("ASSERT EQUAL: {%s == %s}" % (first, second))
2360
- js_utils.post_messenger_success_message(
2361
- self.driver, messenger_post, self.message_duration)'''
2362
2439
2363
2440
def assert_not_equal (self , first , second , msg = None ):
2441
+ """ Asserts that the two values are not equal.
2442
+ Will raise an exception if the values are equal. """
2364
2443
self .assertNotEqual (first , second , msg = msg )
2365
- '''if self.demo_mode:
2366
- messenger_post = ("ASSERT NOT EQUAL: {%s != %s}" % (first, second))
2367
- js_utils.post_messenger_success_message(
2368
- self.driver, messenger_post, self.message_duration)'''
2444
+
2445
+ def assert_raises (self , * args , ** kwargs ):
2446
+ """ Asserts that the following block of code raises an exception.
2447
+ Will raise an exception if the block of code has no exception.
2448
+ Usage Example =>
2449
+ # Verify that the expected exception is raised.
2450
+ with self.assert_raises(Exception):
2451
+ raise Exception("Expected Exception!")
2452
+ """
2453
+ self .assertRaises (* args , ** kwargs )
2369
2454
2370
2455
def assert_title (self , title ):
2371
2456
""" Asserts that the web page title matches the expected title. """
@@ -2610,6 +2695,9 @@ def add_css_style(self, css_style):
2610
2695
def add_js_code_from_link (self , js_link ):
2611
2696
js_utils .add_js_code_from_link (self .driver , js_link )
2612
2697
2698
+ def add_js_code (self , js_code ):
2699
+ js_utils .add_js_code (self .driver , js_code )
2700
+
2613
2701
def add_meta_tag (self , http_equiv = None , content = None ):
2614
2702
js_utils .add_meta_tag (
2615
2703
self .driver , http_equiv = http_equiv , content = content )
0 commit comments