selenium JUnit_自动化测试_断言assertThat_字符串、数值、集合匹配

闲来无事,重新封装下,存档以备后面用到。

1、失败案例截图保存函数;

2、断言函数assertThat再封装

3、字符串、数值、集合匹配

初始化函数

package com.test.common;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.*;

public class InitFunction {
	
	public static WebDriver driver;
	
	public static WebElement elenium;
	
	public static Date date = new Date();
	
	public static int waitTime = 1000;//等待时间
	
	public static String yMdhms = "yyyyMMddhhmmss";
	public static String yMd = "yyyyMMdd";
	
	public static String userName;// = "test";//账号
	public static String userPassword;// = "test";//密码	
	public static String urlString = "http://www.aaaaaaa.com/login";//网址
	
	// 打开IE浏览器
	public void setDriver(){
				
		  System.setProperty("webdriver.ie.driver", "E:\\Soft\\SeleniumSoft\\IEDriverServer.exe");
		  DesiredCapabilities ieCapabilities = DesiredCapabilities
				     .internetExplorer();
				   ieCapabilities
				     .setCapability(
				       InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
				       true);
		driver = new InternetExplorerDriver(ieCapabilities);

	}
	
	// 关闭页面
	public void CloseDriver(){
		driver.quit();
	}
	
	//---------------------------------1.begin_控件再封装 -------------------------------
	// 控件.Sendkeys 优化 (By.id )
	public void ByIdSendkeysFun(String idStr,String keyStr){
		elenium = driver.findElement(By.id(idStr));
		elenium.clear();
		elenium.sendKeys(keyStr);
	}
	// 控件.Sendkeys 优化(By.name)
	public void ByNameSendkeysFun(String nameStr,String keyStr){
		elenium = driver.findElement(By.name(nameStr));
		elenium.clear();
		elenium.sendKeys(keyStr);
	}
	
	// 控件.click 优化 (By.id )
	public void ByIdClickFun(String idStr){ 
		elenium = driver.findElement(By.id(idStr));
		elenium.click();
	}
	// 控件.click 优化(By.name)
	public void ByNameClickFun(String nameStr){
		elenium = driver.findElement(By.name(nameStr));
		elenium.click();
	}
	//---------------------------------end_控件再封装-----------------------------------
	
	//---------------------------------2.begin_通用函数封装 ----------------------------
	
	public void login(String userName1,String userPassword1){
		  ByIdSendkeysFun("username_show", userName1);
		  ByIdSendkeysFun("password_show", userPassword1);
		  ByIdClickFun("btn_login");
	}
	
	public void login1(String userName1,String userPassword1,String nameID,String passwordId,String btnId){
		  ByIdSendkeysFun(nameID, userName1);
		  ByIdSendkeysFun(passwordId, userPassword1);
		  ByIdClickFun(btnId);
	}
	
	/*
	 * 返回当前日期字符串;
	 * 格式以参数为准:yyyyMMddhhmmss
	 * 
	 */
	public String editeDateString(){

		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		date=calendar.getTime(); 
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddhhmmss");
		String dateString = formatter.format(date);
		return dateString;
	}
	
