Exemple #1
0
    def test_setAttributes(self):
        tag = AdvancedTag('div')
        tag.setAttributes({'id': 'abc', 'name': 'cheese', 'x-attr': 'bazing'})

        assert tag.getAttribute('id') == 'abc'
        assert tag.getAttribute('name') == 'cheese'
        assert tag.getAttribute('x-attr') == 'bazing'
    def test_setAttribute(self):
        tag = AdvancedTag('div')

        tag.setAttribute('id', 'abc')

        assert tag.getAttribute('id') == 'abc' , 'Expected id to be abc'

        assert tag.getAttribute('blah') == None , 'Expected unset attribute to return None, actually returned %s' %(tag.getAttribute('blah'),)
Exemple #3
0
    def test_setAttribute(self):
        tag = AdvancedTag('div')

        tag.setAttribute('id', 'abc')

        assert tag.getAttribute('id') == 'abc', 'Expected id to be abc'

        assert tag.getAttribute(
            'blah'
        ) == None, 'Expected unset attribute to return None, actually returned %s' % (
            tag.getAttribute('blah'), )
    def test_setAttributes(self):
        tag = AdvancedTag('div')
        tag.setAttributes( {
            'id' : 'abc',
            'name'  :  'cheese',
            'x-attr' : 'bazing'
        })

        assert tag.getAttribute('id') == 'abc'
        assert tag.getAttribute('name') == 'cheese'
        assert tag.getAttribute('x-attr') == 'bazing'
Exemple #5
0
    def test_specialAttributes(self):
        tag = AdvancedTag('div')
        tag.setAttribute('style', 'position: absolute')
        styleValue = str(tag.getAttribute('style'))
        styleValue = styleValue.strip()
        assert styleValue == 'position: absolute', 'Expected position: absolute, got %s' % (
            str(tag.getAttribute('style')), )

        tag.className = 'one two'
        assert str(tag.className).strip(
        ) == 'one two', 'Expected classname to be "one two", got %s' % (repr(
            str(tag.className).strip()), )
Exemple #6
0
    def test_maxLength(self):
        '''
            test_maxLength - Test the "maxLength" attribute
        '''

        inputEm = AdvancedTag('input')

        assert inputEm.maxlength is None, 'Expected .maxlength to not be valid (should be .maxLength)'

        assert inputEm.maxLength == -1, 'Expected default .maxLength to be -1'

        inputEm.maxLength = 5

        assert inputEm.maxLength == 5, 'Expected to be able to set maxLength to 5 and get 5 back, but got: ' + repr(
            inputEm.maxLength)

        inputEmHTML = str(inputEm)

        assert 'maxlength="5"' in inputEmHTML, 'Expected .maxLength to set the "maxlength" attribute. Got: ' + inputEmHTML

        maxLengthAttr = inputEm.getAttribute('maxlength', None)
        assert maxLengthAttr == "5", 'Expected .getAttribute("maxlength") to return "5", but got: ' + repr(
            maxLengthAttr)

        inputEm.maxLength = 0

        assert inputEm.maxLength == 0, 'Expected to be able to set maxLength to 0'

        gotException = False
        try:
            inputEm.maxLength = -4
        except Exception as e:
            gotException = e

        assert gotException is not False, 'Expected to get an exception when setting maxLength < 0. Did not.'

        try:
            inputEm.setAttribute('maxlength', '-5')
        except Exception as e:
            raise AssertionError(
                'Expected to be able to use .setAttribute to set "maxlength" to an invalid value, but got exception: %s: %s'
                % (e.__class__.__name__, str(e)))

        inputEmHTML = str(inputEm)

        assert 'maxlength="-5"' in inputEmHTML, 'Expected .setAttribute to really set an invalid value on the HTML. Should be maxlength="-5", but got: ' + inputEmHTML

        maxLengthValue = None
        try:
            maxLengthValue = inputEm.maxLength
        except Exception as e:
            raise AssertionError(
                'Expected to be able to query .maxLength after .setAttribute to an invalid value, but got an exception: %s: %s'
                % (e.__class__.__name__, str(e)))

        assert maxLengthValue == -1, 'Expected invalid attribute value to return "-1" on .maxLength access, but got: ' + repr(
            maxLengthValue)
    def test_specialAttributes(self):
        tag = AdvancedTag('div')
        tag.setAttribute('style', 'position: absolute')
        styleValue = str(tag.getAttribute('style'))
        styleValue = styleValue.strip()
        assert styleValue == 'position: absolute' , 'Expected position: absolute, got %s' %(str(tag.getAttribute('style')),)

        tag.setAttribute('className', 'one two')
        assert str(tag.className).strip() == 'one two' , 'Expected classname to be "one two", got %s' %(repr(str(tag.className).strip()),)
