Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Showing posts with label getAttribute. Show all posts
Showing posts with label getAttribute. Show all posts

Manipulation XML using XPath for VBScript/UFT

This article explains how to extract information from an xml file based on the element xpath using Microsoft.XMLDOM and VBSript. You can try using the code as shown below.


''---------------------------------------------------------------------------------
'' Step 1 : Copy the content removing comments in a file and save the file
''----------------------------------------------------------------------------------

'' --------------File Content ------------------------------------------------------
''XMLTextFile = "<books><name>mybook</name><price>50</price><Author rating="high">matt</Author>
''<name>his book</name><price>150</price><Author rating="low">henry</Author>
''<name>twist</name><price>550</price><Author rating="high">henry</Author></books>"

''-------------Call the functions with Parameters------------------------------------
msgbox getXMLTextFileValue("d:\testdta.xml","//price")
msgbox getXMLTextFileValue("d:\testdta.xml","//Author[@rating='high']")
msgbox getxmlAttributeValue("d:\testdta.xml","//Author","rating")
call getAllNodeValues("d:\testdta.xml","//Author")
''-----------------------------------------------------------------------------------

'' Function 1 - Reading the node text
''Arguments - FileName and xpath of node

''-----------------------------------------------------------------------------------
function getXMLTextFileValue(XMLTextFile,NodeXpath)
Set objXML = CreateObject("Microsoft.XMLDOM")

objXML.async = False
objXML.resolveExternals = False
objXML.validateOnParse = False
getXMLTextFileValue = ""
boolxmlParse = objXML.load(XMLTextFile)
if(boolxmlParse) then
 objXML.setProperty "SelectionLanguage", "XPath"
 set objXML_node = objXML.selectSingleNode(NodeXpath)
        getXMLTextFileValue = objXML_node.text
 else
 msgbox "issue in xml file"
End If
End Function

''-----------------------------------------------------------------------------------

'' Function 1 - Reading the node attribute value
''Arguments - FileName and xpath of node. attribute Name of which value is to be returned

''-----------------------------------------------------------------------------------


function getxmlAttributeValue(XMLTextFile,NodeXpath, attributeName)
Set objXML = CreateObject("Microsoft.XMLDOM")

objXML.async = False
objXML.resolveExternals = False
objXML.validateOnParse = False
getxmlAttributeValue = ""
boolxmlParse = objXML.load(XMLTextFile)
if(boolxmlParse) then
 objXML.setProperty "SelectionLanguage", "XPath"
 set objXML_node = objXML.selectSingleNode(NodeXpath)
        getxmlAttributeValue = objXML_node.getAttribute(attributeName)
 else
 msgbox "issue in xml file"
End If
End Function

''-----------------------------------------------------------------------------------

'' Function 1 - Reading the value of all the matching nodes
''Arguments - FileName and xpath of node.

''-----------------------------------------------------------------------------------

function getAllNodeValues(XMLTextFile,NodeXpath)
Set objXML = CreateObject("Microsoft.XMLDOM")

objXML.async = False
objXML.resolveExternals = False
objXML.validateOnParse = False

boolxmlParse = objXML.load(XMLTextFile)
if(boolxmlParse) then
 objXML.setProperty "SelectionLanguage", "XPath"
 set objXML_node = objXML.selectNodes(NodeXpath)
        for each nodet in objXML_node
  msgbox nodet.text
 Next
End If
End Function

How to get attributes values of element in Selenium using getAttribute Method

Using getAttribute, we can extract the value of attribute of an element in Selenium WebDriver. Extracting values of attribute of element can be helpful in a number of ways. Some of the important use of getattribute method are as follows: 


  • Name of all the links or button or an object type in the Page.
  • Validating if object with particular property exists in the Page.
  • Extracting the href value for a button/link.
  • Extracting values of element of a particular class or Id. For e.g: If error message are displayed on the page with the same class. We can get all the error messages displayed getting text attribute for all element with the common class name.

Below code explains use of getattribute in Selenium Webdriver for the above purpose.

 package testproject;  
 import java.io.File;  
 import java.util.List;  
 import org.openqa.selenium.By;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.WebElement;  
 import org.openqa.selenium.ie.InternetExplorerDriver;  
 public class testingC {  
      static WebDriver driver;  
      public static void main(String[] args) throws InterruptedException {  
           // Connect to the Internet driver server and create an instance of Internet explorer driver.       
           File file = new File("D:\\selenium\\IEDriverServer.exe");  
           System.setProperty("webdriver.ie.driver", file.getAbsolutePath());       
           try{  
                driver = new InternetExplorerDriver();  
       driver.manage().window().maximize();  
           }  
           catch(Exception e)  
           {  
                driver=new InternetExplorerDriver();  
           }  
           driver.navigate().to("https://google.com");  
           //function to get names of all the links in the Page  
           getALLNameforObject("link");  
           // function to validate a particular object with particular text appears in the Page  
           ValidateObjectExists("link", "About");       
           //   
      }  
      public static void getALLNameforObject(String strObject)  
       {  
           if(strObject.toLowerCase().trim().contentEquals("link"))  
           {  
                strObject = "a";  
           }  
           if(strObject.toLowerCase().trim().contentEquals("button"))  
           {  
                strObject = "button";  
           }  
           List<WebElement> elemLink = driver.findElements(By.tagName(strObject));  
           int intLinksinPage = elemLink.size();  
           System.out.println(intLinksinPage);  
           for (int i = 0;i<intLinksinPage;i++)  
           {  
                System.out.println("The name of the link " + (i+1) +" in the page is :- " + elemLink.get(i).getAttribute("text"));  
           }  
      }  
      public static void ValidateObjectExists(String strObject, String ObjName)  
       {  
           if(strObject.toLowerCase().trim().contentEquals("link"))  
           {  
                strObject = "a";  
           }  
           if(strObject.toLowerCase().trim().contentEquals("button"))  
           {  
                strObject = "button";  
           }  
           List<WebElement> elemLink = driver.findElements(By.tagName(strObject));  
           int intLinksinPage = elemLink.size();  
           System.out.println(intLinksinPage);  
           for (int i = 0;i<intLinksinPage;i++)  
           {  
                if(elemLink.get(i).getAttribute("text").contentEquals(ObjName))  
                {  
                     System.out.println("Link exists in Page with text " + elemLink.get(i).getAttribute("text"));  
                }  
           }  
      }  
 }