细数自动化测试中经常用到的功能类

    自动化测试是软件测试技术重要的组成部分,在自动化测试中我们会用到一些功能类,本文在这里介绍几种常用的功能类。

1、WebDriver处理一些弹窗

  import java.util.Set;

  import org.openqa.selenium.Alert;

  import org.openqa.selenium.By;

  import org.openqa.selenium.NoAlertPresentException;

  import org.openqa.selenium.WebDriver;

  import org.openqa.selenium.WebDriverException;

  import org.openqa.selenium.ie.InternetExplorerDriver;

  public class AlertOperate {

  static WebDriver dr = new InternetExplorerDriver();

  public static void main(String args[]) throws InterruptedException{

  dr.get(www.test-edu.com);

  dr.findElement(By.id("lb"))。click();

  try {

  Thread.sleep(2000);

  } catch (InterruptedException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  tanchukuang();

  }

  //处理潜在的1个alert(javascript弹出框)

  public boolean dealPotentialAlert(WebDriver driver,boolean option) {

  boolean flag = false;

  try {

  Alert alert = driver.switchTo()。alert();

  if (null == alert)

  throw new NoAlertPresentException();

  try {

  if (option) {

  alert.accept();

  System.out.println("Accept the alert: " + alert.getText());

  } else {

  alert.dismiss();

  System.out.println("Dismiss the alert: " + alert.getText());

  }

  flag = true;

  } catch (WebDriverException ex) {

  if (ex.getMessage()。startsWith("Could not find"))

  System.out.println("There is no alert appear!");

  else

  throw ex;

  }

  } catch (NoAlertPresentException e) {

  System.out.println("There is no alert appear!");

  }

  return flag;

  }

  //处理非JS弹窗

  public static boolean testNewWindow(){

  //当前窗口句柄

  String currentHandle = dr.getWindowHandle();

  //得到所有窗口的句柄

  Set handles = dr.getWindowHandles();

  handles.remove(currentHandle);

  if (handles.size() > 0) {

  try{

  dr.switchTo()。window(handles.iterator()。next());

  //dr.switchTo()。window(dr.getWindowHandles()。iterator()。next());

  return true;

  }catch(Exception e){

  System.out.println(e.getMessage());

  return false;

  }

  }

  System.out.println("Did not find window");

  return false;

  }

  //一般弹出窗口

  public static void tanchukuang() throws InterruptedException{

  //得到所有窗口

  Set allWindowsId = dr.getWindowHandles();

  //通过查找页面内容得到新的窗口

  for(String windowId : allWindowsId){

  dr.switchTo()。window(windowId);

  Thread.sleep(1000);

  dr.findElement(By.id("TANGRAM__PSP_10__userName"))。sendKeys("test");

  //第一个按钮是确定按钮

  //dr.findElement(By.xpath("//button[@type='button']"))。click();

  //System.out.println(dr.switchTo()。window(windowId)。getTitle());

  break;

  }

  }

  }

  2、一些数据类型转换

  public class Chanage {

  //int to String

  public static String IntToString(int i){

  String s = Integer.toString(i);

  return s;

  }

  //String to int

  public static int StringToInt(String s){

  int i = Integer.parseInt(s);

  return i;

  }

  }

  3、和数据库交互

  import java.sql.DriverManager;

  import java.sql.ResultSet;

  import java.sql.SQLException;

  import java.sql.Statement;

  import com.mysql.jdbc.Connection;

  class ConnMySQL {

  Connection conn;

  Statement stmt;

  ResultSet rs1;

  int rs2;

  public void connection() throws Exception{

  String db = "meeting";

  String driver = "com.mysql.jdbc.Driver";

  String url = "jdbc:mysql://172.16.3.9:3306/"+db;

  String uname = "admin";

  String pwd = "itserver";

  //加载驱动

  Class.forName(driver);

  //连接数据库

  conn = (Connection) DriverManager.getConnection(url, uname, pwd);

  if(!conn.isClosed()){

  //System.out.println("连接成功!");

  }

  }

  //执行查询操作返回ResultSet类型

  public void  executeSql0(String sql) throws SQLException{

  //创建语句对象,用来执行sql语句

  stmt = conn.createStatement();

  //执行sql

  rs1 = stmt.executeQuery(sql);

  while(rs1.next()){

  String name = rs1.getString("meetingId");

  String subject = rs1.getString("e164");

  String regionName = rs1.getString("state");

  System.out.println(name+" "+subject+" "+regionName);

  }

  rs1.close();

  }

//执行查询操作返回ResultSet类型

  public void  executeSql1(String sql) throws SQLException{

  //创建语句对象,用来执行sql语句

  stmt = conn.createStatement();

  //执行sql

  rs1 = stmt.executeQuery(sql);

  while(rs1.next()){

  String id = rs1.getString("id");

  String subject = rs1.getString("subject");

  String startTime = rs1.getString("startTime");

  String endTime = rs1.getString("endTime");

  String organiger = rs1.getString("organiger");

  String status = rs1.getString("status");

  System.out.println(id+" "+subject+" "+startTime+" "+endTime+" "+organiger+" "+status);

  }

  rs1.close();

  conn.close();

  }

  //执行增删改操作返回int类型

  public void executeSql2(String sql) throws SQLException{

  stmt = conn.createStatement();

  rs2 = stmt.executeUpdate(sql);

  stmt.close();

  conn.close();

  }

  }

  4、创建EXCEL

  // 生成Excel的类

  import  java.io.File;

  import  jxl.Workbook;

  import  jxl.write.Label;

  import  jxl.write.WritableSheet;

  import  jxl.write.WritableWorkbook;

  public class CreateExcel{

  public static void main(String args[]){

  //Create_Excel c_e = new Create_Excel();

  //c_e.createexcel();

  //System.out.printf("success!!");

  }

  }

  class Create_Excel{

  public void createexcel(){

  try{

  //  打开文件

  WritableWorkbook book = Workbook.createWorkbook(new File("test.xls"));

  //  生成名为"第一页"的工作表,参数0表示这是第一页

  WritableSheet sheet = book.createSheet("第一页",0);

  //  在Label对象的构造子中指名单元格位置是第一列第一行(0,0)

  //  以及单元格内容为test

  Label label1 = new  Label(0,0,"test");

  Label label2 = new  Label(1,1,"test");

  //  将定义好的单元格添加到工作表中

  sheet.addCell(label1);

  sheet.addCell(label2);

  /*

  * 生成一个保存数字的单元格 必须使用Number的完整包路径,否则有语法歧义 单元格位置是第二列,第一行,值为555

  */

  jxl.write.Number number = new jxl.write.Number(1,0,555);

  sheet.addCell(number);

  //  写入数据并关闭文件

  book.write();

  book.close();

  }catch(Exception e){

  System.out.println(e);

  }

  }

  }

  5、读取EXCEL

  // 读取Excel的类

  import  java.io.File;

  import  jxl.Cell;

  import  jxl.Sheet;

  import  jxl.Workbook;

  /*

  * 参数1:第几个工作表

  * 参数2:第几列

  * 参数3:第几行

  * 参数都从0开始

  * 返回值:当前单元格的数据

  */

  public class ReadExcel{

  static String result;

  static Workbook book;

  static Sheet sheet;

  public static void main(String args[]){

  //readExcel(0,1,0);

  excelNum(0);

  }

  public static String readExcel(int no,int row,int line){

  try{

  book = Workbook.getWorkbook(new File("test.xls"));

  //  获得第一个工作表对象

  sheet = book.getSheet(no);

  //  得到第一列第一行的单元格

  Cell cell1 = sheet.getCell(row,line);

  result = cell1.getContents();

  System.out.println(result);

  book.close();

  }catch(Exception e){

  System.out.println(e);

  }

  return result;

  }

  //返回行数

  public static int excelNum(int no){

  int col = 0;

  int row = 0;

  try{

  book = Workbook.getWorkbook(new File("test.xls"));

  sheet = book.getSheet(no);

  //得到行数和列数

  col = sheet.getColumns(); //列数

  row = sheet.getRows();  //行数

  System.out.println(col+" 列");

  System.out.println(row+" 行");

  book.close();

  }catch(Exception e){

  System.out.println(e);

  }

  return row;

  }

  }

    以上几个功能类,在自动化测试中经常用到,希望能够帮助到对软件测试感兴趣的朋友。

转载于:https://www.cnblogs.com/liyuchen8908/p/3354337.html

你可能感兴趣的:(细数自动化测试中经常用到的功能类)