How to pass data from XML file into application and I'm using page object model with page factory.


public class LoginPage extends BaseClass {    

    WebElement username;

    WebElement password;

    WebElement click;

    WebElement captchavalue;

    WebElement Login;    

    public LoginPage()
        PageFactory.initElements(driver, this);

    public personal_details login(String username1, String password1)

        return new personal_details();


XML code:

public static void readxml()
   try {
          File fXmlFile = new File("E:\\DATAFLOWAUTOMATIONFRAMEWOK\\xml\\a.xml");

          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

          Document doc = dBuilder.parse(fXmlFile);


          System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

          NodeList nList = doc.getElementsByTagName("staff");


          for (int temp = 0; temp < nList.getLength(); temp++) {

             Node nNode = nList.item(temp);

             System.out.println("\nCurrent Element :" + nNode.getNodeName());

             if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;

                String id1= eElement.getAttribute("id");

                //String  username1=eElement.getElementsByTagName("username").item(0).getTextContent();
                //String password1= eElement.getElementsByTagName("password").item(0).getTextContent();
      catch (Exception e) 

Test method code:

public class LoginTest extends BaseClass {

        LoginPage loginpage;

        personal_details pd;

        private String password1;

        public LoginTest()

        public void setup()

            loginpage=new LoginPage();   

        public void loginmethod()
           loginpage.login(username1,password1);// how to pass code here, could you please let me know

1 Answer 1


Too much work here for just an answer. I will rather give you the direction on where to move.

First you should learn how to supply your tests with test data:

Then you should rework your readxml() method. Currently it doesn't do anything valuable. It should integrate what it parses from your file into data provisioning mechanism of the particular framework you have chosen.

A separate issue is how you parse your file. It actually depends on the file structure.

Once you have all the issues resolved your unit-testing framework will take care of proper provisioning your test with the data from your xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.