(五)PC自動化測試框架之自定義框架介紹(二)--function篇(二)(java)

上一篇講了另外三個文件裏的方法(與驅動無關),下面來詳細講解一下,我們最最主要的幾個方法(依賴驅動)。

UserFunction

首先聲明幾個變量,下面會用到。

public class UserFunction {
	public static WebDriver dr;
	public static String Text;
	public static String title;
	public static String objtext;
	static String url;
  • driver
    將driver與公共方法放一起,可以共用一個driver,這個方法很重要噢,瞭解selenium的應該都知道,自動化每一步的操作都是在ChromeDriver的基礎上來觸發的,給它單獨拿出來作爲一個方法的話,就不用每個用例單獨new一個驅動了。
	public static void driver(String url) {
		// TODO Auto-generated method stub
		System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\chromedriver.exe");
		ChromeDriver driver = new ChromeDriver();
		dr = driver;
		Navigation na = dr.navigate();
		dr.manage().window().maximize();
		dr.get(url);
	}
  • activate
    點擊操作,這裏做了一個異常處理,如果這個元素位置沒有找到,則先截圖再拋異常,這樣便於直觀的排查問題。
	/*
	 *  識別元素,並點擊 <input type="submit" value="百度一下" id="su"
	 * class="btn self-btn bg s_btn"> 
	 * activate(by.id("su"))
	 */
	public static void activate(By locator) throws Exception {
		try {
			WebElement element = (new WebDriverWait(dr,20)).until(ExpectedConditions.presenceOfElementLocated(locator));
			dr.findElement(locator).click();
		} catch (Exception e) {
			UserFunction.screenShotSpecial();
			dr.close();
			throw new Exception("對不起,該元素未抓取到,請您查看當前頁面元素位置是否有變動!");
		}
	}

擴展一下。如果一個頁面存在多個相同屬性值,無法使用唯一值來定位,那麼就使用findelements這個方法,比如下面的這個方法:

  • activateByIndex
	/*
	 * 當頁面存在多個相同元素時,使用該方法。index是從0開始計算的。比如說頁面存在兩個相同的submit元素需要點擊第二個, 就使用
	 * activateByIndex這個方法,用法activateByIndex(By.id("submitOrder"), 1)
	 * 
	 */

	public static void activateByIndex(By locator, int index) throws Exception {
		try {
			WebElement element = (new WebDriverWait(dr, 20))
					.until(ExpectedConditions.presenceOfElementLocated(locator));
			List<WebElement> list = null;
			list = dr.findElements(locator);
			list.get(index).click();
		} catch (Exception e) {
			UserFunction.screenShotSpecial();
			dr.close();
			throw new Exception("對不起,該元素未抓取到,請您查看當前頁面元素位置是否有變動!");
		}
	}
  • enterTextIn
    也是加了一個異常處理
	/*
	 * 識別元素,並傳值 <input type="text" class="s_ipt" name="wd" id="kw"
	 * maxlength="100" autocomplete="off"> 
	 * enterTextIn(by.id("kw"),"selenium2")
	 */
	public static void enterTextIn(By locator, String value) throws Exception {
		try {
			WebElement element = (new WebDriverWait(dr,20)).until(ExpectedConditions.presenceOfElementLocated(locator));
			dr.findElement(locator).sendKeys(value);
		} catch (Exception e) {
			UserFunction.screenShotSpecial();
			dr.close();
			throw new Exception("對不起,該元素未抓取到,請您查看當前頁面元素位置是否有變動!");
		}
	}

擴展,相同屬性值,不同位置的元素操作,可以先獲取這個元素,然後再傳入位置來進行輸入操作,同activateByIndex