Exemple #8
0
    def test_removeAttribute(self):
        '''
            test_removeAttribute - Test removing attributes
        '''

        tag = AdvancedTag('div')

        tag.setAttribute('align', 'left')
        tag.setAttribute('title', 'Hover text')

        htmlStr = str(tag)

        assert 'align="left"' in htmlStr, 'Expected setAttribute("align", "left") to result in align="left" in HTML representation. Got: ' + htmlStr

        alignAttrValue = tag.getAttribute('align')

        assert alignAttrValue == 'left', 'Expected getAttribute("align") to return "left" after having set align to "left". Got: ' + repr(
            alignAttrValue)

        tag.removeAttribute('align')

        htmlStr = str(tag)

        assert 'align="left"' not in htmlStr, 'Expected removeAttribute("align") to remove align="left" from HTML representation. Got: ' + htmlStr

        alignAttrValue = tag.getAttribute('align')

        assert alignAttrValue != 'left', 'Expected removeAttribute("align") to remove align: left from attributes map. Got: ' + repr(
            alignAttrValue)

        tag.removeAttribute('title')

        htmlStr = str(tag)

        assert 'align="left"' not in htmlStr, 'Expected after all attributes removed via removeAttribute that align="left" would not be present. Got: ' + htmlStr
        assert 'title=' not in htmlStr, 'Expected after all attributes removed via removeAttribute that align="left" would not be present. Got: ' + htmlStr

        attributes = tag.attributes

        assert 'align' not in attributes, 'Expected to NOT find "align" within the attributes. Got: ' + repr(
            attributes)
        assert 'title' not in attributes, 'Expected to NOT find "align" within the attributes. Got: ' + repr(
            attributes)
Exemple #9
0
    def test_specialAttributes(self):
        tag = AdvancedTag('div')

        assert tag.spellcheck is False, 'Expected default value of "spellcheck" field via dot-access to be False'
        assert 'spellcheck' not in str(
            tag
        ), 'Expected "spellcheck" to not show up in HTML representation before set. Got: ' + str(
            tag)

        tag.spellcheck = True

        assert tag.spellcheck is True, 'Expected after setting spellcheck to True, dot-access returns True.'

        tagHTML = str(tag)

        assert 'spellcheck="true"' in tagHTML, "Expected spellcheck to have value of string 'true' in HTML representation. Got: " + tagHTML

        tag.spellcheck = False
        tagHTML = str(tag)

        assert tag.spellcheck is False, 'Expected spellcheck to have dot-access value of False after being set to False. Got: ' + repr(
            tag.spellcheck)
        assert 'spellcheck="false"' in tagHTML, "Expected spellcheck to have value of string 'false' in HTML representation after being set to False. Got: " + tagHTML
        assert tag.getAttribute(
            'spellcheck'
        ) == 'false', 'Expected getAttribute("spellcheck") to return string "false" in HTML representation. Got: ' + repr(
            tag.getAttribute('spellcheck'))

        tag.spellcheck = "yes"

        assert tag.spellcheck is True, 'Expected after setting spellcheck to "yes", dot-access reutrns True. Got: ' + repr(
            tag.spellcheck)
        tagHTML = str(tag)

        assert 'spellcheck="true"' in tagHTML, "Expected spellcheck to have value of string 'true' in HTML representation after being set to 'yes'. Got: " + tagHTML

        assert tag.getAttribute(
            'spellcheck'
        ) == "true", "Expected getAttribute('spellcheck') to return string of True"
Exemple #10
0
    def test_setAttributesOnItem(self):
        tag = AdvancedTag('div')

        tag.id = 'hello'

        assert tag.id == 'hello', 'Expected to be able to set special attribute "id" and have it show up as both attribute and on item'
        assert tag.getAttribute(
            'id', ''
        ) == 'hello', 'Expected to be able to set special attribute "id" and have it show up as both attribute and on item'

        tag.name = 'cheese'

        assert tag.name == 'cheese', 'Expected to be able to set special attribute "name" and have it show up as both attribute and on item'
        assert tag.getAttribute(
            'name', ''
        ) == 'cheese', 'Expected to be able to set special attribute "name" and have it show up as both attribute and on item'

        assert tag.tabIndex == -1, 'Expected default tab index (unset) to be -1'

        tag.tabIndex = 5

        assert 'tabindex="5"' in tag.outerHTML, 'Expected setting the tabIndex to set tabindex attribute on html'
