Selenium2常用方法封裝

Selenium2常用方法封裝

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

/**
 * @author taoxu
 **/
public class MyWebdriver {
    private WebDriver driver;
    private String value = "";
    private boolean flag = true;
    private WebDriverWait wait;
    private WebElement element;

    public MyWebdriver() {

    }

    public void setWebDriver(WebDriver driver) {
        this.driver = driver;
    }

    public WebDriver getWebDriver() {
        return this.driver;
    }
    /**
     * 查找元素
     *
     * @param by      傳入一個類型
     * @param byValue 傳入一個類型值
     * @return 返回一個WebElement對象
     */
    public WebElement findElement(String by, String byValue) {
        try {
            switch (by) {
                case "id":
                    element = driver.findElement(By.id(byValue));
                    break;
                case "name":
                    element = driver.findElement(By.name(byValue));
                    break;
                case "class":
                    element = driver.findElement(By.className(byValue));
                    break;
                case "tag":
                    element = driver.findElement(By.tagName(byValue));
                case "link":
                    element = driver.findElement(By.linkText(byValue));
                    break;
                case "partiallinktext":
                    element = driver.findElement(By.partialLinkText(byValue));
                case "css":
                    element = driver.findElement(By.cssSelector(byValue));
                    break;
                case "xpath":
                    element = driver.findElement(By.xpath(byValue));
                    break;
                default:
                    throw new RuntimeException("輸入的定位類型未在程序中定義,類型為:" + byValue);
            }
        }catch (Exception e){
            System.out.println("沒有找到元素:"+byValue);
        }
        return element;
    }

    /**
     * 查找元素并點擊
     *
     * @param by      傳入一個類型
     * @param byValue 傳入一個類型值
     */
    public void findElementClick(String by, String byValue) {
        try {
            findElement(by, byValue).click();
        } catch (Exception e) {
            System.out.println("沒有找到該元素或者該元素不能被點擊");
        }
    }

    /**
     * 查找元素并清除
     *
     * @param by      傳入一個類型
     * @param byValue 傳入一個類型值
     */
    public void findElementClear(String by, String byValue) {
        try {
            findElement(by, byValue).clear();
        } catch (Exception e) {
            System.out.println("沒有找到該元素或者該元素沒有輸入值");
        }
    }
    /**
     * 查找元素并輸入值
     *
     * @param by      傳入一個類型
     * @param byValue 傳入一個類型值
     * @param key 填寫要輸入的值
     */
    public void findElementSendKeys(String by, String byValue,String key) {
        try {
            findElement(by, byValue).sendKeys(key);
        } catch (Exception e) {
            System.out.println("沒有找到該元素或者該元素無法輸入");
        }
    }

