使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离
新建ObjectMap .properties文件,其本地路径为C:\Users\Administrator\Desktop\ObjectMap .properties
内容如下:
QQ.Login.frame=id:switcher_plogin
QQ.Email.username=id:u
QQ.Email.password=id:p
QQ.Email.login_button=id:login_button
新建ObjectMap.java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
public class ObjectMap
{
Properties properties;
public ObjectMap(String propFile)
{
properties = new Properties();
try
{
FileInputStream in = new FileInputStream(propFile);
properties.load(in);
in.close();
}
catch (IOException e)
{
System.out.println("读取文件对象出错");
e.printStackTrace();
}
}
public By getLocator(String ElementNameInpropFile) throws Exception
{
String locator = properties.getProperty(ElementNameInpropFile);
String locatorType = locator.split(":")[0];
String locatorValue = locator.split(":")[1];
System.out.println("获取定位类型:" + locatorType + "\t 获取的定位表达式" + locatorValue);
if(locatorType.toLowerCase().equals("id"))
{
return By.id(locatorValue);
}
else if(locatorType.toLowerCase().equals("name"))
{
return By.name(locatorValue);
}
else if((locatorType.toLowerCase().equals("classname"))||(locatorType.toLowerCase().equals("class")))
{
return By.className(locatorValue);
}
else if((locatorType.toLowerCase().equals("tagname"))||(locatorType.toLowerCase().equals("tag")))
{
return By.tagName(locatorValue);
}
else if((locatorType.toLowerCase().equals("linktext"))||(locatorType.toLowerCase().equals("link")))
{
return By.linkText(locatorValue);
}
else if(locatorType.toLowerCase().equals("partialLinkText"))
{
return By.partialLinkText(locatorValue);
}
else if((locatorType.toLowerCase().equals("cssSelector"))||(locatorType.toLowerCase().equals("cssSelector")))
{
return By.cssSelector(locatorValue);
}
else if(locatorType.toLowerCase().equals("xpath"))
{
return By.xpath(locatorValue);
}
else throw new Exception("输入的locatorType未在程序中被定义:" + locatorType);
}
}
新建TestQQMailLoginByObjectMap.java
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
public class TestQQMailLoginByObjectMap {
WebDriver driver;
String url = "https://en.mail.qq.com/";
private ObjectMap objectMap;
@Test
public void testQQMailLogin() throws Exception {
try{
objectMap = new ObjectMap("C:\\Users\\Administrator\\Desktop\\ObjectMap .properties");
}catch(Exception e){
System.out.println("生成Object对象失败");
}
driver.switchTo().frame("login_frame");
WebElement frame = driver.findElement(objectMap.getLocator("QQ.Login.frame"));
WebElement username = driver.findElement(objectMap.getLocator("QQ.Email.username"));
WebElement password = driver.findElement(objectMap.getLocator("QQ.Email.password"));
WebElement button = driver.findElement(objectMap.getLocator("QQ.Email.login_button"));
frame.click();
username.sendKeys("输入你的QQ邮箱");
password.sendKeys("输入你的邮箱密码");
button.click();
Thread.sleep(3000);
Assert.assertTrue(driver.getPageSource().contains("QQ邮箱"));
}
@BeforeMethod
public void beforeMethod() {
driver = new FirefoxDriver();
driver.get(url);
}
@AfterMethod
public void afterMethod() {
driver.quit();
}
}
运行结果: