5
5
import json
6
6
import pytest
7
7
import polyplug
8
+ from unittest import mock
8
9
9
10
10
11
DOM_FROM_JSON = {
@@ -330,7 +331,7 @@ def test_element_node_get_outer_html():
330
331
"""
331
332
n = polyplug .ElementNode (tagName = "div" , attributes = {"foo" : "bar" })
332
333
n .add_child (polyplug .TextNode (nodeValue = "Hello" ))
333
- assert n .outerHTML == " <div foo=\ " bar\ " >Hello</div>"
334
+ assert n .outerHTML == ' <div foo="bar">Hello</div>'
334
335
335
336
336
337
def test_element_node_get_inner_html_empty ():
@@ -349,6 +350,7 @@ def test_element_node_get_set_inner_html_complex():
349
350
n .innerHTML = "<!-- comment --><p>Hello</p>"
350
351
assert n .innerHTML == "<!-- comment --><p>Hello</p>"
351
352
353
+
352
354
def test_element_node_set_inner_html_empty ():
353
355
"""
354
356
Set the innerHTML of the node as empty.
@@ -367,6 +369,133 @@ def test_element_node_set_inner_html_textarea():
367
369
n .innerHTML = "<textarea>Test <fake html></textarea>"
368
370
assert n .innerHTML == "<textarea>Test <fake html></textarea>"
369
371
372
+
373
+ def test_element_node_find ():
374
+ """
375
+ The find method validates the selector.
376
+ """
377
+ # Can't be empty.
378
+ selector = ""
379
+ n = polyplug .ElementNode (tagName = "div" )
380
+ with pytest .raises (ValueError ):
381
+ n .find (selector )
382
+ # Must be a valid id.
383
+ selector = ".my-id" # valid
384
+ n ._find_by_id = mock .MagicMock ()
385
+ n .find (selector )
386
+ n ._find_by_id .assert_called_once_with ("my-id" )
387
+ selector = "." # in-valid
388
+ with pytest .raises (ValueError ):
389
+ n .find (selector )
390
+ # Must be a valid class.
391
+ selector = "#my-class" # valid
392
+ n ._find_by_class = mock .MagicMock ()
393
+ n .find (selector )
394
+ n ._find_by_class .assert_called_once_with ("my-class" )
395
+ selector = "#" # in-valid
396
+ with pytest .raises (ValueError ):
397
+ n .find (selector )
398
+ # Must be a valid tagName.
399
+ selector = "tagName" # valid
400
+ n ._find_by_tagName = mock .MagicMock ()
401
+ n .find (selector )
402
+ n ._find_by_tagName .assert_called_once_with ("tagname" ) # lowercase!
403
+ selector = "not a tag name because spaces etc" # in-valid
404
+ with pytest .raises (ValueError ):
405
+ n .find (selector )
406
+
407
+
408
+ def test_element_node_find_by_id ():
409
+ """
410
+ The expected individual nodes are returned for various combinations of
411
+ searching the tree by unique id.
412
+ """
413
+ # Will return itself as the first match.
414
+ n = polyplug .ElementNode (tagName = "div" , attributes = {"id" : "foo" })
415
+ assert n == n .find (".foo" )
416
+ # Will return the expected child.
417
+ n = polyplug .ElementNode (tagName = "div" )
418
+ n .innerHTML = "<ul><li>Nope</li><li id='foo'>Yup</li></ul>"
419
+ result = n .find (".foo" )
420
+ assert isinstance (result , polyplug .ElementNode )
421
+ assert result .innerHTML == "Yup"
422
+ # Returns None if no match.
423
+ assert n .find (".bar" ) is None
424
+
425
+
426
+ def test_element_node_find_by_class ():
427
+ """
428
+ The expected collection of matching nodes are returned for various
429
+ combinations of searching the tree by CSS class.
430
+ """
431
+ # Returns itself if matched.
432
+ n = polyplug .ElementNode (tagName = "div" , attributes = {"class" : "foo" })
433
+ assert [
434
+ n ,
435
+ ] == n .find ("#foo" )
436
+ # Returns expected children (along with itself).
437
+ n = polyplug .ElementNode (tagName = "div" , attributes = {"class" : "foo" })
438
+ n .innerHTML = "<ul><li class='foo'>Yup</li><li class='foo'>Yup</li></ul>"
439
+ result = n .find ("#foo" )
440
+ assert len (result ) == 3
441
+ assert result [0 ] == n
442
+ assert result [1 ].tagName == "li"
443
+ assert result [2 ].tagName == "li"
444
+ # Returns just expected children (not itself).
445
+ n = polyplug .ElementNode (tagName = "div" , attributes = {"class" : "bar" })
446
+ n .innerHTML = "<ul><li class='foo'>Yup</li><li class='foo'>Yup</li></ul>"
447
+ result = n .find ("#foo" )
448
+ assert len (result ) == 2
449
+ assert result [0 ].tagName == "li"
450
+ assert result [1 ].tagName == "li"
451
+ # Returns just expected children with multiple classes.
452
+ n = polyplug .ElementNode (tagName = "div" , attributes = {"class" : "bar foo" })
453
+ n .innerHTML = (
454
+ "<ul><li class='foo bar'>Yup</li><li class='foobar'>Nope</li></ul>"
455
+ )
456
+ result = n .find ("#foo" )
457
+ assert len (result ) == 2
458
+ assert result [0 ] == n
459
+ assert result [1 ].tagName == "li"
460
+ # No match returns an empty list.
461
+ n = polyplug .ElementNode (tagName = "div" , attributes = {"class" : "bar" })
462
+ n .innerHTML = "<ul><li class='foo'>Nope</li><li class='foo'>Nope</li></ul>"
463
+ result = n .find ("#baz" )
464
+ assert result == []
465
+
466
+
467
+ def test_element_node_find_by_tagName ():
468
+ """
469
+ The expected collection of matching nodes are returned for various
470
+ combinations of searching the tree by tagName.
471
+ """
472
+ # Returns itself if matched.
473
+ n = polyplug .ElementNode (tagName = "div" )
474
+ assert [
475
+ n ,
476
+ ] == n .find ("div" )
477
+ # Returns expected children (along with itself).
478
+ n = polyplug .ElementNode (tagName = "li" )
479
+ n .innerHTML = "<ul><li>Yup</li><li>Yup</li></ul>"
480
+ result = n .find ("li" )
481
+ assert len (result ) == 3
482
+ assert result [0 ] == n
483
+ assert result [1 ].innerHTML == "Yup"
484
+ assert result [2 ].innerHTML == "Yup"
485
+ # Returns just expected children (not itself).
486
+ n = polyplug .ElementNode (tagName = "div" )
487
+ n .innerHTML = "<ul><li>Yup</li><li>Yup</li></ul>"
488
+ result = n .find ("li" )
489
+ assert len (result ) == 2
490
+ assert result [0 ].innerHTML == "Yup"
491
+ assert result [1 ].innerHTML == "Yup"
492
+ # No match returns an empty list.
493
+ n = polyplug .ElementNode (tagName = "div" )
494
+ n .innerHTML = "<ul><li>Nope</li><li>Nope</li></ul>"
495
+ result = n .find ("p" )
496
+ assert result == []
497
+
498
+
370
499
def test_text_node ():
371
500
"""
372
501
The TextNode instantiates as expected.
@@ -526,9 +655,9 @@ def test_htmltokenizer_get_value():
526
655
"""
527
656
Given a potential value for an attribute, grab and return it.
528
657
"""
529
- raw = "='foo '>"
658
+ raw = "='font: arial; font-weight: bold!important; '>"
530
659
tok = polyplug .HTMLTokenizer (raw )
531
- assert tok .get_value () == "foo "
660
+ assert tok .get_value () == "font: arial; font-weight: bold!important; "
532
661
assert tok .expect (">" ) is None
533
662
534
663
0 commit comments