本人在使用selenium做测试的时候,封装了很多方法,由于工作原因估计很长时间不会更新方法库了,中间关于js的部分还差一些没有完善,其中设计接口的部分暂时就先不发了,以后有机会在更新。

package soucrce;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
//import org.openqa.selenium.chrome.*;
//import org.openqa.selenium.ie.*;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;

public class SelLibrary extends SourceCode{
    private static SelLibrary library = new SelLibrary();
    public static SelLibrary getInstance() {
        return library;
    }
    public static WebDriver driver = getDrive();
//    public static WebDriver driverH5 = getDriveH5();
    public static WebDriver getDrive() {
        /* 谷歌浏览器
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.baidu.com/");
        */
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.manager.showWhenStarting", false);//是否显示下载进度框
        profile.setPreference("browser.offline-apps.notify", false);//网站保存离线数据时不通知我
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);//应用程序设置不询问
        profile.setPreference("browser.download.folderList", 0);//设置下载地址0是桌面;1是“我的下载”;2是自定义
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip, application/msword");
        profile.setPreference("dom.webnotifications.enabled", false);//允许通知         
        WebDriver driver = new FirefoxDriver(profile);//启动火狐浏览器
        driver.manage().window().maximize();//设置窗口大小
        driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);//设置页面加载超时
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置查询组件等待时间
        return driver;
    }
    /*
     *此为H5页面方法,暂不使用
    public static WebDriver getDriveH5() {
        // 谷歌浏览器
//        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
//        WebDriver driver = new ChromeDriver();
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.manager.showWhenStarting", false);//是否显示下载进度框
        profile.setPreference("browser.offline-apps.notify", false);//网站保存离线数据时不通知我
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);//应用程序设置不询问
        profile.setPreference("browser.download.folderList", 0);//设置下载地址0是桌面;1是“我的下载”;2是自定义
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip, application/msword");
        profile.setPreference("dom.webnotifications.enabled", false);//允许通知 
        WebDriver driver = new FirefoxDriver(profile);//启动火狐浏览器
        driver.manage().window().setSize(new Dimension(435, 773));//设置窗口大小
        driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);//设置页面加载超时
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);//设置查询组件等待时间
        return driver;
    }
    */
    public final String LINE = "\r\n";
    public final String testAppURL = "api.dev.chaojizhiyuan.com";
    public final String appURL = "api.gaotu100.com";
    public final String testWebURL = "beta-web.gaotu100.com";
    public final String smile = "^_^";
    public final String sad = "*o*";
    //截图命名为当前时间保存桌面
    public void takeScreenshotByNow() throws IOException {
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        String file = "C:\\Users\\fankaiqiang\\Desktop\\888\\picture\\321\\"+getNow()+".png";
        FileUtils.copyFile(srcFile,new File(file));
    }
    //截图重命名保存至桌面
    public void takeScreenshotByName(String name) throws IOException {
        File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        String file = "C:\\Users\\fankaiqiang\\Desktop\\888\\picture\\321\\"+name+".png";
        FileUtils.copyFile(srcFile,new File(file));
    }
    //通过id获取元素并点击
    public void findElementByIdAndClick(String id) {
        driver.findElement(By.id(id)).click();
    }
    public void findElementByNameAndClick(String name) {
        findElementByName(name).click();
    }
    //根据文本获取元素并点击
    public void findElementByTextAndClick(String text) {
        driver.findElement(By.linkText(text)).click();
    }
    //根据文本模糊查找
    public void findElementByPartiaTextAndClick(String text) {
        driver.findElement(By.partialLinkText(text)).click();
    }
    //根据xpath获取元素
    public WebElement findElementByXpath(String xpath) {
        return driver.findElement(By.xpath(xpath));
    }
    public WebElement findElementByTag(String tag) {
        return driver.findElement(By.tagName(tag));
    }
    //根据id获取元素
    public WebElement findElementById(String id) {
        return driver.findElement(By.id(id));
    }
    //根据id获取元素清除文本写入文本
    public void findElementByIdAndClearSendkeys(String id1 , String id2, String text) {
        driver.findElement(By.id(id1)).clear();
        driver.findElement(By.id(id2)).sendKeys(text);
    }
    public void findElementByIdAndClearSendkeys(String id, String text) {
        driver.findElement(By.id(id)).clear();
        driver.findElement(By.id(id)).sendKeys(text);
    }
    public void findElementByIdAndClearSendkeys(String id, int num) {
        driver.findElement(By.id(id)).clear();
        driver.findElement(By.id(id)).sendKeys(num+"");
    }
    public void findElementByNameAndClearSendkeys(String name, String text) {
        findElementByName(name).clear();
        findElementByName(name).sendKeys(text);
    }
    public void findElementByNameAndClearSendkeys(String name, int num) {
        findElementByName(name).clear();
        findElementByName(name).sendKeys(num+"");
    }
    //通过xpath获取元素点击
    public void findElementByXpathAndClick(String xpath) {
        driver.findElement(By.xpath(xpath)).click();
    }
    //通过class获取元素并点击
    public void findElementByClassNameAndClick(String name) {
        driver.findElement(By.className(name)).click();
    }
    public WebElement findElementByClassName(String name){
        return driver.findElement(By.className(name));
    }
    //获取一组元素
    public List<WebElement> findElementsByClassName(String className) {
        return driver.findElements(By.className(className));
    }
    //根据text获取一组页面元素
    public List<WebElement> findElementsByText(String text) {
        return driver.findElements(By.linkText(text));
    }
    public List<WebElement> findElementsByPartialText(String text) {
        return driver.findElements(By.partialLinkText(text));
    }
    //根据id获取一组页面元素
    public List<WebElement> findElementsById(String id) {
        return driver.findElements(By.id(id));
    }
    //根据tagName获取一组页面元素
    public List<WebElement> findElementsByTag(String tag) {
        return driver.findElements(By.tagName(tag));
    }
    public WebElement findElementByText(String text){
        return driver.findElement(By.linkText(text));
    }
    public WebElement findElementByPartialText(String text){
        return driver.findElement(By.partialLinkText(text));
    }
    public WebElement findElementByName(String name) {
        return driver.findElement(By.name(name));
    }
    //输出cookies信息
    public void outputCookie() {
        Set<Cookie> cookie = driver.manage().getCookies();
        System.out.println(cookie);
//        Cookie abc = new Cookie("", "");

相关文章:

  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-08-27
  • 2021-08-27
  • 2021-08-27
  • 2021-08-27
猜你喜欢
  • 2022-12-23
  • 2021-08-05
  • 2021-08-27
  • 2021-08-27
  • 2022-12-23
  • 2021-08-27
  • 2021-12-01
相关资源
相似解决方案