示例#1
0
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")
示例#2
0
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")
示例#3
0
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")
示例#4
0
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')
示例#6
0
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')
示例#7
0
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
示例#8
0
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'])
示例#9
0
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'])
示例#10
0
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)
示例#11
0
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;")
示例#12
0
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>'
    )
示例#13
0
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',
        ]
    )
示例#14
0
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")
示例#15
0
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")
示例#16
0
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')
示例#17
0
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")
示例#18
0
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'
        }
    )
示例#19
0
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")
示例#20
0
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',
        ]
    )
示例#21
0
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',
        ]
    )
示例#22
0
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',
        ]
    )
示例#23
0
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)
示例#24
0
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
示例#25
0
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")
示例#26
0
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>'
    )
示例#27
0
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>'
    )
示例#28
0
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')
示例#29
0
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>'
    )
示例#30
0
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>'
    )