Season Libraries - Ep. 06 Library XML - Manipulação e Conferência de XMLs
Olááááá Robotizadores!!! Vamos falar de como ler, salvar, manipular e conferir XMLs!!! É uma biblioteca chatinha de entender, confesso, mas esse tutorial veio pra tirar várias dúvidas!!! Segue só...
#FICADICA: Lembrando que é necessário que você já tenha uma noção sobre o Robot Framework!!! Se ainda não tem, dá uma olhadinha nos primeiros posts do blog!!!
01 - A library XML
A Library XML é uma library do tipo Standard, então você não precisará baixá-la e nem instalá-la, ela já vem com o Robot. Mas para utilizá-la nos seus testes você precisará instanciá-la:
*** Settings ***
Library XML
Com ela você poderá ler elementos e atributos, criar, manipular e conferir XMLs. A documentação de keywords dela é bem completa, porém é COMPLEXA, recomendo leitura com CALMA e ATENÇÃO nos exemplos dados.
02 - Exemplo de uso
Trago aqui um código cheio de exemplos, claro que não tem todos e o XML é bem simples, mas vale a pena para entender o funcionamento dela:
Considere o XML base abaixo:
Veja o Resource com vários exemplos de manipulação e conferência:
Olhe como ficou o XML após a execução do Resource acima:
Para executar na sua máquina, baixe o projeto completo no meu GitHub:
ExemploLibraryXML
Espero que tenha tirado muitas dúvidas sobre essa Library!!! Até a próxima Robotizadores!!!
Considere o XML base abaixo:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<SOAP-ENV:Body> | |
<consultarAvisosPendentes> | |
<idConsultante>ID_ANTES</idConsultante> | |
<senhaConsultante>12345678</senhaConsultante> | |
<dataReferencia>19000101000000</dataReferencia> | |
<outroParametro valor="false" nome="atendimentoPlantao">false</outroParametro> | |
<outroParametro valor="true" nome="urgente">true</outroParametro> | |
</consultarAvisosPendentes> | |
</SOAP-ENV:Body> | |
</SOAP-ENV:Envelope> |
Veja o Resource com vários exemplos de manipulação e conferência:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*** Settings *** | |
Library OperatingSystem | |
Library String | |
Library XML | |
*** Keywords *** | |
Manipular e conferir XML | |
############ OPÇÃO 01 - Manipular como STRING ############ | |
### Pegue um XML no diretório, armazene em uma variável e manipule como STRING | |
${XML_CONTENT} Get File xmlBaseExemplo.xml | |
${XML_CONTENT} Replace String ${XML_CONTENT} <idConsultante>ID_ANTES</idConsultante> <idConsultante>ID_DEPOIS</idConsultante> | |
Log ${XML_CONTENT} | |
############ OPÇÃO 02 - Manipular como XML ############ | |
### Pegando Elementos e Atributos com a library XML | |
${ELEMENTO} Get Element Text ${XML_CONTENT} xpath=.//idConsultante | |
Log ${ELEMENTO} | |
${ATRIBUTO} Get Element Attribute ${XML_CONTENT} nome xpath=.//outroParametro[1] | |
Log ${ATRIBUTO} | |
### Manipulando Elementos com a library XML | |
${XML_CONTENT} Remove Element ${XML_CONTENT} xpath=.//senhaConsultante | |
Log Element ${XML_CONTENT} | |
${XML_CONTENT} Add Element ${XML_CONTENT} <senhaConsultante>9999999</senhaConsultante> xpath=.//consultarAvisosPendentes index=2 | |
Log Element ${XML_CONTENT} | |
${XML_CONTENT} Set Element Text ${XML_CONTENT} 02072019 xpath=.//dataReferencia | |
Log Element ${XML_CONTENT} | |
${XML_CONTENT} Add Element ${XML_CONTENT} <outroParametro>true</outroParametro> xpath=.//consultarAvisosPendentes | |
${XML_CONTENT} Set Element Attribute ${XML_CONTENT} nome mayara xpath=.//outroParametro[3] | |
${XML_CONTENT} Set Element Attribute ${XML_CONTENT} valor QA xpath=.//outroParametro[3] | |
Log Element ${XML_CONTENT} | |
### Após manipular, salve o XML e use como preferir | |
Save Xml ${XML_CONTENT} meuXMLmanipulado.xml | |
${XML_FINAL} Get File meuXMLmanipulado.xml | |
Log ${XML_FINAL} | |
### Conferências com a library XML | |
Element Text Should Be ${XML_FINAL} ID_DEPOIS xpath=.//idConsultante | |
Element Attribute Should Be ${XML_FINAL} nome atendimentoPlantao xpath=.//outroParametro[1] | |
Element Attribute Should Be ${XML_FINAL} valor false xpath=.//outroParametro[1] | |
Element Attribute Should Be ${XML_FINAL} nome urgente xpath=.//outroParametro[2] | |
Element Attribute Should Be ${XML_FINAL} valor true xpath=.//outroParametro[2] | |
Element Attribute Should Be ${XML_FINAL} nome mayara xpath=.//outroParametro[3] | |
Element Attribute Should Be ${XML_FINAL} valor QA xpath=.//outroParametro[3] |
Olhe como ficou o XML após a execução do Resource acima:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> | |
<Body> | |
<consultarAvisosPendentes xmlns=""> | |
<idConsultante>ID_DEPOIS</idConsultante> | |
<dataReferencia>02072019</dataReferencia> | |
<senhaConsultante>9999999</senhaConsultante> | |
<outroParametro nome="atendimentoPlantao" valor="false">false</outroParametro> | |
<outroParametro nome="urgente" valor="true">true</outroParametro> | |
<outroParametro nome="mayara" valor="QA">true</outroParametro> | |
</consultarAvisosPendentes> | |
</Body> | |
</Envelope> |
Para executar na sua máquina, baixe o projeto completo no meu GitHub:
ExemploLibraryXML
Espero que tenha tirado muitas dúvidas sobre essa Library!!! Até a próxima Robotizadores!!!
Ótimo exemplo, salvo nos favoritos para referência futura! Abraços e sucesso.
ResponderExcluirBoa noite! Gostaria de saber se é possível extrair um conteúdo de uma página html e salvar em um arquivo em formato xml. Obrigadoo e excelente blog :)
ResponderExcluir