  • enterTextInByIndex
	/*
	 * 當頁面存在相同屬性值的元素值,需要輸入文本的時候,可以採取這個方法。index是從0開始計算的,比如,像頁面第5個標籤信息爲input的元素輸入selenium2.
	 * enterTextInByIndex(By.tagName("input"), 4,"selenium2")
	 */
	public static void enterTextInByIndex(By locator, int index,String value) throws Exception {
		try {
			WebElement element = (new WebDriverWait(dr, 20))
					.until(ExpectedConditions.presenceOfElementLocated(locator));
			List<WebElement> list=dr.findElements(locator);
			list.get(index).sendKeys(value);
		} catch (Exception e) {
			UserFunction.screenShotSpecial();
			dr.close();
			throw new Exception("對不起,該元素未抓取到,請您查看當前頁面元素位置是否有變動!");
		}
	}
  • switchWindow
    這個方法很重要,大家都知道,打開一個網頁(窗口A),點擊一個連接,頁面可能會重新打開一個網頁(窗口B),這個時候selenium的默認窗口還是第一個窗口A,但是你需要切換到第二個窗口B。那麼直接調用這個方法,然後傳入你想定位第幾個窗口即可。比如,默認的窗口是A,現在切換到第二個窗口B,則switch_window(1),回到A窗口,則switch_window(0).這樣是不是很簡單了呢?
	/*
	 * 切換窗口
	 * switchWindow(1)
	 */
	public static void switchWindow(int i) {
		Set<String> winHandels = dr.getWindowHandles();// 得到當前窗口的set集合
		List<String> it = new ArrayList<String>(winHandels); // 將set集合存入list對象
		dr.switchTo().window(it.get(i));// 切換到彈出的新窗口
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// String url = dr.getCurrentUrl();// 獲取新窗口的url
		// System.out.println(url);
		// dr.switchTo().window(it.get(0));// 返回至原頁面

	}
  • isElementExist
    判斷當前頁面是否存在某個元素,有則返回true,沒有這個元素,則截圖拋異常處理。
	/*
	 * 判斷元素是否存在
	 */
	public static boolean isElementExist(By locator) throws Exception {
		try {
			WebElement element = (new WebDriverWait(dr,20)).until(ExpectedConditions.presenceOfElementLocated(locator));
			return true;
		} catch (Exception e) {
			return false;
		}
	}
  • clear
    清除文本框內容
	/*
	 * 清除輸入框
	 */
	public static void clear(By locator) throws Exception {
		try {
			WebElement element = (new WebDriverWait(dr,20)).until(ExpectedConditions.presenceOfElementLocated(locator));
			dr.findElement(locator).clear();
		} catch (Exception e) {
			UserFunction.screenShotSpecial();
			dr.quit();
			throw new Exception("對不起,該元素未抓取到,請您查看當前頁面元素位置是否有變動!");
		}
	}
  • close
    close和quit要注意區別,close只是將當前打開的窗口關閉。比如打開網頁A,後來點擊鏈接新開了一個窗口B,目標窗口已切到B,這個時候需要關閉B窗口,則用close(),如果需要關閉所有的窗口,則用quit()
	/*
	 * 關閉當前窗口
	 */
	public static void close() {
		dr.close();
	}
  • quit
    close和quit要注意區別,quit是將瀏覽器關閉,針對的是瀏覽器,而close只是針對當前多個窗口中的一個窗口。
	/*
	 * 關閉瀏覽器
	 */
	public static void quit() {
		dr.quit();
	}
  • screenShot
    截圖,直接調用這個方法即可截圖,截圖保存的位置結合NewDirectory方法,在項目的testresult下(方法裏用的都是相對路徑),以當前日期命名的文件夾內,圖片名稱是根據BasicFunction.DateString()這個方法命名的。
    在這裏插入圖片描述
	/*
	 * 截圖
	 * 
	 */
	public static void screenShot() {
		String filename = BasicFunction.DateString();
		filename = filename + ".jpg";
		String filepath = BasicFunction.NewDirectory();
		File scrFile = ((TakesScreenshot) dr).getScreenshotAs(OutputType.FILE);
		try {
			FileUtils.copyFile(scrFile, new File(filepath + "\\" + filename));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Can't save screenshot");
			e.printStackTrace();
		} finally {
		}
	}
  • screenShotSpecial異常截圖處理
	public static void screenShotSpecial() throws Exception {
		String picname = BasicFunction.dateString();
		picname = picname + ".jpg";
		String filename = System.getProperty("user.dir");
		String file = filename + "\\src\\test_result\\special";
		File scrFile = ((TakesScreenshot) dr).getScreenshotAs(OutputType.FILE);
		try {
			FileUtils.copyFile(scrFile, new File(file + "\\" + filename));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("Can't save screenshot");
			e.printStackTrace();
		} finally {
		}
	}

有疑問的加V瞭解詳情:zx1187463903

到這裏,依賴ChromeDriver的幾個常用方法已經封裝完了,理解了嗎?
如果你也搞定了,開心的同時請小編喝個咖啡也極好的呀。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章