架构

fileutil:

package com.report.html;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.PrintStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.WildcardFileFilter;

public class FileUtil
{
  public static void copyFile(String p_srcFile, String p_destDir)
  {
    try
    {
      FileUtils.copyFileToDirectory(new File(p_srcFile), new File(p_destDir));
      System.out.println("copy file : " + p_srcFile + " to folder: " + p_destDir);
    }
    catch (IOException e)
    {
      System.err.println("Excepion in method copyFile(String p_srcFile,String p_destDir),details info is:" +
        e.getMessage());
    }
  }

  public static void deleteFile(String p_file)
  {
    FileUtils.deleteQuietly(new File(p_file));
    System.out.println("delete file : " + p_file);
  }

  public static File[] getSpecifalFile(String p_fold, String p_regular)
  {
    File dir = new File(p_fold);
    FileFilter fileFilter = new WildcardFileFilter(p_regular);
    return dir.listFiles(fileFilter);
  }
}

-----------------------------------------------------------------------------------------------

package com.report.html;

import com.report.base.IFile;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

public class HtmlFile
  implements IFile
{
  private Logger logger = Logger.getLogger(HtmlFile.class.getName());
  private FileHandler fileHTML;

  public void createLog(String p_logName, boolean p_apped)
  {
    try
    {
      this.fileHTML = new FileHandler(p_logName, p_apped);
    }
    catch (SecurityException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    this.fileHTML.setFormatter(new HtmlReportFormatter());
    this.logger.addHandler(this.fileHTML);
  }

  public void createLog(String p_logName)
  {
    try {
      this.fileHTML = new FileHandler(p_logName);
    }
    catch (SecurityException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    this.fileHTML.setFormatter(new HtmlReportFormatter());
    this.logger.addHandler(this.fileHTML);
  }

  public void closeLog()
  {
    this.fileHTML.close();
    RecordStore.p_pass = 0;
    RecordStore.p_fail = 0;
    RecordStore.result = "";
    RecordStore.expected = "";
    RecordStore.actual = "";
  }

  public void write(String p_info, String p_expected, String p_actual)
  {
    RecordStore.actual = p_actual;
    RecordStore.expected = p_expected;

    if (p_expected.equals(p_actual))
      RecordStore.result = "Pass";
    else
      RecordStore.result = "Fail";
    try
    {
      this.logger.info(p_info);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(" logger write exception!");
    }
  }

  public void write(String p_info)
  {
    this.logger.info(p_info);
  }

  public void write(String p_info, String p_result)
  {
    RecordStore.result = p_result;
    try
    {
      this.logger.info(p_info);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(" logger write exception!");
    }
  }

  public String read()
  {
    return null;
  }

  public String read(String p_key)
  {
    return null;
  }

  public String read(String p_section, String p_key)
  {
    return null;
  }

  public void write(String p_info, Object p_expected, Object p_actual)
  {
    RecordStore.actual = p_actual;
    RecordStore.expected = p_expected;

    if (p_expected.equals(p_actual))
      RecordStore.result = "Pass";
    else
      RecordStore.result = "Fail";
    try
    {
      this.logger.info(p_info);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(" logger write exception!");
    }
  }
}

----------------------------------------------------------------------

package com.report.html;

import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

public class HtmlFileGlobal
{
  private static Logger logger = Logger.getLogger(HtmlFileGlobal.class.getName());
  private static FileHandler fileHTML;

  public static void createLog(String p_logName)
  {
    try
    {
      fileHTML = new FileHandler(p_logName);
    }
    catch (SecurityException e) {
      e.printStackTrace();
    }
    catch (IOException e) {
      e.printStackTrace();
    }
    fileHTML.setFormatter(new HtmlReportFormatter());
    logger.addHandler(fileHTML);
  }

  public static void closeLog()
  {
    fileHTML.close();
    RecordStore.p_pass = 0;
    RecordStore.p_fail = 0;
    RecordStore.result = "";
    RecordStore.expected = "";
    RecordStore.actual = "";
  }

  public static void write(String p_info, Object p_expected, Object p_actual)
  {
    RecordStore.actual = p_actual;
    RecordStore.expected = p_expected;

    if (p_expected.equals(p_actual))
      RecordStore.result = "Pass";
    else
      RecordStore.result = "Fail";
    try
    {
      logger.info(p_info);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(" logger write exception!");
    }
  }

  public static void write(String p_info)
  {
    try
    {
      logger.info(p_info);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(" logger write exception!");
    }
  }

  public static void setScreenshot(String p_path)
  {
    RecordStore.screenshot = p_path;
  }

  public static String getScreenshot()
  {
    return RecordStore.screenshot;
  }
}

---------------------------------------------------------

package com.report.html;

import java.io.PrintStream;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

public class HtmlReportFormatter extends Formatter
{
  private int i = 0;
  private long setStartTime;
  private long setEndTime;
  private final String HTML_HEADER = "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"><META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"><link rel="stylesheet" href="demo_report_style.css"/><html><head><title>测试报告</title></head><body><div class="page_title"><center><h1>测试报告</h1></center></div><div class="statistics"><table id="statistics_table" class="sortable" align="center" border="0"  style="100%;"><tr><th><b>序号</b></th><th><b>用例描述</b></th><th><b>期待结果</b></th><th><b>实际结果</b></th><th><b>执行时间</b></th><th><b>状态</b></th></tr>";

  private int recordStep()
  {
    this.i += 1;
    return this.i;
  }

  public String format(LogRecord rec) {
    StringBuffer buf = new StringBuffer(1000);

    buf.append("<div class="statistics">");
    buf.append("<tr>");
    buf.append("<td>");
    buf.append(recordStep());
    buf.append("</td>");
    buf.append("<td>");

    buf.append(formatMessage(rec));
    buf.append(' ');
    buf.append("</td>");
    buf.append("<td>");
    buf.append(RecordStore.expected);
    buf.append("</td>");
    buf.append("<td>");
    buf.append(RecordStore.actual);
    buf.append("</td>");
    buf.append("<td>");
    buf.append(HtmlUtil.getCalcDate(rec.getMillis()));
    buf.append("</td>");
    buf.append("<td>");
    if ((RecordStore.result.matches("Pass")) || (RecordStore.result.matches("PASS"))) {
      RecordStore.p_pass += 1;
      buf.append("<b>");
      buf.append("<font color=Green>");
      buf.append(RecordStore.result);
      buf.append("</font>");
      buf.append("</b>");
    } else if ((RecordStore.result.matches("Fail")) || (RecordStore.result.matches("FAIL"))) {
      RecordStore.p_fail += 1;
      buf.append("<a href=" + RecordStore.screenshot + ">");
      buf.append("<b>");
      buf.append("<font color=Red>");
      buf.append(RecordStore.result);
      buf.append("</font>");
      buf.append("</b>");
      buf.append("</a>");
    }
    else
    {
      buf.append("<b>");

      buf.append("");
      buf.append("</b>");
    }

    buf.append("</td>");
    buf.append("</tr>");
    buf.append("</div> ");
    try {
      Thread.sleep(10L);
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }

    return buf.toString();
  }

  public String getHead(Handler h)
  {
    this.setStartTime = System.currentTimeMillis();
    System.out.println("starttime: " + this.setStartTime);
    return "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE"><META HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE"><link rel="stylesheet" href="demo_report_style.css"/><html><head><title>测试报告</title></head><body><div class="page_title"><center><h1>测试报告</h1></center></div><div class="statistics"><table id="statistics_table" class="sortable" align="center" border="0"  style="100%;"><tr><th><b>序号</b></th><th><b>用例描述</b></th><th><b>期待结果</b></th><th><b>实际结果</b></th><th><b>执行时间</b></th><th><b>状态</b></th></tr>";
  }

  public String getTail(Handler h)
  {
    this.setEndTime = System.currentTimeMillis();
    System.out.println("endtime: " + this.setEndTime);

    int p_total = RecordStore.p_pass + RecordStore.p_fail;
    String HTML_Tail;
    String HTML_Tail;
    if (p_total > 0)
    {
      String HTML_Tail;
      if (RecordStore.p_fail > 0)
      {
        HTML_Tail = "</table></PRE><br>&nbsp;开始时间   :" + HtmlUtil.getCalcDate(this.setStartTime) +
          "<br>&nbsp;结束时间      :" + HtmlUtil.getCalcDate(this.setEndTime) +
          "<br>&nbsp;运行时间      :" + HtmlUtil.getDeltaTime(this.setEndTime, this.setStartTime) +
          "<br>&nbsp;执行用例      :" + p_total +
          "<br>&nbsp;用例成功         :" + RecordStore.p_pass +
          "<br>&nbsp;<font color=Red>用例失败      :" + RecordStore.p_fail + "</font>" +
          "<br>&nbsp;成功率(%) :" + HtmlUtil.getPercnet(RecordStore.p_pass, p_total) +
          "<br>&nbsp;<font color=Red>失败率(%) :" + HtmlUtil.getPercnet(RecordStore.p_fail, p_total) + "</font>" +
          "<br><br>" +
          "</BODY></HTML>";
      }
      else HTML_Tail = "</table></PRE><br>&nbsp;开始时间   :" +
          HtmlUtil.getCalcDate(this.setStartTime) + "<br>&nbsp;结束时间   :" +
          HtmlUtil.getCalcDate(this.setEndTime) + "<br>&nbsp;运行时间   :" +
          HtmlUtil.getDeltaTime(this.setEndTime, this.setStartTime) +
          "<br>&nbsp;执行用例      :" + p_total +
          "<br>&nbsp;用例成功      :" + RecordStore.p_pass +
          "<br>&nbsp;用例失败      :" + RecordStore.p_fail +
          "<br>&nbsp;成功率(%) :" + HtmlUtil.getPercnet(RecordStore.p_pass, p_total) +
          "<br>&nbsp;失败率(%) :" + HtmlUtil.getPercnet(RecordStore.p_fail, p_total) +
          "<br><br>" +
          "</BODY></HTML>";
    }
    else { HTML_Tail = "</table></PRE><br>&nbsp;用例执行异常!<br><br></BODY></HTML>"; }


    return HTML_Tail;
  }
}

-------------------------------------------------

package com.report.html;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HtmlUtil
{
  public static String getPercnet(double p_numerator, double p_denominator)
  {
    double percent = p_numerator / p_denominator;
    NumberFormat nt = NumberFormat.getPercentInstance();

    nt.setMinimumFractionDigits(1);
    return nt.format(percent);
  }

  public static String getCalcDate(long millisecs)
  {
    SimpleDateFormat date_format = new SimpleDateFormat(
      "yyyy-MM-dd HH:mm:ss");
    Date resultdate = new Date(millisecs);
    return date_format.format(resultdate);
  }

  public static String formatCurrentTime() {
    SimpleDateFormat date_format = new SimpleDateFormat(
      "yyyy_MM_dd_HH_mm_ss");
    Date resultdate = new Date(System.currentTimeMillis());
    return date_format.format(resultdate);
  }

  public static String getDeltaTime(long p_startTime, long p_endTime) {
    long day = (p_endTime - p_startTime) / 86400000L;
    long hour = (p_endTime - p_startTime) / 3600000L - day * 24L;
    long min = (p_endTime - p_startTime) / 60000L - day * 24L * 60L - hour * 60L;
    long s = (p_endTime - p_startTime) / 1000L - day * 24L * 60L * 60L - hour *
      60L * 60L - min * 60L;

    return day + "天" + hour + "小时" + min + "分" + s + "秒";
  }

  public static void sleep(int p_time)
  {
    try {
      Thread.sleep(p_time * 1000);
    }
    catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
}

----------------------------------------------------------

package com.report.html;

public class RecordStore
{
  static int i = 0;
  static int p_pass = 0;
  static int p_fail = 0;
  static String result = "";
  static Object expected = "";
  static Object actual = "";
  static String screenshot = null;
}

----------------------------------------------

package com.report.ini;

import com.report.base.IFile;
import java.io.File;
import java.io.IOException;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalINIConfiguration;

public class IniFile
  implements IFile
{
  private String fileName = null;
  HierarchicalINIConfiguration ini = null;

  public void createLog(String p_fileName)
  {
    this.fileName = p_fileName;
    File file = new File(this.fileName);
    if (!file.exists()) {
      try
      {
        file.createNewFile();
      }
      catch (IOException e)
      {
        e.printStackTrace();
      }
    }
    try
    {
      this.ini = new HierarchicalINIConfiguration(this.fileName);
      this.ini.setEncoding("utf-8");
      this.ini.load(new File(this.fileName));
    }
    catch (ConfigurationException e) {
      e.printStackTrace();
    }
  }

  public void write(String section, String key, String value)
  {
    this.ini.setProperty(section + "." + key, value);
  }

  public String read(String section, String key)
  {
    return this.ini.getString(section + "." + key);
  }

  public void write(String p_info)
  {
  }

  public void closeLog()
  {
    try
    {
      this.ini.save();
    }
    catch (ConfigurationException e) {
      e.printStackTrace();
    }
  }

  public void write(String p_info1, String p_info2)
  {
  }

  public String read()
  {
    return null;
  }

  public String read(String p_info)
  {
    return null;
  }

  public void write(String p_info1, Object p_info2, Object p_info3)
  {
  }
}------------------------------------------------------------------

package com.report.properties;

import com.report.base.IFile;
import java.io.File;
import java.io.IOException;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class PropertiesFile
  implements IFile
{
  File file;
  String filePath = null;
  PropertiesConfiguration config = new PropertiesConfiguration();

  public void createLog(String p_filePath)
  {
    this.filePath = p_filePath;
    this.file = new File(this.filePath);
    if (!this.file.exists()) {
      try
      {
        this.file.createNewFile();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }

    try
    {
      this.config.load(new File(this.filePath));
    }
    catch (ConfigurationException e) {
      e.printStackTrace();
    }
  }

  public String read(String p_key)
  {
    return this.config.getProperty(p_key).toString();
  }

  public void write(String p_key, String p_value)
  {
    this.config.setProperty(p_key, p_value);
  }

  public void write(String p_info)
  {
  }

  public void write(String p_section, String p_key, String p_info)
  {
  }

  public String read(String p_section, String p_key)
  {
    return null;
  }

  public void closeLog()
  {
    File f = new File(this.filePath);
    try
    {
      this.config.save(f);
    }
    catch (ConfigurationException e1) {
      e1.printStackTrace();
    }

    this.file = null;
    this.config = null;
  }

  public String read()
  {
    return null;
  }

  public void write(String p_info1, Object p_info2, Object p_info3)
  {
  }
}

----------------------------------------------

package com.report.entry;

import com.report.base.IFile;
import com.report.html.HtmlFile;
import com.report.ini.IniFile;
import com.report.log.LogFile;
import com.report.properties.PropertiesFile;
import com.report.txt.TxtFile;
import java.io.PrintStream;

public class ReportEntry
{
  IFile myFile;
  String fileName;

  public void crateLog(String p_file)
  {
    if (p_file.endsWith(".ini"))
      this.myFile = new IniFile();
    else if (p_file.endsWith(".properties"))
      this.myFile = new PropertiesFile();
    else if (p_file.endsWith(".html"))
      this.myFile = new HtmlFile();
    else if (p_file.endsWith(".txt"))
      this.myFile = new TxtFile();
    else if (p_file.endsWith(".log"))
      this.myFile = new LogFile();
    else {
      this.myFile = null;
    }
    if (this.myFile != null) {
      this.myFile.createLog(p_file);
    }
    else
    {
      System.out.println("不支持的文件类型");
      System.exit(0);
    }
  }

  public void closeLog() {
    this.myFile.closeLog();
  }

  public void write(String p_info) {
    this.myFile.write(p_info);
  }

  public void write(String p_info1, String p_info2)
  {
    this.myFile.write(p_info1, p_info2);
  }

  public void write(String p_info1, String p_info2, String p_info3) {
    this.myFile.write(p_info1, p_info2, p_info3);
  }

  public String read(String p_key) {
    return this.myFile.read(p_key).toString();
  }

  public String read(String p_section, String p_key) {
    return this.myFile.read(p_section, p_key);
  }
}

----------------------------------------------------

package com.report.entry;

import java.io.PrintStream;

public class MyMain
{
  public static void main(String[] args)
  {
    ReportEntry re = new ReportEntry();

    re.crateLog("d://config2.properties");
    re.write("url", "www.163.com");
    System.out.println(re.read("url"));
    re.closeLog();
  }
}

-------------------------------------------------设计部分------------------------------------

package com.ml0.autoframe.lib;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.Select;
import com.report.html.HtmlFileGlobal;

public class WebDriverLib {
 ////在javal Logger中getLogger是一个全局的,如果只要名字是相同的,不同的类只要用一个名字的话,Logger起的作用是一样的
 public  Logger logger = Logger.getLogger(DataStore.D_DebugLogger);
 private WebDriver driver=null;
 private FileHandler fileHandler=null;
 private String browser=DataStore.D_Browser;
 private String baseUrl=DataStore.D_URL; 
  
  /**
      * Constructor that takes in the instrumentation.
      *
      * @param instrumentation the {@link Instrumentation} instance
      *
      */
 
  
  public void newSetup(String p_Name)
   
  
   {

  
   if (browser.equalsIgnoreCase("chrome")) {
    // System.setProperty("webdriver.chrome.driver","D:\MyWorkplace\webdriverServer\chromedriver.exe");
    
     driver = new ChromeDriver();

  }else if (browser.equalsIgnoreCase("ie")) {

    
     driver = new InternetExplorerDriver();
   
  }else if (browser.equalsIgnoreCase("safari")) {
    // System.setProperty("webdriver.safari.noinstall","C:\Program Files\Safari\Safari.exe");
    Platform current = Platform.getCurrent();
    if (Platform.WINDOWS.is(current))
     driver = new SafariDriver();


  }else{
       ProfilesIni pi = new ProfilesIni();
       FirefoxProfile profile = pi.getProfile("default");
       driver = new FirefoxDriver(profile);

   }
      
            if (driver!=null)
            {   
             try {
     fileHandler = new FileHandler(DataStore.D_LogPath+File.separator+"Debug.log",true);//后面的true代表追加
   //fileHandler默认的formatter是XML的formatter,我们要求输出跟控制台的格式一样,new SimpleFormatter就行了   
     fileHandler.setFormatter(new SimpleFormatter());
    } catch (SecurityException e) {
     // TODO Auto-generated catch block
     logger.severe(e.toString());
     e.printStackTrace();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
             logger.addHandler(fileHandler);
             HtmlFileGlobal.createLog(DataStore.D_LogPath+File.separator+p_Name+CommonLib.getCurrentTime()+".html");
             driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
       driver.get(baseUrl);
       driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
       driver.manage().window().maximize();
       logger.info(TextStore.T_Init + TextStore.T_Pass);
       //logger.info("初始化成功");
            }
           
   
   }
  
   public void newTeardown(){
   
    logger.info("webdriverLib 销毁"); 
    HtmlFileGlobal.closeLog();
    driver.quit();
    fileHandler.close();
  
   

   }
    
     public static By parseObject(String p_object) {
   String newObjecyt = null;

   if (p_object.startsWith(".//") || p_object.startsWith("//")) {
    return By.xpath(p_object);
   } else if (p_object.startsWith("link=") || p_object.startsWith("Link=")) {
    newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
    return By.linkText(newObjecyt);
   } else if (p_object.startsWith("xpath=")) {
    newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
    return By.xpath(newObjecyt);
   } else if (p_object.startsWith("id=")) {
    newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
    return By.id(newObjecyt);
   } else if (p_object.startsWith("css=")) {
    newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
    return By.cssSelector(newObjecyt);
   } else if (p_object.startsWith("class=")) {
    newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
    return By.className(newObjecyt);
   } else if (p_object.startsWith("tagName=")) {
    newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
    return By.tagName(newObjecyt);
   } else if (p_object.startsWith("name=")) {
    newObjecyt = p_object.substring(p_object.indexOf("=") + 1);
    return By.name(newObjecyt);
   } else
    return null;

  }
  
  
  public void newClick(String p_id){
   
   CommonLib.sleep(DataStore.D_Wait_ShortTime);
   
   try {
    driver.findElement(parseObject(p_id)).click();
    logger.info(TextStore.T_ClickObject + p_id + TextStore.T_Pass);
    //logger.info("点击对象" + p_id + "成功!");  //这样写也是OK的

   }

   catch (Exception e) {
    logger.severe(TextStore.T_Exception + "newClick(String p_id)"
      + TextStore.T_DetailInfo + e.toString());

   }
   
      
  }
    
  public void newType(String p_id,String p_text){
   CommonLib.sleep(DataStore.D_Wait_ShortTime);
   
   try {
    driver.findElement(parseObject(p_id)).clear(); // 输入文字前,清除文本框中的文字
    driver.findElement(parseObject(p_id)).sendKeys(p_text);
    logger.info(TextStore.T_Input + p_text + TextStore.T_To + p_id
      + TextStore.T_Pass);

   } catch (Exception e) {
    logger.severe(TextStore.T_Exception + "newType"
      + TextStore.T_DetailInfo + e.toString());
   }
  }
  
  public void newSelect(String p_id,String p_text){
   CommonLib.sleep(DataStore.D_Wait_ShortTime);
   
   try {
    Select select = new Select(driver.findElement(parseObject(p_id)));
    select.selectByVisibleText(p_text);
    
    logger.info(TextStore.T_SelectListValue + p_id+"内容是"+p_id
      + TextStore.T_Pass);

   } catch (Exception e) {
    logger.severe(TextStore.T_Exception + "newSelect"
      + TextStore.T_DetailInfo + e.toString());
   }
  }
   
  
 
   
   
    public void newVerifyEquals(String p_message, Object p_expected,
    Object p_actual) throws Exception {

   if (p_expected.equals(p_actual)) {
    HtmlFileGlobal.write(p_message, p_expected.toString(), p_actual.toString());//写入html report or debug report
    logger.info("");
   
   } else {
    screenshot();
    HtmlFileGlobal.write(p_message, p_expected.toString(), p_actual.toString());//写入html report or debug report
    logger.severe("");
 

    
   }
  }
   
    public void newAssertEquals(String p_message, Object p_expected,
    Object p_actual) throws Exception {
      
   if (p_expected.equals(p_actual)) {
    HtmlFileGlobal.write(p_message, p_expected.toString(), p_actual.toString());//写入html report or debug report
    logger.info("");
   
   } else {
    screenshot();
    HtmlFileGlobal.write(p_message, p_expected.toString(), p_actual.toString());//写入html report or debug report
    HtmlFileGlobal.closeLog();
       driver.quit();
    logger.severe("");
 

    
   }
  }
   
   
    public void swithchToWindow(String p_windowName){
     CommonLib.sleep(DataStore.D_Wait_ShortTime);
     for (String s : driver.getWindowHandles()) {
    driver.switchTo().window(s);
    if (driver.getTitle().equals(p_windowName)) {
     {
      logger.info("切换到窗口:" + p_windowName + TextStore.T_Pass);
      break;
     }
     
    }
      }
    }
   
   
    public void newRunScript(String p_script) {
    
     CommonLib.sleep(DataStore.D_Wait_ShortTime);

   try {
    ((JavascriptExecutor) driver).executeScript(p_script);
    logger.info("执行jS代码:" + p_script + TextStore.T_Pass);

   } catch (Exception e) {
    logger.severe(TextStore.T_Exception
      + "newRunScript(String p_script)" + TextStore.T_DetailInfo
      + e.toString());

   }
    }
   
   public boolean newIsElementPresent(String p_id) {
       try {
         driver.findElement(parseObject(p_id));
         return true;
       } catch (NoSuchElementException e) {
         return false;
       }
     }
   
   public void screenshot()
    
   {

          File scrFile = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE);  
          String screenshot=DataStore.D_ScreenShotPath+File.separator+"screenshot_"+CommonLib.getCurrentTime()+".png";
         
          System.out.println(screenshot);
              
           try { 
 
               FileUtils.copyFile(scrFile, new File(screenshot));
               HtmlFileGlobal.setScreenshot(screenshot);
              
           } catch (IOException e) {
               // TODO Auto-generated catch block
         
               e.printStackTrace();
           }
        
     }
   
   
   

   
}

--------------------------------------------------------

原文地址:https://www.cnblogs.com/wjy123/p/8078425.html