selenium+Eclipse+testNG读取XML内容登陆系统
2014-04-22 17:22 xiaoluosun 阅读(436) 评论(0) 编辑 收藏 举报package sun;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class XmlLoginDemo
{
private static String username;
private static String password;
public static void main(String[] args)//throws Exception
{
//调用OpenBS()方法
new XmlLoginDemo().OpenBS();
System.out.print("开始登陆:"+"\n"+"用户名:"+username+"\n"+"密 码:"+password+"\n"+"登陆成功!");
}
public void OpenBS()
{
//配置本地firefox浏览器安装路径
System.setProperty("webdriver.firefox.bin", "D:\\FireFox\\firefox.exe");
//调用ReadXml()方法
new XmlLoginDemo().ReadXml();
WebDriver driver=new FirefoxDriver();
//输入URL地址
driver.get("http://ips2.ips.cn");
//输入用户和密码并点击登陆按钮
WebElement login_user = driver.findElement(By.id("username"));
login_user.sendKeys(username);
WebElement login_pwd = driver.findElement(By.id("password"));
login_pwd.sendKeys(password);
WebElement login_button=driver.findElement(By.id("Submit"));
login_button.click();
//处理异常
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
public void ReadXml()
{
String pathProSys=System.getProperty("user.dir");
//XML文件相对路径
String pathProUser="\\data\\user.xml";
//XML文件绝对路径
String path=pathProSys+pathProUser;
File f=new File(path);
SAXReader reader=new SAXReader();
try {
Document doc=reader.read(f);
Element root=doc.getRootElement();
username=root.elementText("username");
password=root.elementText("password");
} catch (DocumentException e) {
e.printStackTrace();
}
}
}