Exemple #11
0
    def test_unknownStillAttribute(self):
        '''
            test_unknownStillAttribute - Test that setting an unknwon attribute still sets it in HTML
        '''
        tag = AdvancedTag('div')

        tag.setAttribute('squiggle', 'wiggle')

        htmlStr = str(tag)

        assert 'squiggle="wiggle"' in htmlStr

        squiggleAttrValue = tag.getAttribute('squiggle')

        assert squiggleAttrValue == 'wiggle', "Expected 'squiggle' attribute from tag.getAttribute to return 'wiggle'. Got: " + repr(
            squiggleAttrValue)
    def test_inputAutocomplete(self):
        '''
            test input autocomplete attribute
        '''

        inputEm = AdvancedTag('input')

        assert inputEm.autocomplete == '', 'Expected default for unset "autocomplete" to be "". Got: ' + repr(
            inputEm.autocomplete)

        inputEm.autocomplete = 'on'
        inputHTML = str(inputEm)

        assert inputEm.autocomplete == 'on', 'Expected autocomplete="on" to retain on. Got: ' + repr(
            inputEm.autocomplete)

        assert inputEm.getAttribute(
            'autocomplete'
        ) == 'on', 'Expected getAttribute to return the value on a binary attribute when provided'

        assert 'autocomplete="on"' in inputHTML, 'Expected html property to be set. Got: ' + repr(
            inputHTML)

        inputEm.autocomplete = 'blah'

        assert inputEm.autocomplete == '', 'Expected autocomplete="blah" to use invalid fallback of "". Got: ' + repr(
            inputEm.autocomplete)

        inputEm.autocomplete = 'off'
        inputHTML = str(inputEm)

        assert inputEm.autocomplete == 'off', 'Expected to be able to switch autocomplete to off. Got: ' + repr(
            inputEm.autocomplete)

        assert 'autocomplete="off"' in inputHTML, 'Expected html property to be set. Got: ' + repr(
            inputHTML)

        inputEm.autocomplete = ''
        inputHTML = str(inputEm)

        assert inputEm.autocomplete == '', 'Expected to be able to set autocomplete to empty string. Got: ' + repr(
            inputEm.autocomplete)

        assert 'autocomplete=""' in inputHTML, 'Expected html property to be set to empty string. Got: ' + repr(
            inputHTML)
Exemple #13
0
    def test_attributesCase(self):
        '''
            test_attributesCase - Test that getAttribute and setAttribute force lowercase for keys
        '''

        em = AdvancedTag('input')

        em.setAttribute('MaXlEnGtH', '5')

        html = str(em)

        assert 'maxlength="5"' in html, 'Expected setAttribute to lowercase the key before setting. Got: ' + html

        assert em.getAttribute(
            'MAXlength', None
        ) == '5', 'Expected getAttribute to lowercase the key before setting.'

        assert em.hasAttribute(
            'maXlenGTH') is True, 'Expected hasAttribute to lowercase the key'

        assert 'MaxLength' in em.attributes, 'Expected "in" operator to lowercase the key'
Exemple #14
0
    def test_nameChangeFields(self):
        '''
            Test that fields with a different dot-access variable are handled properly
        '''

        td = AdvancedTag('td')

        assert td.colspan is None, 'Expected "colspan" to be "colSpan"'

        assert td.colSpan is not None, 'Expected "colSpan" to map to "colspan"'

        td.colSpan = 5

        assert not td.colspan, 'dot access should be colSpan, but colspan worked.'

        assert str(
            td.colSpan) == "5", "dot access on .colSpan should have worked"

        assert str(
            td.getAttribute('colspan')
        ) == "5", "Expected getAttribute to use the all lowercase name"

        tdHTML = str(td)

        assert "colSpan" not in tdHTML, "Expected html attribute to be the lowercased name. Got: " + tdHTML

        assert 'colspan="5"' in tdHTML, 'Expected colspan="5" to be present. Got: ' + tdHTML

        td.setAttribute('colspan', '8')

        tdHTML = str(td)

        assert str(
            td.colSpan
        ) == '8', 'Expected setAttribute("colspan",...) to update .colSpan attribute. Was 5, set to 8, and got: ' + repr(
            td.colSpan)

        assert 'colspan="8"' in tdHTML, 'Expected setAttribute("colspan") to update HTML. Got: ' + tdHTML

        assert td.colSpan == 8, 'Expected colSpan to be an integer value. Got: ' + str(
            type(td.colSpan))

        td = AdvancedTag('td', attrList=[('colspan', '5')])

        assert td.colSpan == 5, 'Expected setting "colspan" in attrList sets colSpan'

        # Now try a binary field

        form = AdvancedTag('form')

        assert form.novalidate is None, 'Expected novalidate on form to have dot-access name of noValidate'
        assert form.noValidate is not None, 'Expected novalidate on form to have dot-access name of noValidate'

        assert form.noValidate is False, 'Expected default for form.noValidate to be False'

        form.noValidate = "yes"

        assert form.noValidate is True, 'Expected noValidate to be converted to a bool. Got: ' + repr(
            form.noValidate)

        formHTML = str(form)

        assert 'novalidate' in formHTML, 'Expected form.noValidate to set "novalidate" property in HTML. Got: ' + formHTML

        form.noValidate = True
        formHTML = str(form)

        assert 'novalidate' in formHTML, 'Expected form.noValidate to set "novalidate" property in HTML. Got: ' + formHTML

        form.noValidate = False
        formHTML = str(form)

        assert 'novalidate' not in formHTML, 'Expected setting form.noValidate to False to remove it from HTML. Got: ' + formHTML