	/*
	 * 截图并保存,图片名称以(当前年月日时分秒 +备注说明) 命名
	 */
	public void printScr(String remarksStr){
		String dateString = editeDateString();
		File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
	    try {
			FileUtils.copyFile(srcFile, new File("c:\\"+dateString+remarksStr+".png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//---------------------------------end_通用函数封装----------------------------------
	
	//---------------------------------3. begin_断言函数再封装 (assertThat)--------------
	
	//---------------------------------3.1  对象 断言函数 begin--------------------------
	/*
	 * 测试对象是否相等,使用Object.equals方法
	 * if(testValue=s)测试通过 else测试不通过
	 */
	public void assertThat_equalTo(String testValue,Object s){
		assertThat(testValue, equalTo(s));
	}
	
	//---------------------------------3.2 字符串 断言函数 begin-------------------------
	/*
	 * 比较两个字符串是否相等 ,忽略大小写;
	 *(if(测试字符testValue=s) 测试通过)else 测试不通过
	 */
	public void assertThat_equalToIgnoringCase(String testValue,String s){
		assertThat(testValue,equalToIgnoringCase(s));
	}
	
	/*
	 * 测试变量在忽略头尾任意空格的情况下是否等于指定字符串
	 * if(testValue 等于忽略任意空格的s)测试通过 else测试不通过
	 */
	public void assertThat_equalToIgnoringWhiteSpace(String testValue,String s){
		assertThat(testValue, equalToIgnoringWhiteSpace(s));
	}
	
	/*
	 * 测试变量是否已指定字符串开头
	 * if(testValue是以s开头的)测试通过 else测试不通过
	 */
	public void assertThat_startsWith(String testValue,String s){
		assertThat(testValue, startsWith(s));
	}
	
	/*
	 * 测试变量是否以指定字符串结尾
	 * if(testValue是以s结束的)测试通过 else测试不通过
	 */
	public void assertThat_endsWith(String testValue,String s){
		assertThat(testValue, endsWith(s));
	}
	
	/*
	 * 测试变量是否包含指定字符
	 * if(testValue中包含s)测试通过 ,else测试不通过
	 */
	public void assertThat_containsString(String testValue,String s){
		assertThat(testValue, containsString(s));
	}

	//---------------------------------3.3 数值 断言函数 begin---------------------------
	/*
	 * 测试变量是否大于指定值
	 *  if(测试值testValue>比较值i) 测试通过,else测试不通过
	 */
	public void assertThat_greaterThan(int testValue,int i){
		assertThat(testValue,greaterThan(i));
	}
	
	/*
	 * 测试变量(testValue)是否同时满足条件1条件2
	 * if(测试值testValue>比较值i1  并(&&) 测试值testValue<i2) 测试通过,else测试不通过
	 */
	public void assertThat_allOf(int testValue,int i1,int i2){
		assertThat(testValue,allOf(greaterThan(i1),lessThan(i2)));
	}
	
	/*
	 * 测试变量(testValue)满足任一条件
	 * if(测试值testValue>比较值i1  或(||) 测试值testValue<i2) 测试通过,else测试不通过
	 */
	public void assertThat_anyOf(int testValue,int i1,int i2){
		assertThat(testValue,anyOf(greaterThan(i1),lessThan(i2)));
	}
	
	/*
	 * 测试变量是否为空
	 * if(测试值testValue不为null)测试通过,else测试不通过
	 */
	public void assertThat_notNullValue(Object testValue){
		assertThat(testValue,notNullValue());
	}
	
	//---------------------------------3.4 集合 断言函数 begin---------------------------
	/*
	 * 测试一个集合是否包含指定元素
	 *  if(测试集合testList中含有字符串s)测试通过,else测试不通过
	 */
	public void assertThat_hasItem(List<String> testList,String s){
		assertThat(testList,hasItem(s));
	}
	/*
	 * 测试一个集合是否包含指定的多个元素
	 *  if(测试集合testList中含有字符串s1和s2)测试通过,else测试不通过
	 */
	public void assertThat_hasItems(List<String> testList,String s1,String s2){
		assertThat(testList,hasItems(s1,s2));
	}
	/*
	 * 测试一个Map,是否包含指定的Key值
	 *  if(测试testMap中含有KEY值s)测试通过,else测试不通过
	 */
	public void assertThat_hasKey(Map<String,String> testMap,String s){
		assertThat(testMap,hasKey(s));
	}
	/*
	 * 测试一个Map,是否包含指定的value值
	 *  if(测试testMap中含有value值s)测试通过,else测试不通过
	 */
	public void assertThat_hasValue(Map<String,String> testMap,String s){
		assertThat(testMap,hasValue(s));
	}
	/*
	 * 测试map中是否还有指定键值对
	 */
	public void assertThat_hasEntery(Map<String,String> testMap,String s1,String s2){
		assertThat(testMap, hasEntry(s1, s2));
	}

	public void assertThat_valueEquel(Map<String,String> testMap,String s1,String s2){
		assertThat(testMap.get(s1),equalTo(s2));
	}
	//---------------------------------end_断言函数----------------------------------
}


测试函数

package com.test.fun;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.testng.annotations.Test;
import com.test.common.InitFunction;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
public class test9 extends InitFunction {
	
	InitFunction initFun = new InitFunction();
	String a = "hello world";
	String s1 = "hello";
	String s2 = "world";
	Object b;
	List<String> testList = new ArrayList<String>();
	Map<String,String> testMap = new HashMap<String,String>();
	
  @Test
  public void testString1() {
	  assertThat(a, equalToIgnoringCase("hello world"));
  }
  @Test
  public void testString2() {
	  assertThat(a, equalToIgnoringCase("hi world"));//失败案例
  }
  @Test
  public void testString3() {
	  initFun.assertThat_equalToIgnoringCase("hello world", "hello world");
  }
  @Test
  public void testString4() {
	  assertThat(a,equalTo("hello world"));
	  assertThat(a, startsWith("h"));
	  assertThat(a, endsWith("d"));
	  assertThat(a, containsString("or"));
	  assertThat(a, equalToIgnoringWhiteSpace("  hello world "));
  }
  
  @Test
  public void testInt1() {
	  int i = 2;
	  assertThat(i, greaterThan(1));

  }
  
  @Test
  public void testInt2() {
	  initFun.assertThat_allOf(5, 4, 6);
  }
  
  @Test
  public void testInt3() {
	  initFun.assertThat_anyOf(5, 4, 3);
  }
  
  @Test
  public void testInt4() {
	  initFun.assertThat_notNullValue(b);//失败案例
  }
  @Test
  public void testList1(){
	  testList.add("hello");
	  testList.add("world");
	  initFun.assertThat_hasItem(testList, s1);
  }
  @Test
  public void testList2(){
	  testList.add("hello");
	  testList.add("world");
	  initFun.assertThat_hasItems(testList, s1,s2);
  }
  @Test
  public void testMap1(){
	  testMap.put("英语", "90");
	  testMap.put("语文", "95");
	  testMap.put("数学", "100");
	  initFun.assertThat_hasKey(testMap, "语文");

  }
  @Test
  public void testMap2(){
	  testMap.put("英语", "90");
	  testMap.put("语文", "95");
	  testMap.put("数学", "100");
	  initFun.assertThat_hasValue(testMap, "95");
  }
  @Test
  public void testMap3(){
	  testMap.put("英语", "90");
	  testMap.put("语文", "95");
	  testMap.put("数学", "100");
	  initFun.assertThat_hasEntery(testMap, "语文", "95");
  }
  @Test
  public void testMap4(){
	  testMap.put("英语", "90");
	  testMap.put("语文", "95");
	  testMap.put("数学", "100");
	  initFun.assertThat_valueEquel(testMap, "数学", "100");
  }
}

猜你喜欢

转载自blog.csdn.net/wjok2009/article/details/89878744
今日推荐