Articles On Testing

Wecome to http://www.articlesontesting.com !!!

Articles On Testing

Wecome to http://www.articlesontesting.com !!!

Articles On Testing

Wecome to http://www.articlesontesting.com !!!

Articles On Testing

Wecome to http://www.articlesontesting.com !!!

Articles On Testing

Wecome to http://www.articlesontesting.com !!!

Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

XML File handling Using QTP


QTP Descriptive programming with xml file handling

The following example uses the ItemByName property to confirm that the QTP attribute was added.

Set doc = XMLUtil.CreateXML()

doc.LoadFile "c:\example.xml"

Set root = doc.GetRootElement()

Set attrs = root.Attributes()

msg = "At first I had " & attrs.Count() & " attributes. "

root.AddAttribute "QTP","Great"

Set attr = attrs.ItemByName("QTP")

if attr is nothing then

msgbox "Failed to add attribute!!!"

end if

msg = msg + "Now I have " & attrs.Count() & " attributes. "

root.RemoveAttribute "QTP"

msg = msg + "Again I have " & attrs.Count() & " attributes."

msgbox msg


For gaining more insights in the automation using QTP log on to below url :

Automation Testing Using QTP


XML Part 1

The following JavaScript fragment loads an XML document
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}

The following code loads and parses an XML string:
if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(text,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(text);
  }
function loadXMLString(txt)
{
if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt);
  }
return xmlDoc;
}

XML DOM Properties
These are some typical DOM properties:
    * x.nodeName - the name of x
    * x.nodeValue - the value of x
    * x.parentNode - the parent node of x
    * x.childNodes - the child nodes of x
    * x.attributes - the attributes nodes of x
Note: In the list above, x is a node object.

XML DOM Methods
    * x.getElementsByTagName(name) - get all elements with a specified tag name
    * x.appendChild(node) - insert a child node to x
    * x.removeChild(node) - remove a child node from x
Note: In the list above, x is a node object.
The JavaScript code to get the text from the first element in books.xml:
txt=xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue
After the execution of the statement, txt will hold the value "Everyday Italian"
Explained:
    * xmlDoc - the XML DOM object created by the parser.
    * getElementsByTagName("title")[0] - the first element
    * childNodes[0] - the first child of the element (the text node)
    * nodeValue - the value of the node (the text itself)
Accessing Nodes
You can access a node in three ways:
1. By using the getElementsByTagName() method
2. By looping through (traversing) the nodes tree.
3. By navigating the node tree, using the node relationships.

