使用对象库的作用是能够把测试页面上元素的定位方式和定位表达式存放在配置文件中,做到定位数据和程序分离,策划书程序写好以后,可以方便不具备编程能力的测试人员进行自定义修改和配置。
下面以http://mail.sohu.com作为例子。
要实现定位与数据分离,需要分3部进行:
第一步:实现工具类Object工具类,供测试程序调用。
package cn.om.webdriverapi; import java.io.FileInputStream; import java.io.FileNotFoundException; 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 (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block 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]; 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("tag")||locatorType.toLowerCase().equals("tagname")) 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("css")) return By.cssSelector(locatorValue); else if(locatorType.toLowerCase().equals("xpath")){ return By.xpath(locatorValue); } else { throw new Exception("输入的locator type未在程序中定义:"+locatorType); } } }
第二步:对要测试的网页进行分析,把需要定位的元素的定位表达式存放在配置文件中(此处为ObjectMap.properties)
SohuMai.HomePage.username=xpath:.//*[@id='theme']/form/div[1]/div[1]/input SohuMai.HomePage.password=xpath:.//*[@id='theme']/form/div[2]/div[1]/input SohuMai.homePage.submitButton=xpath:.//*[@id='theme']/form/div[5]/input
第三步:编写测试类
package cn.om.webdriverapi; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterMethod; public class TestSohuMailLoginByObjectMap { private WebDriver driver; private ObjectMap objectmap; String url; @Test public void testSohuMailLoginByObjectMap() { driver.get(url); // 每次获取元素的默认超时时间为10秒。如果不设置,没获取完就超时了。显示失败 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); try { objectmap = new ObjectMap("F:\\workspace\\WebDriver API\\ObjectMap.properties"); WebElement username = driver.findElement(objectmap.getLocator("SohuMai.HomePage.username")); System.out.println("-------------------------------------------------"); WebElement password = driver.findElement(objectmap.getLocator("SohuMai.HomePage.password")); System.out.println("-------------------------------------------------"); WebElement submitButton = driver.findElement(objectmap.getLocator("SohuMai.homePage.submitButton")); System.out.println("-------------------------------------------------"); username.sendKeys("fosterwu"); password.sendKeys("1111"); submitButton.click(); } catch (Exception e) { e.printStackTrace(); } } @BeforeMethod public void beforeMethod() { url = "http://mail.sohu.com"; System.setProperty("webdriver.firefox.bin", "D:/Mozilla Firefox/firefox.exe"); driver = new FirefoxDriver(); } @AfterMethod public void afterMethod() { driver.quit(); } }