Exemple #15
0
    def test_delAttributes(self):
        '''
            test_delAttributes - Test deleting attributes
        '''
        tag = AdvancedTag('div')

        tag.setAttribute('id', 'abc')

        tag.style = 'display: block; float: left'

        tagHTML = tag.toHTML()

        assert 'id="abc"' in tagHTML, 'Expected id="abc" to be present in tag html. Got: ' + tagHTML
        assert 'style=' in tagHTML, 'Expected style attribute to be present in tag html. Got: ' + tagHTML

        assert tag.style.display == 'block', 'Expected style.display to be "block". Style is: ' + str(
            tag.style)
        assert tag.style.float == 'left', 'Expected style.float to be "left". Style is: ' + str(
            tag.style)

        del tag.attributes['id']

        tagHTML = tag.toHTML()

        assert not tag.id, "Expected id to be empty after deleting attribute. It is: " + repr(
            tag.id)
        assert 'id="abc"' not in tagHTML, 'Expected id attribute to NOT be present in tagHTML after delete. Got: ' + tagHTML

        del tag.attributes['style']

        tagHTML = tag.toHTML()

        assert 'style=' not in tagHTML, 'Expected style attribute to NOT be present in tagHTML after delete. Got: ' + tagHTML
        assert str(
            tag.style
        ) == '', 'Expected str(tag.style) to be empty string after delete of style attribute. It was: ' + repr(
            str(tag.style))
        assert tag.style.display == '', 'Expected display style property to be cleared after delete of style attribute. Style is: ' + str(
            tag.style)
        assert tag.style.float == '', 'Expected float style property to be cleared after delete of style attribute. Style is: ' + str(
            tag.style)

        tag.style = 'font-weight: bold; float: right'

        tagHTML = tag.toHTML()

        assert 'style=' in tagHTML, 'Expected after deleting style, then setting it again style shows up as HTML attr. Got: ' + tagHTML

        assert 'font-weight: bold' in tagHTML, 'Expected after deleting style, then setting it again the properties show up in the HTML "style" attribute. Got: ' + tagHTML

        assert id(tag.getAttribute('style', '')) == id(
            tag.style
        ), 'Expected after deleting style, then setting it again the attribute remains linked.'

        assert tag.style.fontWeight == 'bold', 'Expected after deleting style, then setting it again everything works as expected. Set style to "font-weight: bold; float: left" but on tag.style it is: ' + str(
            tag.style)

        tag.addClass('bold-item')
        tag.addClass('blue-font')

        assert 'bold-item' in tag.classList, 'Did addClass("bold-item") but did not find it in classList. classList is: ' + repr(
            tag.classList)
        assert 'blue-font' in tag.classList, 'Did addClass("blue-font") but did not find it in classList. classList is: ' + repr(
            tag.classList)

        classes = tag.getAttribute('class')

        assert 'bold-item' in classes and 'blue-font' in classes, 'Expected class attribute to contain both classes. Got: ' + str(
            classes)

        # This should call del tag._attributes['class']
        tag.removeAttribute('class')

        assert 'bold-item' not in tag.classList, 'After removing class, expected to not find classes in tag.classList. classList is: ' + repr(
            tag.classList)

        assert len(
            tag.classList
        ) == 0, 'Expected to have no classes in classList after delete. Got %d' % (
            len(tag.classList), )

        assert tag.getAttribute(
            'class'
        ) == '', 'Expected after removing class it would be an empty string'

        assert tag.getAttribute(
            'clazz'
        ) is None, 'Expected default getAttribute on non-set attribute to be None'