1

I have a unique scenario. There is a web application which is a simulator to check sending of data in XML and getting the data back in xml and verifying few details in xml.

Now the xml data which I am sending has a lot of details. In that xml I will have to insert a parameter which I have defined in my test. I am not able to get, how to send the data as parameter in the xml before sending it.

the xml structre looks like this

id='12345'><version>1.3.4<</version><accno>1234567890</accno>add<address details</> ..........

Now int this xml structure, I have parameterized <accno>1234567890</accno> ... Mean in begin of the script I am declaring accno='1234567890'

Now I want to using accno as parameter in the xml instead of the hard coded value in the xml. Please suggest how to do this.

Toto
  • 89,455
  • 62
  • 89
  • 125
anagraj
  • 109
  • 1
  • 7
  • 2
    XML is not regular, but context-free. Use a proper parser like `Nokogiri` instead of regex. See http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Reactormonk Jan 16 '12 at 12:34
  • @Tass - you should have answered instead of commenting this question. IMHO, your comment is the best answer so far. – Pedro Rolo Jan 16 '12 at 16:48
  • I don't understand why this is tagged for Watir and Watir-webdriver. Are you testing stuff at a web api level here? or are you intending to drive a web-app and validate stuff as it appears in the UI? Or pull the data off the UI and process it further after parsing it etc? It's not clear to me what test method you are trying to use. Watir would be applicable only if you are actually driving a browser. If you are just making calls to something like a REST API, then a gem like REST-Client might be a lot more applicable as a way to interact with the server. – Chuck van der Linden Jan 16 '12 at 19:01
  • I am using Watir and Webdriver to drive the browser. I am working an web application which is more of a simulator to test sending of xml and receving the response in xml itself. It is a finincial domain application where the simulator ( web application ) does some transaction simulating the same what happens when a user does a ecommerce transaction using internet, like buying some thing on ebay using a credit card – anagraj Jan 17 '12 at 06:27

4 Answers4

2

XML is not regular, but context-free. Use a proper parser like Nokogiri instead of regex. See RegEx match open tags except XHTML self-contained tags.

As answer, as requested.

Community
  • 1
  • 1
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
1

First identify the pattern, then replace it using gsub!

xml_data.gsub! (pattern, replacement)

http://ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.gsub_oh

1

I will say editing xml, by regex is a bad idea.

but just to answer the direct question use gsub. eg.

str.gsub(/reg_match/, newstring)

but better way of doing it will be use of hpricot,

Or you can also use ruby templates.

require 'erb'
require 'ostruct'

data = {:accno => "1234567890"}
variables = OpenStruct.new(data)

template = "<id='12345'><version>1.3.4</version><accno><%= accno%></accno>"

res = ERB.new(template).result(variables.instance_eval { binding })
puts res
CantGetANick
  • 1,799
  • 15
  • 25
  • I get an error... Syntax error, unexpected tINTEGER, expecting $end.....and it points out the place where the values is between ''..i.e Looks like it is unable to read that – anagraj Jan 16 '12 at 12:24
  • Well, I deleted the '' ... like i was passing only id=12345, now i dont get error ... but the code still puts <%=accno% instead of 1234567890 – anagraj Jan 16 '12 at 12:26
  • just use " instead or ' around the string for var template. – CantGetANick Jan 16 '12 at 12:45
  • Your solution works great. I am able to exactly replace the text in the XML. Thanks you for quick and correct response. The solution rocks.... – anagraj Jan 16 '12 at 13:17
0

The fast way to do it is with gsub (like Rajkaran says). The right way to do it is rexml or some other xml library. Investment should be related to how much you will use this kind of thing in the future.

Dave McNulla
  • 2,006
  • 16
  • 23