0

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

Code:

public class LoginPage extends BaseClass {    

    @FindBy(id="EmailAddress")
    WebElement username;

    @FindBy(id="Password")
    WebElement password;

    @FindBy(id="EligibilityNumber")
    WebElement click;

    @FindBy(id="CaptchaInputText")
    WebElement captchavalue;

    @FindBy(id="loginBtn")
    WebElement Login;    

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

    public personal_details login(String username1, String password1)
    {           
        username.sendKeys(username1);
        password.sendKeys(password1);
        Login.click();

        return new personal_details();
}

testutils:

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);

          doc.getDocumentElement().normalize();

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

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

          System.out.println("----------------------------");

          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) 
      {
         e.printStackTrace();
      }
   }
}

Test method code:

public class LoginTest extends BaseClass {

        LoginPage loginpage;

        personal_details pd;

        private String password1;

        public LoginTest()
        {
            super();
        }

        @BeforeMethod
        public void setup()
        {
            initialization();

            loginpage=new LoginPage();   
        }

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

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.

0

XML code is working.

If someone is facing issue with import part then use below Code:.

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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