Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Read Xml File in Robot Framework Python

Robot Framework has builtIn libraries to manipulate xml file in testing. In this example, you will learn how to read xml file, get element by xpath and element attribute.

 

For example, we have a testcase.xml file that contains several test case ids and test case names. We use robot framework to read the content of the xml file.

Step 1: create a xml test project in robot framework IDE "RIDE". Create a new test suite named "Tests", import XML library and create a variable named ${XmlFile}. Add test cases "Parse-Xml-Test","Add-Element-Test","Get-Child-Elements-Test", and "Get-Elements-With-Attributes-Test".

Step 2: after adding those test cases above, text version of the test suite looks like this below

*** Settings ***
Library           XML

*** Variables ***
${XmlFile}        testcase.xml

*** Test Cases ***
Parse-Xml-Test
    ${root}=    Parse XML    ${XmlFile}
    Should Be Equal    ${root.tag}    TestCases
    ${testcase1}=    Get Element Text    ${root}    TestCase[@id='1']
    ${testcase2}=    Get Element Text    ${root}    TestCase[@id='2']
    ${testcase3}=    Get Element Text    ${root}    TestCase[@id='3']
    ${testcase4}=    Get Element Text    ${root}    TestCase[@id='4']
    ${testcase5}=    Get Element Text    ${root}    TestCase[@id='5']
    ${testcase6}=    Get Element Text    ${root}    TestCase[@id='6']

Add-Element-Test
    ${root}=    Parse XML    ${XmlFile}
    Should Be Equal    ${root.tag}    TestCases
    Add Element    ${root}    New Test Case
    ${testcase7}    Get Element Text    ${root}    TestCase[@id='7']

Get-Child-Elements-Test
    ${root}=    Parse XML    testcase.xml
    Should Be Equal    ${root.tag}    TestCases
    Get Child Elements    ${root}
    Length Should Be    ${root}    6

Get-Element-With-Attributes-Test
    ${root}=    Parse XML    testcase.xml
    Should Be Equal    ${root.tag}    TestCases
    ${TestCase2}    Get Element Text    ${root}    *[@id="2"]
    ${TestCase5}    Get Element Text    ${root}    *[@id="5"]

run the test and see the log

 
======================================================================================
XmlTest                                                                               
======================================================================================
XmlTest.Tests                                                                         
======================================================================================
Parse-Xml-Test                                                                | PASS |
--------------------------------------------------------------------------------------
Add-Element-Test                                                              | PASS |
--------------------------------------------------------------------------------------
Get-Child-Elements-Test                                                       | PASS |
--------------------------------------------------------------------------------------
Get-Element-With-Attributes-Test                                              | PASS |
--------------------------------------------------------------------------------------
XmlTest.Tests                                                                 | PASS |
4 critical tests, 4 passed, 0 failed
4 tests total, 4 passed, 0 failed
======================================================================================
XmlTest                                                                       | PASS |
4 critical tests, 4 passed, 0 failed
4 tests total, 4 passed, 0 failed
======================================================================================
Output:  c:\XmlTest\output.xml
Log:     c:\XmlTest\log.html
Report:  c:\XmlTest\report.html

test finished 20140728 13:44:23

Detailed Test Log

 

Starting test: XmlTest.Tests.Parse-Xml-Test
20140728 13:44:22.702 :  INFO : ${root} = <Element 'TestCases' at 0x0000000003500CF0>
20140728 13:44:22.704 :  INFO : 
Argument types are:
<type 'str'>
<type 'unicode'>
20140728 13:44:22.705 :  INFO : ${testcase1} = Valid User Login
20140728 13:44:22.706 :  INFO : ${testcase2} = Invalid User Login
20140728 13:44:22.707 :  INFO : ${testcase3} = Admin User Login
20140728 13:44:22.708 :  INFO : ${testcase4} = Dashboard Verification
20140728 13:44:22.709 :  INFO : ${testcase5} = User Settings Verification
20140728 13:44:22.710 :  INFO : ${testcase6} = Logout Verification
Ending test:   XmlTest.Tests.Parse-Xml-Test

Starting test: XmlTest.Tests.Add-Element-Test
20140728 13:44:22.712 :  INFO : ${root} = <Element 'TestCases' at 0x0000000003508540>
20140728 13:44:22.713 :  INFO : 
Argument types are:
<type 'str'>
<type 'unicode'>
20140728 13:44:22.716 :  INFO : ${testcase7} = New Test Case
Ending test:   XmlTest.Tests.Add-Element-Test

Starting test: XmlTest.Tests.Get-Child-Elements-Test
20140728 13:44:22.718 :  INFO : ${root} = <Element 'TestCases' at 0x000000000350C960>
20140728 13:44:22.719 :  INFO : 
Argument types are:
<type 'str'>
<type 'unicode'>
20140728 13:44:22.721 :  INFO : Length is 6
Ending test:   XmlTest.Tests.Get-Child-Elements-Test

Starting test: XmlTest.Tests.Get-Element-With-Attributes-Test
20140728 13:44:22.724 :  INFO : ${root} = <Element 'TestCases' at 0x0000000003500A20>
20140728 13:44:22.725 :  INFO : 
Argument types are:
<type 'str'>
<type 'unicode'>
20140728 13:44:22.726 :  INFO : ${TestCase2} = Invalid User Login
20140728 13:44:22.727 :  INFO : ${TestCase5} = User Settings Verification
Ending test:   XmlTest.Tests.Get-Element-With-Attributes-Test

visit the link to view output report and log html file