5.1对象库(UI Map)
目的:能够使用配置文件存储被测试页面上的元素的定位方式和定位表达式,做到定位数据和程序的分离。方便不具备编码能力的测试人员进行修改和配置。
被测网页的网址:
http://www.baidu.com
Java语言版本的API实例代码
首先实现ObjectNap工具类,供测试程序调用
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{
//根据变量ElementNameInpropFile,从属性配置文件中读取对象
String locator = properties.getProperty(ElementNameInpropFile);
//将配置文件中的值存储至locatorType及locatorValue
String locatorType = locator.split(":")[0];
String locatorValue = locator.split(":")[1];
//输出locatorType变量值和locatorValue变量值,验证是否赋值正确
System.out.println("获取的定位类型:"+locatorType+"\t获取的定位表达式"+locatorValue);
//根据localtorType的变量值内容判断返回何种定位方式的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.className(locatorValue);
else if(locatorType.toLowerCase().equals("partiallinktext"))
return By.name(locatorValue);
else if((locatorType.toLowerCase().equals("cssselector")) || (locatorType.toLowerCase().equals("css")))
return By.className(locatorValue);
else if(locatorType.toLowerCase().equals("xpath"))
return By.name(locatorValue);
else
throw new Exception("输入的locator type 未在程序中被定义:"+locatorType);
}
}
建立ObjectMap.properties存储元素表达式的资源文件
Baidu.HomePage.search = id:kw
Baidu.HomePage.button = id:su
在测试类中调用ObjectMap工具类的方法实现测试逻辑
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
public class testSohuMailLoginByObjectMap {
WebDriver driver;
String url = "https://www.baidu.com/";
private ObjectMap objectMap;
@Test
public void testSohuMailLogin()throws Exception {
try {
//声明一个objectMap对象实例,路径为ObjectMap的绝对路径
objectMap = new ObjectMap("E:\\project\\WebDriverAPIProj\\src\\cn\\api1\\ObjectMap.properties");
} catch (Exception e) {
System.out.println("生成ObjectMap对象失败");
}
WebDriverWait wait = new WebDriverWait(driver, 10);
//调用objectMap的getLocator方法获取元素位置
WebElement search = driver.findElement(objectMap.getLocator("Baidu.HomePage.search"));
WebElement button = driver.findElement(objectMap.getLocator("Baidu.HomePage.button"));
//在搜索框中输入要搜索的内容
search.sendKeys("selenium");
//单击搜索按钮
button.click();
//断言判断页面上是否显示了selenium
Assert.assertTrue(driver.getPageSource().contains("selenium"));
}
@BeforeMethod
public void beforeMethod() {
System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to(url);
}
@AfterMethod
public void afterMethod() {
driver.quit();
}
}
原文:https://www.cnblogs.com/z-zzz/p/10521703.html