1 package com.yanfuchang.selenium.utils;
   2 
   3 import java.awt.AWTException;
   4 import java.awt.Robot;
   5 import java.awt.event.KeyEvent;
   6 import java.io.File;
   7 import java.io.IOException;
   8 import java.net.MalformedURLException;
   9 import java.net.URL;
  10 import java.text.SimpleDateFormat;
  11 import java.util.Date;
  12 import java.util.HashMap;
  13 import java.util.List;
  14 import java.util.Map;
  15 import java.util.Set;
  16 import java.util.concurrent.TimeUnit;
  17 import org.apache.commons.io.FileUtils;
  18 import org.openqa.selenium.Alert;
  19 import org.openqa.selenium.By;
  20 import org.openqa.selenium.Cookie;
  21 import org.openqa.selenium.Dimension;
  22 import org.openqa.selenium. Executor;
  23 import org.openqa.selenium.Keys;
  24 import org.openqa.selenium.NoAlertPresentException;
  25 import org.openqa.selenium.NoSuchElementException;
  26 import org.openqa.selenium.OutputType;
  27 import org.openqa.selenium.TakesScreenshot;
  28 import org.openqa.selenium.WebDriver;
  29 import org.openqa.selenium.WebDriverException;
  30 import org.openqa.selenium.WebElement;
  31 import org.openqa.selenium.chrome.ChromeDriver;
  32 import org.openqa.selenium.firefox.FirefoxDriver;
  33 import org.openqa.selenium.firefox.FirefoxProfile;
  34 import org.openqa.selenium.ie.InternetExplorerDriver;
  35 import org.openqa.selenium.interactions.Actions;
  36 import org.openqa.selenium.interactions.HasInputDevices;
  37 import org.openqa.selenium.interactions.Keyboard;
  38 import org.openqa.selenium.remote.DesiredCapabilities;
  39 import org.openqa.selenium.remote.RemoteWebDriver;
  40 import org.openqa.selenium.support.ui.ExpectedConditions;
  41 import org.openqa.selenium.support.ui.Select;
  42 import org.openqa.selenium.support.ui.WebDriverWait;
  43 
  44 /**
  45  *    基于selenium的二次封装
  46  */
  47 public class WebDriverUtil {
  48     private static WebDriver driver = null;
  49     private static Select select = null;
  50     private static Alert alert = null;
  51     private static WebElement element = null;
  52     private static List<WebElement> elementList = null;
  53     private static long timeOutInSeconds = 10;
  54     //--------------------自定义常量------------------------
  55     public final String LINE = "rn";
  56     public final String smile = "^_^";
  57     public final String sad = "*o*";
  58 
  59     public WebDriverUtil(long timeOutInSeconds) {
  60         WebDriverUtil.timeOutInSeconds = timeOutInSeconds;
  61     }
  62 
  63     public WebDriverUtil() {}
  64     
  65     /**
  66      *     指定浏览器打开URL
  67      */
  68     public static void openBrowser(String url, String browser) {
  69         driver = initBrowser(browser);
  70         driver.manage().timeouts().implicitlyWait(timeOutInSeconds, TimeUnit.SECONDS);
  71         driver.get(url);
  72     }
  73     /**
  74      *     指定浏览器打开URL
  75      */
  76     public static void openBrowser(String url) {
  77         driver = initBrowser();
  78         driver.get(url);
  79     }
  80     
  81     /**
  82      *    初始化浏览器,方式1
  83      *        Firefox
  84      */
  85     public static WebDriver initBrowser() {
  86         /*
  87          * 谷歌浏览器 System.setProperty("webdriver.chrome.driver",
  88          * "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
  89          * WebDriver driver = new ChromeDriver(); driver.get("http://www.baidu.com/");
  90          */
  91         FirefoxProfile profile = new FirefoxProfile();
  92         profile.setPreference("browser.download.manager.showWhenStarting", false);// 是否显示下载进度框
  93         profile.setPreference("browser.offline-apps.notify", false);// 网站保存离线数据时不通知我
  94         profile.setPreference("browser.helperApps.alwaysAsk.force", false);// 应用程序设置不询问
  95         profile.setPreference("browser.download.folderList", 0);// 设置下载地址0是桌面;1是“我的下载”;2是自定义
  96         profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
  97                 "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip, application/msword");
  98         profile.setPreference("dom.webnotifications.enabled", false);// 允许通知
  99         WebDriver driver = new FirefoxDriver(profile);// 启动火狐浏览器
 100         driver.manage().window().maximize();// 设置窗口大小
 101         driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);// 设置页面加载超时
 102         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);// 设置查询组件等待时间
 103         return driver;
 104     }
 105     
 106     /**
 107      *     初始化浏览器,方式2
 108      *         (ie, ff, chrome)
 109      */
 110     private static WebDriver initBrowser(String browser) {
 111         switch (browser) {
 112         case "ie":
 113             System.setProperty("webdriver.ie.driver", ".\\Tools\\IEDriverServer.exe");
 114             driver = new InternetExplorerDriver();
 115             break;
 116         case "ff":
 117         case "firefox":
 118         case "Firefox":
 119         case "FireFox":
 120             /**
 121              *     FireFox安装方式为默认安装:
 122              *     FireFox版本小于48
 123              *         System.setProperty("webdriver.firefox.marionette", ".\\Tools\\geckodriver.exe");
 124              *     FireFox版本大于48,默认安装时可以试试,应该可以
 125              *         System.setProperty("webdriver.gecko.driver", ".\\Tools\\geckodriver.exe");
 126              */
 127             // FireFox安装方式为自定义安装
 128             System.setProperty("webdriver.firefox.bin", "D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
 129             driver = new FirefoxDriver();
 130             break;
 131         case "chrome":
 132         case "Chrome":
 133             System.setProperty("webdriver.chrome.driver", ".\\Tools\\chromedriver.exe");
 134             driver = new ChromeDriver();
 135             break;
 136         default:
 137             try {
 138                 throw new Exception("浏览器错误!");
 139             } catch (Exception e) {
 140                 e.printStackTrace();
 141             }
 142         }
 143         return driver;
 144     }
 145 
 146     //----------------------------------------------------元素相关-----------------------------------------------------------------------------
 147     /**
 148      *     查找元素
 149      * @param by      传入一个类型        例如:name
 150      * @param byValue 传入一个类型值       例如:username
 151      */
 152     public WebElement findElement(String by, String byValue) {
 153         try {
 154             switch (by) {
 155             case "id":
 156                 element = driver.findElement(By.id(byValue));
 157                 break;
 158             case "name":
 159                 element = driver.findElement(By.name(byValue));
 160                 break;
 161             case "class":
 162                 element = driver.findElement(By.className(byValue));
 163                 break;
 164             case "tag":
 165                 element = driver.findElement(By.tagName(byValue));
 166             case " ":
 167                 element = driver.findElement(By. Text(byValue));
 168                 break;
 169             case "partial text":
 170                 element = driver.findElement(By.partial Text(byValue));
 171             case "css":
 172                 element = driver.findElement(By.cssSelector(byValue));
 173                 break;
 174             case "xpath":
 175                 element = driver.findElement(By.xpath(byValue));
 176                 break;
 177             default:
 178                 throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
 179             }
 180         } catch (Exception e) {
 181             System.out.println("没有找到元素:" + byValue);
 182         }
 183         return element;
 184     }
 185     /**
 186      *     查找一组元素
 187      * @param by      传入一个类型        例如:name
 188      * @param byValue 传入一个类型值       例如:username
 189      */
 190     public List<WebElement> findElements(String by, String byValue) {
 191         try {
 192             switch (by) {
 193             case "id":
 194                 elementList = driver.findElements(By.id(byValue));
 195                 break;
 196             case "name":
 197                 elementList = driver.findElements(By.name(byValue));
 198                 break;
 199             case "class":
 200                 elementList = driver.findElements(By.className(byValue));
 201                 break;
 202             case "tag":
 203                 elementList = driver.findElements(By.tagName(byValue));
 204             case " ":
 205                 elementList = driver.findElements(By. Text(byValue));
 206                 break;
 207             case "partial text":
 208                 elementList = driver.findElements(By.partial Text(byValue));
 209             case "css":
 210                 elementList = driver.findElements(By.cssSelector(byValue));
 211                 break;
 212             case "xpath":
 213                 elementList = driver.findElements(By.xpath(byValue));
 214                 break;
 215             default:
 216                 throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
 217             }
 218         } catch (Exception e) {
 219             System.out.println("没有找到元素:" + byValue);
 220         }
 221         return elementList;
 222     }
 223     
 224     /**
 225      *     获取单个元素
 226      */
 227     public WebElement findElementByXpath(String xpath) {
 228         return driver.findElement(By.xpath(xpath));
 229     }
 230     public WebElement findElementByTag(String tag) {
 231         return driver.findElement(By.tagName(tag));
 232     }
 233     public WebElement findElementById(String id) {
 234         return driver.findElement(By.id(id));
 235     }
 236     public WebElement findElementByClassName(String name) {
 237         return driver.findElement(By.className(name));
 238     }
 239     public WebElement findElementByText(String text) {
 240         return driver.findElement(By. Text(text));
 241     }
 242     public WebElement findElementByPartialText(String text) {
 243         return driver.findElement(By.partial Text(text));
 244     }
 245     public WebElement findElementByName(String name) {
 246         return driver.findElement(By.name(name));
 247     }
 248     
 249     /**
 250      *    获取多个元素
 251      */
 252     public List<WebElement> findElementsByClassName(String className) {
 253         return driver.findElements(By.className(className));
 254     }
 255     public List<WebElement> findElementsByText(String text) {
 256         return driver.findElements(By. Text(text));
 257     }
 258     public List<WebElement> findElementsByPartialText(String text) {
 259         return driver.findElements(By.partial Text(text));
 260     }
 261     public List<WebElement> findElementsById(String id) {
 262         return driver.findElements(By.id(id));
 263     }
 264     public List<WebElement> findElementsByTag(String tag) {
 265         return driver.findElements(By.tagName(tag));
 266     }
 267     
 268     /**
 269      *    获取一组元素中的指定元素
 270      */
 271     public WebElement FindByElements(By by, int index) {
 272         WebElement element = null;
 273         if (this.elementsExists(by)) {
 274             element = driver.findElements(by).get(index);
 275         }
 276         return element;
 277     }
 278     
 279     /**
 280      *     查找元素并点击
 281      * @param by      传入一个类型        例如:name
 282      * @param byValue 传入一个类型值       例如:username
 283      */
 284     public boolean findElementClick(String by, String byValue) {
 285         try {
 286             switch (by) {
 287             case "id":
 288                 driver.findElement(By.id(byValue)).click();
 289                 return true;
 290             case "name":
 291                 driver.findElement(By.name(byValue)).click();
 292                 return true;
 293             case "class":
 294                 driver.findElement(By.className(byValue)).click();
 295                 return true;
 296             case "tag":
 297                 driver.findElement(By.tagName(byValue)).click();
 298             case " ":
 299                 driver.findElement(By. Text(byValue)).click();
 300                 return true;
 301             case "partial text":
 302                 driver.findElement(By.partial Text(byValue)).click();
 303             case "css":
 304                 driver.findElement(By.cssSelector(byValue)).click();
 305                 return true;
 306             case "xpath":
 307                 driver.findElement(By.xpath(byValue)).click();
 308                 return true;
 309             default:
 310                 throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
 311             }
 312         } catch (Exception e) {
 313             System.out.println("*****没有找到元素,类型为::" + by + "属性值为:" + byValue + "  的元素或者该元素无法点击****");
 314             return false;
 315         }
 316     }
 317     
 318     /**
 319      *    定位元素并点击
 320      */
 321     public void findElementByIdAndClick(String id) {
 322         driver.findElement(By.id(id)).click();
 323     }
 324     public void findElementByNameAndClick(String name) {
 325         driver.findElement(By.name(name)).click();
 326     }
 327     public void findElementByTextAndClick(String text) {
 328         driver.findElement(By. Text(text)).click();
 329     }
 330     public void findElementByPartiaTextAndClick(String text) {
 331         driver.findElement(By.partial Text(text)).click();
 332     }
 333     public void findElementByXpathAndClick(String xpath) {
 334         driver.findElement(By.xpath(xpath)).click();
 335     }
 336     public void findElementByClassNameAndClick(String name) {
 337         driver.findElement(By.className(name)).click();
 338     }
 339     
 340     /**
 341      *     查找元素并清除文本内容
 342      * @param by      传入一个类型        例如:name
 343      * @param byValue 传入一个类型值       例如:username
 344      */
 345     public boolean findElementClear(String by, String byValue) {
 346         try {
 347             switch (by) {
 348             case "id":
 349                 driver.findElement(By.id(byValue)).clear();
 350                 return true;
 351             case "name":
 352                 driver.findElement(By.name(byValue)).clear();
 353                 return true;
 354             case "class":
 355                 driver.findElement(By.className(byValue)).clear();
 356                 return true;
 357             case "tag":
 358                 driver.findElement(By.tagName(byValue)).clear();
 359                 return true;
 360             case " ":
 361                 driver.findElement(By. Text(byValue)).clear();
 362                 return true;
 363             case "partial text":
 364                 driver.findElement(By.partial Text(byValue)).clear();
 365                 return true;
 366             case "css":
 367                 driver.findElement(By.cssSelector(byValue)).clear();
 368                 return true;
 369             case "xpath":
 370                 driver.findElement(By.xpath(byValue)).clear();
 371                 return true;
 372             default:
 373                 throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
 374             }
 375         } catch (Exception e) {
 376             System.out.println("*****没有找到元素,类型为::" + by + "属性值为:" + byValue + "  的元素或者该元素没有输入值****");
 377             return false;
 378         }
 379     }
 380     
 381     /**
 382      *     查找元素并输入值
 383      * @param by      传入一个类型        例如:name
 384      * @param byValue 传入一个类型值       例如:username
 385      * @param key     填写要输入的值        例如:zhangsan
 386      */
 387     public boolean findElementSendKeys(String by, String byValue, String key) {
 388         try {
 389             switch (by) {
 390             case "id":
 391                 driver.findElement(By.id(byValue)).sendKeys(key);
 392                 return true;
 393             case "name":
 394                 driver.findElement(By.name(byValue)).sendKeys(key);
 395                 return true;
 396             case "class":
 397                 driver.findElement(By.className(byValue)).sendKeys(key);
 398                 return true;
 399             case "tag":
 400                 driver.findElement(By.tagName(byValue)).sendKeys(key);
 401                 return true;
 402             case " ":
 403                 driver.findElement(By. Text(byValue)).sendKeys(key);
 404                 return true;
 405             case "partial text":
 406                 driver.findElement(By.partial Text(byValue)).sendKeys(key);
 407             case "css":
 408                 driver.findElement(By.cssSelector(byValue)).sendKeys(key);
 409                 return true;
 410             case "xpath":
 411                 driver.findElement(By.xpath(byValue)).sendKeys(key);
 412                 return true;
 413             default:
 414                 throw new RuntimeException("输入的定位类型未在程序中定义,类型为:" + byValue);
 415             }
 416         } catch (Exception e) {
 417             System.out.println("*****没有找到元素,类型为::" + by + "属性值为:" + byValue + "    的元素或者该元素无法输入****");
 418             return false;
 419         }
 420     }
 421     /**
 422      *     查找元素并输入值
 423      * @param by      传入一个类型        例如:name
 424      * @param byValue 传入一个类型值       例如:username
 425      * @param key     填写要输入的值        例如:zhangsan
 426      */
 427     public boolean findElementClearAndSendKeys(String by, String byValue, String key) {
 428         try {
 429             switch (by) {
 430             case "id":
 431                 findElementClear(by,byValue);
 432                 driver.findElement(By.id(byValue)).sendKeys(key);
 433                 return true;
 434             case "name":
 435                 findElementClear(by,byValue);
 436                 driver.findElement(By.name(byValue)).sendKeys(key);
 437                 return true;
 438             case "class":
 439                 findElementClear(by,byValue);
 440                 driver.findElement(					
收藏 打印
您的足迹: