def select_by_attribute_id(context): "selecting by attribute (id)" dom = DOM(context.html) (body, ) = dom.find("[id=firstp]") assert that(body).is_a(Element) assert that(body.attribute['id']).equals("firstp")
def select_html(context): "selecting html" dom = DOM(context.html) html = dom.get("html") assert that(html).is_a(Element) assert that(html.attribute['id']).equals("html")
def select_by_id(context): "selecting by id" dom = DOM(context.html) body = dom.find("#firstp").first() assert that(body).is_a(Element) assert that(body.attribute['id']).equals("firstp")
def text_return_the_text_within_element(context): "Element().text() returns the text content" dom = DOM(context.html) p = dom.find("#the-only-paragraph").first() assert that(p.text()).equals("the only one in th whole damn thing!?")
def attr_retrieves_each_attribute_by_name(context): "attr retrieves attributes each attribute by name" dom = DOM(context.html) ul = dom.find("#objects").first() assert that(ul.attr('id')).equals('objects') assert that(ul.attr('class')).equals('list no-bullets')
def select_by_class_with_many_classes(context): "selecting by many classes at once" dom = DOM(context.html) elements = dom.find("li.stuff.thing") assert that(elements).the_attribute('tag').equals('li') assert that(elements[0].attribute['id']).equals('house')
def select_paragraphs(context): "selecting paragraphs" dom = DOM(context.html) paragraphs = dom.find("p") assert that(paragraphs).is_a(ElementSet) assert paragraphs.length is 6
def select_by_child(context): "selecting by parent > child, mixing many kinds of selectors" dom = DOM(context.html) elements = dom.find( "ul#objects > li.geometry" ) assert that(elements).in_each('tag').matches(['li', 'li']) assert that(elements).in_each("attribute['id']").matches(['ball', 'square'])
def select_by_child_complex(context): "selecting by parent > child, mixing many kinds of selectors" dom = DOM(context.html) elements = dom.find( "div.ball.dog.square.house.puppet#like-this-one > ul#objects > li.geometry" ) assert that(elements).in_each('tag').matches(['li', 'li']) assert that(elements).in_each("attribute['id']").matches(['ball', 'square'])
def select_paragraphs(context): "selecting paragraphs" dom = DOM(context.html) identifiers = ["firstp", "ap", "sndp", "en", "sap", "first"] paragraphs = dom.find("p") assert that(dom).is_a(DOM) assert that(paragraphs).in_each("attribute['id']").matches(identifiers)
def select_by_class(context): "selecting by class name" dom = DOM(context.html) div = dom.find(".nothiddendiv").first() assert that(div).is_a(Element) assert that(div.attribute['id']).equals("nothiddendiv") assert that(div.attribute['style']).has("height:1px;") assert that(div.attribute['style']).has("background:white;")
def html_return_the_html_string(context): "Element().html() returns the html string" dom = DOM(context.html) p = dom.find("#the-only-paragraph").first() assert that(p.html()).equals( '<p id="the-only-paragraph">the only one in th whole damn thing!?</p>' )
def select_by_attribute_ends_with_with_quotes(context): "selecting attribute that ends with certain value with quotes" dom = DOM(context.html) elements = dom.find("ul#packages > li[id$=\"nd\"]") assert that(elements).in_each("attribute['id']").matches( [ 'java island', ] )
def first_returns_the_first(context): "selecting all childs of some element" dom = DOM(context.html) elements = dom.find("#objects li") p = elements.first() assert that(p).is_a(Element) assert that(p.tag).equals("li") assert that(p.attribute['id']).equals("ball") assert that(p.text()).equals("to kick")
def last_returns_the_last(context): "selecting all childs of some element" dom = DOM(context.html) elements = dom.find("#objects li") p = elements.last() assert that(p).is_a(Element) assert that(p.tag).equals("li") assert that(p.attribute['id']).equals("puppet") assert that(p.text()).equals("to care with")
def select_all_childs_of_some(context): "selecting all childs of some element" dom = DOM(context.html) elements = dom.find("#objects *") assert that(elements[0].attribute['id']).equals('ball') assert that(elements[1].attribute['id']).equals('dog') assert that(elements[2].attribute['id']).equals('square') assert that(elements[3].attribute['id']).equals('house') assert that(elements[4].attribute['id']).equals('puppet')
def select_by_id_and_attribute_selector(context): "selecting by id" dom = DOM(context.html) possibilities = [ "#nothiddendiv[id=nothiddendiv]", "[id=nothiddendiv]#nothiddendiv", ] for selector in possibilities: div = dom.find(selector).first() assert that(div).is_a(Element) assert that(div.attribute['id']).equals("nothiddendiv")
def attr_retrieves_attributes_as_dict(context): "attr retrieves attributes as dict" dom = DOM(context.html) ul = dom.find("#objects").first() assert that(ul.attr()).is_a(dict) assert that(ul.attr()).equals( { 'id': 'objects', 'class': 'list no-bullets' } )
def select_by_class_and_attribute_selector_with_quotes(context): "selecting by class name with quotes" dom = DOM(context.html) possibilities = [ '.nothiddendiv[class="nothiddendiv"]', '[class="nothiddendiv"].nothiddendiv', ] for selector in possibilities: div = dom.find(selector).first() assert that(div).is_a(Element) assert that(div.attribute['id']).equals("nothiddendiv")
def select_by_attribute_contains_prefix_with_quotes(context): "selecting attribute that contains_prefix certain value with quotes" dom = DOM(context.html) elements = dom.find("ul#packages > li[id|=\"python\"]") assert that(elements).in_each("attribute['id']").matches( [ 'python-django', 'python-sponge', 'python-lettuce', ] )
def select_by_attribute_startswith(context): "selecting attribute that startswith certain value" dom = DOM(context.html) elements = dom.find("ul#packages > li[id^=pyt]") assert that(elements).in_each("attribute['id']").matches( [ 'python-django', 'python-sponge', 'python-lettuce', ] )
def select_by_attribute_contains(context): "selecting attribute that contains certain value" dom = DOM(context.html) elements = dom.find("ul#packages > li[id*=python]") assert that(elements).in_each("attribute['id']").matches( [ 'python-django', 'python-sponge', 'python-lettuce', 'something-python', ] )
def select_parent_element(context): "selecting by parent element" dom = DOM(context.html) identifiers = ["firstp", "ap", "sndp", "en", "sap", "first"] paragraphs1 = dom.find("div p") paragraphs2 = dom.find("body p") assert that(dom).is_a(DOM) assert paragraphs1.length is 6 assert that(paragraphs1).in_each("attribute['id']").matches(identifiers) assert paragraphs2.length is 6 assert that(paragraphs2).in_each("attribute['id']").matches(identifiers)
def select_by_attribute_contains_prefix(context): "selecting attribute that contains_prefix certain value" dom = DOM(context.html) elements = dom.find("ul#packages > li[id|=python]") assert that(elements).in_each("attribute['id']").matches( [ 'python-django', 'python-sponge', 'python-lettuce', ] ) found = dom.find("ul#packages > li[id|=java]") assert not found
def error_tolerance_for_non_well_formed_html(context): "DOM(html) ignores a non-well-formed HTML" parsed = DOM(context.html) assert that(parsed).is_a(DOM) head = parsed.find("head title").first() assert that(head.text()).equals(u"Gabriel Falcão's page") a, div, p = parsed.find("body *") assert that(a.text()).equals("My Profile") assert that(a.attr("href")).equals("http://github.com/gabrielfalcao") assert that(div.text()).looks_like("") assert that(div.attr("id")).equals("test") assert that(p.text()).equals("Paragraph")
def remove_attr_removes_attr(context): "remove_attr removes a attr" dom = DOM(context.html) ul = dom.find("#objects").first() ul.remove_attr('class') assert that(ul.attr('class')).equals(None) assert that(ul.html()).looks_like( '<ul id="objects">\n' ' <li class="geometry" id="ball">to kick</li>\n' ' <li id="dog">that barks</li>\n' ' <li class="geometry" id="square">that shapes</li>\n' ' <li class="stuff thing" id="house">for people</li>\n' ' <li id="puppet">to care with</li>\n' '</ul>' )
def attr_changes_a_attribute(context): "attr retrieves attributes each attribute by name" dom = DOM(context.html) ul = dom.find("#objects").first() ul.attr('id', 'list-of-stuff') assert that(ul.attr('id')).equals('list-of-stuff') assert that(ul.html()).looks_like( '<ul class="list no-bullets" id="list-of-stuff">\n' ' <li class="geometry" id="ball">to kick</li>\n' ' <li id="dog">that barks</li>\n' ' <li class="geometry" id="square">that shapes</li>\n' ' <li class="stuff thing" id="house">for people</li>\n' ' <li id="puppet">to care with</li>\n' '</ul>' )
def select_all(context): "selecting all *" dom = DOM(context.html) elements = dom.find("*") assert elements.length is 13 assert that(elements[0].tag).equals('html') assert that(elements[1].tag).equals('head') assert that(elements[2].tag).equals('title') assert that(elements[3].tag).equals('body') assert that(elements[4].tag).equals('div') assert that(elements[5].tag).equals('p') assert that(elements[6].tag).equals('div') assert that(elements[7].tag).equals('ul') assert that(elements, within_range=(8, 12)).the_attribute('tag').equals('li')
def text_modifies_the_text_within_element(context): "Element().text('new text') modifies the text content" dom = DOM( '<div class="drinks">\n' ' <h1 id="header">I like piña colada!</h1>\n' '</div>' ) h1 = dom.find("div.drinks h1").first() assert that(h1.text()).equals("I like piña colada!") h1.text('Do you like vodka?') assert that(h1.text()).equals("Do you like vodka?") assert that(h1.html()).equals('<h1 id="header">Do you like vodka?</h1>') assert that(dom.html()).equals( '<div class="drinks">\n' ' <h1 id="header">Do you like vodka?</h1>\n' '</div>' )
def html_modifies_the_html_within_element(conhtml): "Element().html('new html') modifies the html string" dom = DOM( '<div class="drinks">\n' ' <h1 id="header">I like marguerita!</h1>\n' '</div>' ) h1 = dom.find("div.drinks h1").first() assert that(h1.html()).equals('<h1 id="header">I like marguerita!</h1>') h1.html('<strong>Yeah, whiskey is much better!</strong>') assert that(h1.html()).equals('<strong>Yeah, whiskey is much better!</strong>') assert that(h1.text()).equals('Yeah, whiskey is much better!') assert that(dom.html()).equals( '<div class="drinks">\n' ' <strong>Yeah, whiskey is much better!</strong>\n' '</div>' )