* getElementsByTagName() returns all elements with a specified tag name.
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title");
for (i=0;i
  {
  document.write(x[i].childNodes[0].nodeValue);
  document.write("
");
  if (x[i].nodeType==1)
  {//Process only element nodes (type 1)
  document.write(x[i].nodeName);
  document.write("
");
  }
The following code navigates the node tree using the node relationships:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
y=xmlDoc.getElementsByTagName("book")[0].firstChild;
for (i=0;i
{
if (y.nodeType==1)
  {//Process only element nodes (type 1)
  document.write(y.nodeName + "
");
  }
y=y.nextSibling;
}

* Node Properties
In the XML DOM, each node is an object.
Objects have methods and properties, that can be accessed and manipulated by JavaScript.
Three important node properties are:    * nodeName
    * nodeValue
    * nodeType
The nodeName property specifies the name of a node.

    * read-only
    * nodeName of an element node is the same as the tag name
    * nodeName of an attribute node is the attribute name
    * nodeName of a text node is always #text
    * nodeName of the document node is always #document
The nodeValue property specifies the value of a node.
    * nodeValue for element nodes is undefined
    * nodeValue for text nodes is the text itself
    * nodeValue for attribute nodes is the attribute value

The following code retrieves the text node value of the first element:

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
x.nodeValue="Easy Cooking";

The nodeType property specifies the type of node.
NodeType is read only.
The most important node types are:
Node type     NodeType
Element              1
Attribute            2
Text                   3
Comment          8
Document        9


xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('title').length;
xmlDoc=loadXMLDoc("books.xml");
//the x variable will hold a node list
x=xmlDoc.getElementsByTagName('title');
for (i=0;i
{
document.write(x[i].childNodes[0].nodeValue);
document.write("
");
}
The attributes property of an element node returns a list of attribute nodes.
This is called a named node map, and is similar to a node list, except for some differences in methods and properties.
A attribute list keeps itself up-to-date. If an attribute is deleted or added, the list is automatically updated.
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book')[0].attributes;
document.write(x.getNamedItem("category").nodeValue);
document.write("
" + x.length);

The example below loops through all child nodes of , and displays their names and values:

All modern browsers support the W3C DOM specification.
However, there are some differences between browsers. One important difference is:
    * The way they handle white-spaces and new lines
Internet Explorer will NOT treat empty white-spaces, or new lines as text nodes, wile other browsers will.
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.documentElement.childNodes;
document.write("Number of child nodes: " + x.length);

DOM - Parent NodeAll nodes has exactly one parent node. The following code navigates to the parent node of :    
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
document.write(x.parentNode.nodeName);

Firefox, and some other browsers, will treat empty white-spaces or new lines as text nodes, Internet Explorer will not.
This causes a problem when using the properties: firstChild, lastChild, nextSibling, previousSibling.
To avoid navigating to empty text nodes (spaces and new-line characters between element nodes), we use a function that checks the node type:
  

  function get_nextSibling(n)
{
y=n.nextSibling;
while (y.nodeType!=1)
  {
  y=y.nextSibling;
  }
return y;
}
The nodeValue property is used to get the text value of a node.
The getAttribute() method returns the value of an attribute.

Get the Value of an Element
In the DOM, everything is a node. Element nodes does not have a text value.
The text of an element node is stored in a child node. This node is called a text node.
The way to get the text of an element, is to get the value of the child node (text node).
Get an Element Value
The getElementsByTagName() method returns a node list containing all elements with the specified tag name
in the same order as they appear in the source document.
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
The childNodes property returns a list of child nodes. The element has only one child node. It is a text node.
The following code retrieves the text node of the element:
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
The nodeValue property returns the text value of the text node:
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
txt=y.nodeValue;

Get the Value of an Attribute

In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.
The way to get the value of an attribute, is to get its text value.
This can be done using the getAttribute() method or using the nodeValue property of the attribute node.
Get an Attribute Value - getAttribute()
The getAttribute() method returns an attribute value.
The following code retrieves the text value of the "lang" attribute of the first element:
xmlDoc=loadXMLDoc("books.xml");
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
Get an Attribute Value - getAttributeNode()
The getAttributeNode() method returns an attribute node.
The following code retrieves the text value of the "lang" attribute of the first element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].getAttributeNode("lang");
txt=x.nodeValue;

Change the Value of an Element

In the DOM, everything is a node. Element nodes do not have a text value.
The text of an element node is stored in a child node. This node is called a text node.
The way to change the text of an element, is to change the value of the child node (text node).
Change the Value of a Text Node
The nodeValue property can be used to change the value of a text node.
The following code changes the text node value of the first element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="Easy Cooking";
Change the Value of an Attribute
In the DOM, attributes are nodes. Unlike element nodes, attribute nodes have text values.
The way to change the value of an attribute, is to change its text value.
This can be done using the setAttribute() method or using the nodeValue property of the attribute node.
Change an Attribute Using setAttribute()
The setAttribute() method changes the value of an existing attribute, or creates a new attribute.
The following code changes the category attribute of the element:
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("category","food");

Change an Attribute Using nodeValue
The nodeValue property can be used to change the value of a attribute node:
xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book")[0]
y=x.getAttributeNode("category");
y.nodeValue="food";


QTP XML Usage


We can create an Object Repository Via XML as Well


Function XmlController()
    Set doc = CreateObject("Microsoft.XMLDOM")
    doc.async = false
    doc.ValidateonParse = False
    doc.resolveExternals = False
    doc.LoadXml("")
    doc.save "C:\Test.xml"
    Set doc= Nothing
    Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
    objXMLDoc.async = False
    objXMLDoc.load("C:\Test.xml")
    Dim objCurrNode, objNewNode, objNewText
    Call makeDisc(objXMLDoc)
    Call makeFiles(objXMLDoc)
    objXMLDoc.save("C:\Test.xml")
    set objXMLDoc =Nothing
End Function
'*******************************
'*  makeDisc                                          *
'*******************************
Sub makeDisc(objXMLDoc)
    getElem= GetElement()   
    getNo= GetNode()   
    Set objNewNode = objXMLDoc.createElement(getElem)
    Set objNewText = objXMLDoc.createTextNode(getNo)
    objNewNode.appendChild(objNewText)
    Set objCurrNode = objXMLDoc.documentElement
    objCurrNode.appendChild(objNewNode)
    Set objCurrNode = objCurrNode.lastChild
End Sub  'makeDisc subroutine

'******************************
'*  makeFiles                                       *
'******************************
Sub makeFiles(objXMLDoc)
    Set Root = objXMLDoc.documentElement
    Set NodeList = Root.getElementsByTagName("disc")
    For Each Elem In NodeList 
         Set objNewNode = objXMLDoc.createElement("title")
         Set objNewText = objXMLDoc.createTextNode("AVI 2")
         objNewNode.appendChild(objNewText)
         Set objCurrNode = objXMLDoc.documentElement
         objCurrNode.appendChild(objNewNode)
         Set objCurrNode = objCurrNode.lastChild
         objNewNode.appendChild(objNewText)
    Next

End Sub  'makeDisc subroutine

Function GetElement()
    message = "Enter Project Id"
    title = "Input Project id value"
    var = InputBox(message, title)
    Msgbox var
    GetElement =var
End Function

Function GetNode()
    message = "Enter Project Id"
    title = "Input Project id value"
    var = InputBox(message, title)
    Msgbox var
    GetNode =var
End Function

Call  XmlController

REST


What is REST?


REST is a model for creating services that embraces the model that the web uses. It is a model that is based on resources that are identified by a unique URI. Then, what you are intending to do with the resource is specified by a transport protocol verb. The model is formally not bound to HTTP but for all intents and purposes it currently is. The verbs generally used are: GET, PUT, DELETE and POST.


* GET – Retrieve the resource
* PUT – Update the resource
* DELETE – Delete the resource
* POST – Create the resource


There is some debate about whether you should also use PUT for creation and POST also gives a general catch-all for operations that don't really conform to CRUD (Create, Read, Update, Delete) operations.

Let's look at an example: we want to retrieve the list of products from the products corp. Order system. SOAP sends a message, using HTTP POST, to the order system endpoint (say, http://www.products.com/orderservice) with an action header of GetProducts. With REST we issue an HTTP GET to the URI http://www.products.com/orderservice/products. It is the verb that determines what we are trying to do and the URI that determines what we are trying to do it to. But more than this, REST based systems also return the URIs for the next valid set of operations depending on the context of where we are in the application protocol. So let's look at why this model doesn't suffer from the issues with SOAP detailed above.


HTTP scales


The fact that each resource has a different URL does not imply that they should all be on the same host. There is no need for a single "endpoint". This allows us, for example, to horizontally partition our data on to separate server farms where customers A-M hit one set of machines and customers N-Z hit another. In addition all read operations (namely those that do not alter state on the server) use HTTP GET. GET operations can be cached using the HTTP 1.1 caching directives which allows the service to offload traffic on to the general web infrastructure.
The application protocol is built into the message exchange

A fundamental part of REST is that the next set of valid operations is encoded in the response from the last. This means that as long as you only use actions from the last operation you will always perform a valid operation according to the application protocol.
The URI gives you context

The URI does not have to be a static thing but is inherently a data driven entity. Let's use an expense claim service: we can create a new expense claim passing a number of line items using
POST http://products.com/expenseclaim

This returns the next valid actions one of which could be submitting the expense claim to accounts. This action could be
POST http://products.com/expenseclaim/submission/456

The 456 in the URI is the identifier for the created expense claim. The crucial thing here is that the user can save the URI and then go home for the night. In the morning they simply submit to the URI and the context of where they were in the expense claim submission is maintained.
The payload is whatever makes sense

REST does services very often use XML as a payload but it does not bind you to it if some other format makes more sense. For example, if an AJAX application wants to use your service it makes more sense to return the data as JSON than XML; if the resource is an image then use an image.




Is REST perfect?


So if REST is so great should I always build my systems as REST based ones? Well some people would argue that but there are issues in REST.
No metadata

As may be apparent from the REST benefits above, REST is a pragmatic way of building services and this pragmatism currently means there is no metadata. In other words the consumer has to implicitly know what a service requires and what it may return. Hopefully testing the application should drive out whether assumptions about message formats are correct and the pragmatism does not absolve the service provider from taking a responsible attitude to service evolution.
No standard for actions

The next valid actions are a crucial concept in REST and yet there is no standard way of encoding this information and no agreed place of putting this information. There has been some discussion about using ATOMPub as a mechanism but this is far from an agreed standard.
Tooling is rudimentary

On the .NET platform there is little tool support for consuming REST services. With the lack of metadata you are left with having to consume the saw format of the message whether this XML, JSON, an image or something else. There is growing support on the service side with .NET 3.5 WCF extensions and the upcoming ASP.NET routing infrastructure shipping in .NET 3.5 SP1.


Conclusion


REST is a very powerful way to build services. The model lends itself to high scalability as it uses the same model as the web. It is particularly applicable to services that are exposed on to the public internet as generally all a consumer needs to be able to do is manipulate XML and talk HTTP - which every platform supports.

In the first part of this article I have introduced REST and outlined its benefits and issues. In part two I will show you how to build and consume REST based services using WCF, ASP.NET and LINQ to XML.

AdRotator with Time Interval


This Code will help us to display ads with equivalent Time interval




This code will change the ads, every 5 seconds.


Add this code to your .aspx page:

















Create a XML file with following format, also add some images to your project, customize this xml code based on imagename, navigation url., etc.,


Xml File Content: vip.xml :






\Path\vip8.bmp

http://localhost:8080/



Hello World Of .NET Developers.





\Path\vip9.bmp

http://localhost:8080/



Hello World Of .NET Developers.





\Path\viplav1.bmp

http://localhost:8080/



Hello World Of .NET Developers.





\Path\viplav.bmp

http://localhost:8080/



Hello World Of .NET Developers.





\Path\viplav2.bmp

http://localhost:8080/



Hello World Of .NET Developers.





\Path\viplav3.bmp

http://localhost:8080/



Hello World Of .NET Developers.





\Path\viplav5.bmp

http://localhost:8080/



Hello World Of .NET Developers.

*/