    /**
     * 執(zhí)行js方法
     *
     * @param js
     */
    public boolean excuteJS(String js) {
        if (flag) {
            try {
                ((JavascriptExecutor) driver).executeScript(js);
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)id定位元素并輸入內容
     *
     * @param id
     * @param value
     */
    public boolean inputById(String id, String value) {
        if (flag) {
            try {
                driver.findElement(By.id(id)).sendKeys(value);
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素并輸入內容
     *
     * @param xpath
     * @param value
     */
    public boolean inputByXpath(String xpath, String value) {
        if (flag) {
            try {
                driver.findElement(By.xpath(xpath)).sendKeys(value);
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)css定位元素并輸入內容
     *
     * @param css
     * @param value
     */
    public boolean inputByCss(String css, String value) {
        if (flag) {
            try {
                driver.findElement(By.cssSelector(css)).sendKeys(value);
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    public boolean clickBindCard(String cardNo) {
        flag = false;
        String value = "";
        int i = 0;
        if (!driver.findElement(By.id("r_x")).isDisplayed()) {
            for (i = 0; i < 5; i++) {
                value = driver.findElement(By.id("t_b_" + i)).getText();
                if (value.contains(cardNo)) {
                    flag = true;
                    System.out.println("t_b_" + i);
                    driver.findElement(By.id("sel_img_" + i)).click();
                }
            }
        } else {
            while (driver.findElement(By.id("r_x")).isDisplayed()) {
                System.out.println("i>>" + i);
                if (i > 0 && i % 5 == 0) {
                    driver.findElement(By.id("r_x")).click();
                }
                value = driver.findElement(By.id("t_b_" + i)).getText();
                System.out.println(value + ">>" + value.contains(cardNo));
                if (value.contains(cardNo)) {
                    flag = true;
                    System.out.println("t_b_" + i);
                    driver.findElement(By.id("sel_img_" + i)).click();
                    break;
                }
                i++;
            }
        }
        return flag;
    }

    /**
     * 根據(jù)id定位元素并點擊
     *
     * @param id
     */
    public boolean clickById(String id) {
        if (flag) {
            try {
                if (id.startsWith("sel_img_") && !id.contains("sel_img_ct")) {
                    int i = Integer.parseInt(id.substring(8, id.length()));
                    int j = 4;
                    while (i > 3) {
                        driver.findElement(By.id("sel_img_" + j)).click();
                        i -= 4;
                        j += 4;
                    }
                }
                driver.findElement(By.id(id)).click();
                System.out.println("click element by id>>" + id);
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素并點擊
     *
     * @param xpath
     */
    public boolean clickByXpath(String xpath) {
        if (flag) {
            try {
                driver.findElement(By.xpath(xpath)).click();
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)css定位元素并點擊
     *
     * @param css
     */
    public boolean clickByCss(String css) {
        if (flag) {
            try {
                driver.findElement(By.cssSelector(css)).click();
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)id定位元素并連續(xù)點擊
     *
     * @param id
     */
    public boolean clickById(String id, int count) {
        if (flag) {
            try {
                if (id.startsWith("sel_img_") && !id.contains("sel_img_ct")) {
                    int i = Integer.parseInt(id.substring(8, id.length()));
                    int j = 4;
                    while (i > 3) {
                        driver.findElement(By.id("sel_img_" + j)).click();
                        i -= 4;
                        j += 4;
                    }
                }
                for (int i = 0; i < count; i++) {
                    driver.findElement(By.id(id)).click();
                }
                System.out.println("click element by id>>" + id);
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素并連續(xù)點擊
     *
     * @param xpath
     */
    public boolean clickByXpath(String xpath, int count) {
        if (flag) {
            try {
                for (int i = 0; i < count; i++) {
                    driver.findElement(By.xpath(xpath)).click();
                }
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)css定位元素并連續(xù)點擊
     *
     * @param css
     */
    public boolean clickByCss(String css, int count) {
        if (flag) {
            try {
                for (int i = 0; i < count; i++) {
                    driver.findElement(By.cssSelector(css)).click();
                }
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素獲取值
     *
     * @param xpath
     * @return
     */
    public String getValueByXpath(String xpath) {
        if (flag) {
            value = driver.findElement(By.xpath(xpath)).getText();
            System.out.println();
            return value;
        } else {
            System.out.println("flag is false, function is not excuted");
            return null;
        }
    }

    /**
     * 根據(jù)id定位元素獲取值
     *
     * @param id
     * @return
     */
    public String getValueById(String id) {
        if (flag) {
            value = driver.findElement(By.id(id)).getText();
            System.out.println(value);
            return value;
        } else {
            System.out.println("flag is false, function is not excuted");
            return null;
        }
    }

    /**
     * 根據(jù)css定位元素獲取值
     *
     * @param css
     * @return
     */
    public String getValueByCss(String css) {
        if (flag) {
            value = driver.findElement(By.cssSelector(css)).getText();
            System.out.println(value);
            return value;
        } else {
            System.out.println("flag is false, function is not excuted");
            return null;
        }
    }

    /**
     * 根據(jù)id定位元素并清空值
     *
     * @param id
     */
    public boolean clearInputValueById(String id) {
        if (flag) {
            try {
                driver.findElement(By.id(id)).clear();
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素并清空值
     *
     * @param xpath
     */
    public boolean clearInputValueByXpath(String xpath) {
        if (flag) {
            try {
                driver.findElement(By.xpath(xpath)).clear();
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)css定位元素并清空值
     *
     * @param css
     */
    public boolean clearInputValueByCss(String css) {
        if (flag) {
            try {
                driver.findElement(By.cssSelector(css)).clear();
                return true;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 獲取網(wǎng)頁的title值
     *
     * @return
     */
    public String getTitle() {
        if (flag) {
            return driver.getTitle();
        } else {
            System.out.println("flag is false, function is not excuted");
            return null;
        }
    }

    /**
     * 切換到frame框
     *
     * @param frameName
     */
    public boolean switchToFrame(String frameName) {
        if (flag) {
            try {
                driver.switchTo().frame(frameName);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println(e.getMessage());
                return false;
            }
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)id定位元素并獲取元素的顯示狀態(tài)
     *
     * @param id
     * @return boolean
     */
    public boolean getDisplayStatById(String id) {
        if (flag) {
            return driver.findElement(By.id(id)).isDisplayed();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素并獲取元素的顯示狀態(tài)
     *
     * @param xpath
     * @return
     */
    public boolean getDisplayStatByXpath(String xpath) {
        if (flag) {
            return driver.findElement(By.xpath(xpath)).isDisplayed();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)css定位元素并獲取元素的顯示狀態(tài)
     *
     * @param css
     * @return
     */
    public boolean getDisplayStatByCss(String css) {
        if (flag) {
            return driver.findElement(By.cssSelector(css)).isDisplayed();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)id定位元素并獲取元素的可寫狀態(tài)
     *
     * @param id
     * @return
     */
    public boolean getEnableStatById(String id) {
        if (flag) {
            return driver.findElement(By.id(id)).isEnabled();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素并獲取元素的可寫狀態(tài)
     *
     * @param xpath
     * @return
     */
    public boolean getEnableStatByXpath(String xpath) {
        if (flag) {
            return driver.findElement(By.xpath(xpath)).isEnabled();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)css定位元素并獲取元素的可寫狀態(tài)
     *
     * @param css
     * @return
     */
    public boolean getEnableStatByCss(String css) {
        if (flag) {
            return driver.findElement(By.cssSelector(css)).isEnabled();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }

    }

    /**
     * 根據(jù)id定位元素并獲取元素的選中狀態(tài)
     *
     * @param id
     * @return
     */
    public boolean getSelectStatById(String id) {
        if (flag) {
            return driver.findElement(By.id(id)).isSelected();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)xpath定位元素并獲取元素的選中狀態(tài)
     *
     * @param xpath
     * @return
     */
    public boolean getSelectStatByXpath(String xpath) {
        if (flag) {
            return driver.findElement(By.xpath(xpath)).isSelected();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 根據(jù)css定位元素并獲取元素的選中狀態(tài)
     *
     * @param css
     * @return
     */
    public boolean getSelectStatByCss(String css) {
        if (flag) {
            return driver.findElement(By.cssSelector(css)).isSelected();
        } else {
            System.out.println("flag is false, function is not excuted");
            return false;
        }
    }

    /**
     * 獲取當前焦點所在頁面元素的屬性值(name,value,id,src等等)
     *
     * @param attribute
     * @return
     */
    public String getFocusAttributeValue(String attribute) {
        try {
            Thread.sleep(333);
        } catch (Exception e) {
            e.printStackTrace();
        }
        value = driver.switchTo().activeElement().getAttribute(attribute);
        System.out.println("The focus Element's " + attribute
                + "attribute value is>>" + value);
        return value;
    }

    /**
     * 打開網(wǎng)頁鏈接
     *
     * @param pageUrl
     */
    public boolean openPage(String pageUrl) {
        try {
            driver.get(pageUrl);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 進入測試,打開瀏覽器,輸入網(wǎng)址,打開網(wǎng)頁
     *
     * @param remoteUrl 遠程服務器地址
     * @param pageUrl   測試頁面地址
     */
    public boolean startTest(String remoteUrl, String pageUrl) {
        try {
            try {
                driver = new RemoteWebDriver(new URL(remoteUrl),
                        DesiredCapabilities.firefox());
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            driver.get(pageUrl);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 進入測試,打開瀏覽器,輸入網(wǎng)址,打開網(wǎng)頁
     *
     * @param explore   調用的瀏覽器,需要啟動不同的server,如:firefox,需要運行selenium-server-standalone-
     *                  2.33.0.jar。IE,則需運行IEDriverServer.exe
     * @param remoteUrl 遠程服務器地址
     * @param pageUrl   測試頁面地址
     */
    public boolean startTest(String explore, String remoteUrl, String pageUrl) {
        try {
            try {
                if ("f".equals(explore)) {
                    System.out.println("firefox");
                    driver = new RemoteWebDriver(new URL(remoteUrl),
                            DesiredCapabilities.firefox());
                } else if ("ie".equals(explore)) {
                    System.out.println("internet explorer");
                    DesiredCapabilities cap = DesiredCapabilities
                            .internetExplorer();
                    driver = new RemoteWebDriver(new URL(remoteUrl), cap);
                } else {
                    System.out.println("firefox");
                    driver = new RemoteWebDriver(new URL(remoteUrl),
                            DesiredCapabilities.firefox());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            driver.get(pageUrl);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 設置定位頁面元素的超時時間
     *
     * @param second
     * @return
     */
    public boolean setTimeOut(int second) {
        try {
            driver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 結束測試,關閉瀏覽器
     */
    public boolean endTest() {
        try {
            driver.quit();
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 休息間隔,單位毫秒
     *
     * @param millis
     * @return
     */
    public boolean sleep(Long millis) {
        try {
            Thread.sleep(millis);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 根據(jù)id等待對應的頁面元素出現(xiàn)
     *
     * @param id
     * @return
     */
    public boolean waitForElementById(String id) {
        try {
            driver.findElement(By.id(id));
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 根據(jù)css等待對應的頁面元素出現(xiàn)
     *
     * @param css
     * @return
     */
    public boolean waitForElementByCss(String css) {
        try {
            driver.findElement(By.cssSelector(css));
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    /**
     * 根據(jù)xpath等待對應的頁面元素出現(xiàn)
     *
     * @param xpath
     * @return
     */
    public boolean waitForElementByXpath(String xpath) {
        try {
            driver.findElement(By.xpath(xpath));
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,939評論 25 709
  • 做開發(fā)也有一段時間了,看了好多大神的代碼,總體感覺他們寫的代碼簡潔,好看,然而在對比下我寫的代碼,混亂,無序,簡直...
    蔡林林閱讀 2,150評論 0 0
  • 1.常用控件方法的封裝: #import #import @interfaceMyUtil :NSObject /...
    成熱了閱讀 746評論 0 0
  • 之所以會有選擇,就是因為會有損失焦慮;選擇了這個,自然放棄了另一個!拋硬幣的方式做選擇,真的這么草率嗎?當硬幣出現(xiàn)...
    霏依PL閱讀 391評論 0 0
  • 此篇博客所有源碼均來自JDK 1.8 CyclicBarrier,一個同步輔助類,在API中是這么介紹的:它允許一...
    chenssy閱讀 1,097評論 0 9

友情鏈接更多精彩內容