受同學之託,幫忙解析xml文件 使用selenium rc實現自動測試,對於我來說在java方面就是個剛入門的新手。不過還好,查查資料文檔,動動手就可以搞定了。這個解析xml的方法也不外乎那幾種,爲了他能看懂儘快入手,就選取了DOM解析方法。因爲要做個測試登陸的案例,所以需要有賬號密碼,上代碼
public void DomParse(String xmlFile){
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = docbf.newDocumentBuilder();
Document doc = builder.parse(xmlFile);
NodeList list = doc.getElementsByTagName("TestCase");
for (int i = 0; i < list.getLength(); i++) {
username = doc.getElementsByTagName("username").item(i).getFirstChild().getNodeValue();
password = doc.getElementsByTagName("password").item(i).getFirstChild().getNodeValue();
//自動測試帳號密碼的函數調用
test(username,password);
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
使用selenium實現自動測試,我就是寫了個模板,只需要稍微修改就可以用到各個地方
private void test(String username, String password) {
// TODO Auto-generated method stub
String url="http://www.baidu.com";
Selenium selenium = new DefaultSelenium("localhost", 4444, "*iexplore", url);
selenium.start();
selenium.open("/");
selenium.type("kw",username);
selenium.click( "su");
selenium.waitForPageToLoad("30000");
selenium.stop();
}
利用testNG測試框架 寫個測試類
public class LoginTest {
@Test
public void test() {
XmlParse domxml= new XmlParse();
domxml.DomParse("data.xml");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<TestData>
<TestCase>
<username>cheng</username>
<password>dfhl</password>
</TestCase>
<TestCase>
<username>hong</username>
<password>9191</password>
</TestCase>
</TestData>
執行測試類時,要啓動selenium服務,通過命令提示符運行java -jar selenium-server.jar開啓服務,然後運行,會看到百度輸入框中填充了xml文件中username的文本,並單擊了搜索按鍵,跳轉到了顯示搜索結果的頁面,因爲有兩個節點,所以彈出兩個搜索框。