我正在使用 Selenium Webdriver 以及我在 Java 中编写的代码。在 Eclipse 中运行脚本..
目前我的本地目录中有很多属性文件,它的格式为 test.txt 和 test.properties 。一切都是单独的文件。
示例:
1. login.txt
文件包含以下详细信息
loginUsername= test
loginPassword= 1234
url= https//: ....
2。 dropdown.properties
包含以下值
visualizationId=Day,Week,Month,Quarter,Semester,Year,RD Tech Group,ICC,Center,Software Pack,Product,Project,Customer PRs,Severity,Priority
periodId=Last 4 Weeks,Last 52 Weeks,Date Range,Week Range,Month Range,Year To Date
classificationId=All PRs,External PRs,Internal PRs,Customer PRs
topographyId=Overall,Center,ICC,
swpacksId=ADVIP,ADVLEG,ADVSEC,Boot,H323,IBC,MULTI,None,OneOS_EAD,PROXY,SBO,SIP,TDRE,VoDSL
priorityId=Very Urgent,Urgent,Routine,Not Urgent,On Hold,Not Assigned
severityId=Blocking,Major,Minor,Cosmetic,OLD PR
projectId=/dev/v4.3/r4/e12sqq,BATS,1523 Business IAD & Business CPE,[CoCo2M],VQM
我怀疑的是我如何以XML
文件的形式保存所有内容,它应该是通用的,我应该在任何我想要xml属性文件的地方调用。
请帮我解决这个问题。
答案 0 :(得分:0)
首先创建xml文件为 -
<?xml version="1.0"?>
<project>
<logindetails id="1001">
<username>abc</username>
<password>mook kim</password>
</logindetails >
<staff id="1001">
<firstname>abc</firstname>
<lastname>efd</lastname>
<salary>100000</salary>
</staff>
</project>
您可以根据您的要求创建节点(例如 - 员工,登录xml以上)
然后你可以按照要求从xml获取数据 - 例如 - 假设您想从登录中获取用户名,那么您可以将参数(logindetails,username)传递给readXML方法&amp;得到它如下所示 -
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class A {
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
A a = new A();
String username = a.readXML("logindetails","username");
System.out.println(username);
//use username for webdriver specific actions
driver.findElement(By......).sendkeys(username);
}
public String readXML(String searchelement,String tag) throws SAXException, IOException, ParserConfigurationException{
String ele = null;
File fXmlFile = new File("D://NewFile.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName(searchelement);
Node nNode = nList.item(0);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
ele=eElement.getElementsByTagName(tag).item(0).getTextContent();
}
return ele;
}
}