2 new revisions:

Revision: 0c5cc0bd9d39
Author:   Robot Framework Developers <[email protected]>
Date:     Thu Jun 28 03:19:47 2012
Log:      XML library: attribute related keywords
http://code.google.com/p/robotframework/source/detail?r=0c5cc0bd9d39

Revision: ba4440449ce6
Author:   Robot Framework Developers <[email protected]>
Date:     Thu Jun 28 03:21:55 2012
Log:      XML library: terminology: match -> xpath
http://code.google.com/p/robotframework/source/detail?r=ba4440449ce6

==============================================================================
Revision: 0c5cc0bd9d39
Author:   Robot Framework Developers <[email protected]>
Date:     Thu Jun 28 03:19:47 2012
Log:      XML library: attribute related keywords
http://code.google.com/p/robotframework/source/detail?r=0c5cc0bd9d39

Added:
 /atest/testdata/standard_libraries/xml/element_attribute.txt
Modified:
 /src/robot/libraries/XML.py

=======================================
--- /dev/null
+++ /atest/testdata/standard_libraries/xml/element_attribute.txt Thu Jun 28 03:19:47 2012
@@ -0,0 +1,63 @@
+*** Settings ***
+Library      Collections
+Resource     resource.txt
+
+*** Test Cases ***
+Get attribute of current element
+    ${attr}=     Get Element Attribute    <tag a="1" b="2"/>    a
+    Should Be Equal    ${attr}    1
+
+Get attribute of child element
+ ${attr}= Get Element Attribute <root><tag a="1"/></root> a tag
+    Should Be Equal    ${attr}    1
+
+Getting non-existing attribute returns None
+    ${attr}=     Get Element Attribute    <tag a="1"/>    b
+    Should Be Equal    ${attr}    ${None}
+
+Default value is used when attribute does not exist
+ ${attr}= Get Element Attribute <tag a="1"/> b default=value
+    Should Be Equal    ${attr}    value
+ ${attr}= Get Element Attribute <tag a="1"/> a default=value
+    Should Be Equal    ${attr}    1
+
+Get element attributes
+    ${attrs}=   Get Element Attributes    <tag a="1" b="2"/>
+    Should Be True    ${attrs} == {'a': '1', 'b': '2'}
+    ${attrs}=   Get Element Attributes    <root><tag/></root>    tag
+    Should Be Empty    ${attrs}
+
+Modifying returned attributes does not affect original element
+    ${elem}=    Parse XML    <tag a="1" b="2"/>
+    ${attrs}=   Get Element Attributes    ${elem}
+    Should Be True    ${attrs} == {'a': '1', 'b': '2'}
+    Set To Dictionary    ${attrs}    c    3
+    ${attrs}=   Get Element Attributes    ${elem}
+    Should Be True    ${attrs} == {'a': '1', 'b': '2'}
+
+Element attribute should be
+    [Documentation]    FAIL 1 != 7
+    Element Attribute Should Be    <tag a="1" b="2"/>    a    1
+ Element Attribute Should Be <root><tag a="1" b="2"/></root> a 7 tag
+
+Element attribute should be when no attribute exists
+    [Documentation]    FAIL None != foo
+    Element Attribute Should Be    <tag a="1" b="2"/>    c    ${None}
+    Element Attribute Should Be    <tag a="1" b="2"/>    c    foo
+
+Element attribute should be with custom error message
+    [Documentation]    FAIL My message
+ Element Attribute Should Be <tag a="1"/> a x message=My message
+
+Element attribute should match
+    [Documentation]    FAIL '1' does not match '??'
+    Element Attribute Should Match    <tag a="foo-bar"/>    a    f*-???
+ Element Attribute Should Match <root><tag a="1"/></root> a ?? tag
+
+Element attribute should match when no attribute exists
+    [Documentation]    FAIL Attribute 'c' does not exist.
+    Element Attribute Should Match    <tag a="1" b="2"/>    c    ${None}
+
+Element attribute should match with custom error message
+    [Documentation]    FAIL Special
+ Element Attribute Should Match <tag a="1"/> a ?? message=Special
=======================================
--- /src/robot/libraries/XML.py Wed Jun 27 12:02:04 2012
+++ /src/robot/libraries/XML.py Thu Jun 28 03:19:47 2012
@@ -28,8 +28,8 @@
     only in 1.3 i.e in Python 2.7!
     """

-    _should_be_equal = BuiltIn().should_be_equal
-    _should_match = BuiltIn().should_match
+    _should_be_equal = partial(BuiltIn().should_be_equal, values=False)
+    _should_match = partial(BuiltIn().should_match, values=False)
     _normalize_whitespace = partial(re.compile('\s+').sub, ' ')

     def parse_xml(self, source):
@@ -78,18 +78,27 @@
     def element_text_should_be(self, source, expected, match='.',
                                normalize_whitespace=False, message=None):
         text = self.get_element_text(source, match, normalize_whitespace)
-        self._should_be_equal(text, expected, message, values=False)
+        self._should_be_equal(text, expected, message)

     def element_text_should_match(self, source, pattern, match='.',
normalize_whitespace=False, message=None):
         text = self.get_element_text(source, match, normalize_whitespace)
-        self._should_match(text, pattern, message, values=False)
-
-    def get_element_attribute(self, source, name, match=None):
-        return self.get_element(source, match).get(name)
-
- def element_attribute_should_match(self, source, name, pattern, match=None): - self._should_match(self.get_attribute(source, name, match), pattern)
-
- def element_attribute_should_be(self, source, name, expected, match=None): - self._should_be_equal(self.get_attribute(source, name, match), expected)
+        self._should_match(text, pattern, message)
+
+    def get_element_attribute(self, source, name, match='.', default=None):
+        return self.get_element(source, match).get(name, default)
+
+    def get_element_attributes(self, source, match='.'):
+        return self.get_element(source, match).attrib.copy()
+
+ def element_attribute_should_be(self, source, name, expected, match='.',
+                                    message=None):
+        attr = self.get_element_attribute(source, name, match)
+        self._should_be_equal(attr, expected, message)
+
+ def element_attribute_should_match(self, source, name, pattern, match='.',
+                                       message=None):
+        attr = self.get_element_attribute(source, name, match)
+        if attr is None:
+            raise AssertionError("Attribute '%s' does not exist." % name)
+        self._should_match(attr, pattern, message)

==============================================================================
Revision: ba4440449ce6
Author:   Robot Framework Developers <[email protected]>
Date:     Thu Jun 28 03:21:55 2012
Log:      XML library: terminology: match -> xpath
http://code.google.com/p/robotframework/source/detail?r=ba4440449ce6

Modified:
 /src/robot/libraries/XML.py

=======================================
--- /src/robot/libraries/XML.py Thu Jun 28 03:19:47 2012
+++ /src/robot/libraries/XML.py Thu Jun 28 03:21:55 2012
@@ -41,22 +41,22 @@
             return self.parse_xml(source)
         return source

-    def get_element(self, source, match):
- if match == '.': # TODO: Is this good workaround for ET 1.2 not supporting '.'?
+    def get_element(self, source, xpath):
+ if xpath == '.': # TODO: Is this good workaround for ET 1.2 not supporting '.'?
             return self._get_parent(source)
-        elements = self.get_elements(source, match)
+        elements = self.get_elements(source, xpath)
         if not elements:
-            raise RuntimeError("No element matching '%s' found." % match)
+            raise RuntimeError("No element matching '%s' found." % xpath)
         if len(elements) > 1:
raise RuntimeError("Multiple elements (%d) matching '%s' found."
-                               % (len(elements), match))
+                               % (len(elements), xpath))
         return elements[0]

-    def get_elements(self, source, match):
-        return self._get_parent(source).findall(match)
-
- def get_element_text(self, source, match='.', normalize_whitespace=False):
-        element = self.get_element(source, match)
+    def get_elements(self, source, xpath):
+        return self._get_parent(source).findall(xpath)
+
+ def get_element_text(self, source, xpath='.', normalize_whitespace=False):
+        element = self.get_element(source, xpath)
         text = ''.join(self._yield_texts(element))
         if normalize_whitespace:
             text = self._normalize_whitespace(text).strip()
@@ -71,34 +71,34 @@
         if element.tail and not top:
             yield element.tail

- def get_elements_texts(self, source, match, normalize_whitespace=False): + def get_elements_texts(self, source, xpath, normalize_whitespace=False): return [self.get_element_text(elem, normalize_whitespace=normalize_whitespace)
-                for elem in self.get_elements(source, match)]
-
-    def element_text_should_be(self, source, expected, match='.',
+                for elem in self.get_elements(source, xpath)]
+
+    def element_text_should_be(self, source, expected, xpath='.',
                                normalize_whitespace=False, message=None):
-        text = self.get_element_text(source, match, normalize_whitespace)
+        text = self.get_element_text(source, xpath, normalize_whitespace)
         self._should_be_equal(text, expected, message)

-    def element_text_should_match(self, source, pattern, match='.',
+    def element_text_should_match(self, source, pattern, xpath='.',
normalize_whitespace=False, message=None):
-        text = self.get_element_text(source, match, normalize_whitespace)
+        text = self.get_element_text(source, xpath, normalize_whitespace)
         self._should_match(text, pattern, message)

-    def get_element_attribute(self, source, name, match='.', default=None):
-        return self.get_element(source, match).get(name, default)
-
-    def get_element_attributes(self, source, match='.'):
-        return self.get_element(source, match).attrib.copy()
-
- def element_attribute_should_be(self, source, name, expected, match='.',
+    def get_element_attribute(self, source, name, xpath='.', default=None):
+        return self.get_element(source, xpath).get(name, default)
+
+    def get_element_attributes(self, source, xpath='.'):
+        return self.get_element(source, xpath).attrib.copy()
+
+ def element_attribute_should_be(self, source, name, expected, xpath='.',
                                     message=None):
-        attr = self.get_element_attribute(source, name, match)
+        attr = self.get_element_attribute(source, name, xpath)
         self._should_be_equal(attr, expected, message)

- def element_attribute_should_match(self, source, name, pattern, match='.', + def element_attribute_should_match(self, source, name, pattern, xpath='.',
                                        message=None):
-        attr = self.get_element_attribute(source, name, match)
+        attr = self.get_element_attribute(source, name, xpath)
         if attr is None:
             raise AssertionError("Attribute '%s' does not exist." % name)
         self._should_match(attr, pattern, message)

Reply via email to