Not Able to fetch element using Get Element in Robot Framework - robotframework

I have the below xml snippet and i am unable to fetch the element using Get Element.
<configuration commit-localtime="2020-06-27 12:48:13 IST" commit-seconds="1593242293" commit-user="root">
<groups>
<name>group1</name>
<interfaces>
<interface>
<name><*></name>
<unit>
Is the xpath=configuration/groups/name incorrect?
Have also tried xpath=name but does not work.
Get error as No element matching 'configuration/groups/name' found

Your Xpath is incorrect, I have created a small test case as an example. In it you can see an xpath expression that returns what you want.
Fetch element in XML document
${root} = Parse XML ${XML}
log ${root}
${first} = Get Element ${root} xpath=.//groups/name
Should Be Equal ${first.text} group1

Related

XML Library able to read from XML but not write/changes to XML

I am writing a test case using RobotFramework and the XML Library where I want to change the text of an element.
I am also using DataDriver to fill in my arguments.
I have some basic code where I want to change the text element and afterwards I am attempting to confirm that the change was correctly done:
Test to Change one value in XML
[Arguments] ${DEBMEMRID} ${DEBBANKID} ${DEBCODEBRAND} ${DEBCOMPID} ${DEBCOMMAWLID} ${CREMEMID} ${CREMEMTYPE} ${CREBANKID} ${CRECODEBRAND} ${CRECOMPID} ${CRECOMID}
${root}= Parse XML ${XMLFile}
Set Element Text ${XMLFile} ${DEBMEMRID} xpath=./Payment/Payment_Instruction/Debited_Party/Member/Member_ID
Element Text Should Be ${XMLFile} ${DEBMEMRID} xpath=./Payment/Payment_Instruction/Debited_Party/Member/Member_ID
${text} Get Element Text ${XMLFile} xpath=./Payment/Payment_Instruction/Debited_Party/Member/Member_ID
So the "Element Text Should Be" manages succesfully to read the XML as by text case goes into failed.
------------------------------------------------------------------------------
TestID 2 | FAIL |
400003 != 400001
------------------------------------------------------------------------------
So in the XML the original value is 400003 and the value I want to add is 400001.
This is the logging/output for this part of the code:
<kw library="XML" name="Parse Xml">
<doc>Parses the given XML file or string into an element structure.</doc>
<arguments>
<arg>${XMLFile}</arg>
</arguments>
<assign>
<var>${root}</var>
</assign>
<msg level="INFO" timestamp="20190827 12:54:54.202">${root} = <Element 'Document' at 0x03850240></msg>
<status starttime="20190827 12:54:54.201" status="PASS" endtime="20190827 12:54:54.203"></status>
</kw>
<kw library="XML" name="Set Element Text">
<doc>Sets text and/or tail text of the specified element.</doc>
<arguments>
<arg>${XMLFile}</arg>
<arg>${DEBMEMRID}</arg>
<arg>xpath=./Payment/Payment_Instruction/Debited_Party/Member/Member_ID</arg>
</arguments>
<status starttime="20190827 12:54:54.203" status="PASS" endtime="20190827 12:54:54.204"></status>
</kw>
<kw library="XML" name="Element Text Should Be">
<doc>Verifies that the text of the specified element is ``expected``.</doc>
<arguments>
<arg>${XMLFile}</arg>
<arg>${DEBMEMRID}</arg>
<arg>xpath=./Payment/Payment_Instruction/Debited_Party/Member/Member_ID</arg>
</arguments>
<msg level="FAIL" timestamp="20190827 12:54:54.205">400003 != 400001</msg>
<status starttime="20190827 12:54:54.204" status="FAIL" endtime="20190827 12:54:54.206"></status>
</kw>
Is there something like permissions that need to be set up in order for the XML Library to be able to adapt the file? How can I set does up?
${root}= Parse XML ${XMLFile}
Set Element Text ${root} ${DEBMEMRID} xpath=./Payment/Payment_Instruction/Debited_Party/Member/Member_ID
Element Text Should Be ${root} ${DEBMEMRID} xpath=./Payment/Payment_Instruction/Debited_Party/Member/Member_ID
After parsing the XML to ${root} you shall set the element text and do the comparison for ${root} as well. In your example you parsed the ${XML_FILE} into ${root} but directed all the subsequent keywords against the ${XML_FILE}.
If you want to overwrite the old XML file with the new information you may do so with the keyword Save Xml:
Save XML ${root} ${XML_FILE}
The full Robot Framework XML library documentation can be found here:
https://robotframework.org/robotframework/latest/libraries/XML.html

R SAX Parse attribute in empty element XML

I am new to R and I cannot find example of extracting specific attribute in an empty element, most example i found are extracting data value of a child nodes.
In a nutshell, how to extract an attribute using xmlEventParse() for XML like this:
<elements>
<element attribute1="value" attribute2="value"/>
<element attribute1="value" attribute2="value"/>
</elements>
Assuming I want to get attribute1 on the 2nd element.
Thanks in advance.
Update: Found the solution. It is xmlAttrs(root[[2]])[['attribute1']]

