今后可能需要做前端测试架构,参考了网上相关的文章,考虑用python实现下面的功能
https://www.cnblogs.com/wakey/p/10893993.html
-
Solution结构:
-
使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离:
/**
* 使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离
*/
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 ElementNameInpopFile) throws Exception {
// 根据变量ElementNameInpopFile,从属性配置文件中读取对应的配置对象
String locator = properties.getProperty(ElementNameInpopFile);
// 将配置对象中的定位类型存储到locatorType变量,将定位表达式的值存储到locatorValue变量中
String locatorType = locator.split(":")[0];
String locatorValue = locator.split(":")[1];
// 在Eclipse中的配置文件均默认为ISO-8859-1编码存储,使用getBytes方法可以将字符串编码转换为UTF-8编码,以此来解决在配置文件读取中文乱码的问题
locatorValue = new String(locatorValue.getBytes("ISO-8859-1"), "UTF-8");
// 输出locatorType变量值和locatorValue变量值,验证是否赋值正确
System.out.println("获取的定位类型:" + locatorType + "\t 获取的定位表达式:" + locatorValue);
// 根据locatorType的变量值内容判断返回何种定位方式的By对象
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.className(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("css"))) {
return By.cssSelector(locatorValue);
} else if (locatorType.toLowerCase().equals("xpath")) {
return By.xpath(locatorValue);
} else {
throw new Exception("输入的 locator type 未在程序中被定义:" + locatorType);
}
}
}
-
测试数据驱动,数据保存在Excel里:
-
Log功能:
-
PO对象里属性可以改善写成,Python很容易实现动态增加属性,下面的代码能自动生成?
public class LoginPage : PageObject
{
public WebElement Username { get { return getLocator("QQEmail.addressBook.username"); } }
public WebElement Password { get { return getLocator("QQEmail.addressBook.password"); } }
public WebElement LoginButton { get { return getLocator("QQEmail.addressBook.loginButton"); } }
//...
}
-
Action
public class LoginAction
{
public static void Execute(string userName, string passWord)
{
Log.info("访问网址:http://mail.qq.com");
driver.get("http://mail.qq.com");
driver.switchTo().frame("login_frame");
LoginPage loginPage = new LoginPage();
loginPage.Username.clear();
Log.info("在QQ邮箱登录页面的用户名输入框中输入 " + userName);
loginPage.Username.sendKeys(userName);
Log.info("在QQ邮箱登录页面的密码输入框中输入 " + passWord);
loginPage.Password.sendKeys(passWord);
Log.info("单击登录页面的登录按钮");
loginPage.LoginButton.click();
}
}