selenium 使用对象库

使用配置文件存储测试页面上的定位和定位表达式,做到定位数据和程序的分离

新建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();
  }

}

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值