In Robot Frame Work How Can we use value returned from one keyword as input argument to other keyword directly(without use of variable)

How Can we use value returned from one keyword as input argument to other keyword directly (without assigning return value to variable)
Currently I am maintaining all Web Elements or Variable names and corresponding xpath in an Excel Sheet. I get XPath using keyword read_xpath by passing web element/variable name as argument.
I store the xpath in separate variable then make use of it for other or Next line keyword. Since I need to use a variable for each XPath access, i am trying to find out, whether is there any way to directly use one keyword output as input to other keyword without assigning it to variable.
The main purpose of storing XPath in Excel sheet is to avoid multiple test cases change with single Element XPath change, making just single change on Excel Sheet should be sufficient.
For Example:
read_xpath reads values from a Excel sheet, Excel sheet has two columns, one variable name and second one is xpath. Function read_xpath(element) takes variable name as input and gives back xpath.
xlread.py file looks like xlread.py code Excel Sheet look like Excel sheet
sample.robot file looks as below
${login_user_textctrl}= read_xpath username_textctrl
clear element text ${login_user_textctrl}
Input text ${login_user_textctrl} admin
Now let us check how I used read_xpath keyword in my robot file
I called Keyword read_xpath with argument username_texctrl, which returns xpath for username text Control which is stored in variable ${login_user_textctrl}. Now Input Text ${login_user_textctrl} admin works fine.
For below code
clear element text read_xpath username_textctrl
I am getting, clear element text requires 1 argument but two arguments provided, is there any way i can use the the value returned from Keyword(read_xpath) as input to other keyword directly, without assigning it to any variable?
Thanks in advance.
Separating your locators from your robot code through an Object Repository is often done when the locators are reused a lot. An alternative implementation paradigm is to use the Page Object Model. An example of such an implementation can be found in the Robotframework-PageObjectLibrary.
In case you still prefer the Object Repository approach, then using the Selenium2Library keyword Add Location Strategy might be of interest to you. Below is a working example which uses a YAML file as it's OR to fetch the search input box and search button from Google.
ObjectRepo.yaml
OR:
searchbox: '//input[#id="lst-ib"]'
searchbutton: '//button[#id="_fZl"]'
Robot Script
*** Settings ***
Library Selenium2Library
Variables ObjectRepo.yaml
*** Test Cases ***
Yaml Object Repository
[Setup] Add Location Strategy yro Yaml Locator Strategy
Open Browser http://www.google.com Chrome
Input Text yro=searchbox Robot Framework
Click Element yro=searchbutton
Sleep 3s
[Teardown] Close All Browsers
*** Keywords ***
Yaml Locator Strategy
[Arguments] ${browser} ${criteria} ${tag} ${constraints}
${xpath}= Set Variable ${OR.${criteria}}
${retVal}= Get Webelement xpath=${xpath}
[Return] ${retVal}
As you can see the abstraction through a custom locator keyword allows for clearner code and not require you to fetch into a variable for later reuse.
You can use Evaluate to call python functions directly. Example:
${my element to clear}= Evaluate read_xpath("username_textctrl") xlread
See the documentation here
(Note: Example is untested and you should check if xlread is in PYTHONPATH, or found at test run time)

getNodeSet {XML} not working when XML root node contains "xlmns" attribute

I have an xml document like this
<MasterDataSet xmlns="http://tempuri.org/MasterDataSet.xsd">
<t_attribute>
<class_id>2</class_id>
<description>Latitude</description>
</t_attribute>
<t_object>
<name>Ship</name>
</t_object>
...
</MasterDataSet>
With many "t_attribute" and "t_object" nodes. I want to get a node set of all the "t_object" nodes so I use getNodeSet with xPath:
library("XML")
emtree0 <- xmlParse("EM0.xml", useInternalNodes = TRUE)
onlyobjects <- getNodeSet(emtree0,"/MasterDataSet//t_object")
But this returns an empty list.
However if I modify the XML file to look like this, i.e. if I remove the xmlns attribute, it works perfectly:
<MasterDataSet>
<t_attribute>
...
Any suggestions to make the code work without having to remove the xmlns attribute?

xdmp:document-load throws XDMP-DOCDUPATTR for malformed html document, even with repair=full

I used this expression to load an html document:
xdmp:document-load("http://example.com/index.html",
<options xmlns="xdmp:document-load" xmlns:http="xdmp:http">
<uri>/documents/content.xml</uri>
<repair>full</repair>
<format>xml</format>
</options>
The repair full option works well with unclosed tags. But one of the tags has two attributes with the same name, and this causes an error XDMP-DOCDUPATTR.
Is there a way to avoid this error?
You can try getting the document as text and then applying tidy -- there's an example at the end of:
http://docs.marklogic.com/xdmp:tidy
Hoping that helps
You could also load HTML documents as flat text: <format>text</format> instead of <format>xml</format>. The document will be a single text node. All the HTML will be preserved, but there will be no XML structure so XPath won't be useful.

Resources