1. 程式人生 > >UI“三重天”之selenium--封裝(二)

UI“三重天”之selenium--封裝(二)

基礎示例程式碼:

/**
 * @author Richered
 **/
package com.sample;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestBaidu {
	public static WebDriver driver = null;
	
	public static void main(String[] args) throws InterruptedException{
		System.setProperty("webdriver.chrome.driver", "tools/chromedriver.exe");
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		driver.get("https://www.baidu.com");
		Thread.sleep(2000);
		driver.findElement(By.xpath("//*[@id=\"kw\"]")).sendKeys("桃李不言、下自成蹊 部落格園");
		Thread.sleep(5000);
//		driver.findElement(By.xpath("//*[@id=\"su\"]")).click();
		driver.close();
	}
}  

本例基本上和官方示例程式碼一般無二,首先是獲取瀏覽器driver,開啟瀏覽器,將瀏覽器最大化,進入baidu,等待2s,找到百度輸入框並傳值"桃李不言、下自成蹊 部落格園",等待5s,關閉瀏覽器。

程式碼都是被寫死的,例如這塊不想使用google瀏覽器,想使用firefox瀏覽器或者IE瀏覽器呢?不想將瀏覽器視窗最大化呢?想進入www.bing.com呢?不想使用xpath的定位方式呢?

問題丟擲來了,這樣寫程式碼的方式也著實僵硬,那麼如何解決呢?--封裝關鍵字、動作

封裝

  封裝,即隱藏物件的屬性和實現細節,僅對外公開介面,控制在程式中屬性的讀和修改的訪問級別;將抽象得到的資料和行為(或功能)相結合,形成一個有機的整體,也就是

將資料與操作資料的原始碼進行有機的結合,形成“類”,其中資料和函式都是類的成員。在電子方面,封裝是指把矽片上的電路管腳,用導線接引到外部接頭處,以便與其它器件連

接。(copy百度百科)

  其實講的通俗一點就是:封裝提高了程式碼的重用性,不用關心具體的實現,當然,封裝也是面向物件的三大特徵之一。

  看看封裝上方的程式碼是如何實現:

/**
 * @author Richered
 **/
package com.sample;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class KeyWord { public WebDriver driver; public KeyWord() { } //選擇瀏覽器 public void openBrowser(String browserType) { try { switch (browserType) { case "chrome": GoogleDriver chrome = new GoogleDriver("tools/chromedriver.exe"); driver = chrome.getdriver(); break; case "ff": FFDriver firefox = new FFDriver("F:\\Firefox\\firefox.exe", "tools/geckodriver.exe"); driver = firefox.getdriver(); break; default: GoogleDriver c = new GoogleDriver("tools/chromedriver.exe"); driver = c.getdriver(); break; } } catch (Exception e) { // TODO: handle exception } } //關閉瀏覽器 public void closeBrowser() { try { driver.quit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println("關閉瀏覽器出錯!"); } } //瀏覽器大小顯示 public void setWindow(){ driver.manage().window().maximize(); } //訪問網站 public void getURL(String url) { try { driver.get(url); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //封裝name定位元素並傳值 public void inputByNameAndSubmit(String name,String content) { try { driver.findElement(By.name(name)).sendKeys(content); driver.findElement(By.name(name)).submit(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //封裝id定位元素並傳值 public void inputById(String id, String content) { try { driver.findElement(By.id(id)).sendKeys(content); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //用xpath定位元素並傳值 public void inputByxpath(String xpath, String content) { try { driver.findElement(By.xpath(xpath)).sendKeys(content); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /***********************************************************/ //用id定位元素並點選 public void clickByid(String id) { try { driver.findElement(By.id(id)).click(); } catch (Exception e) { // TODO: handle exception } } //用name定位元素並點選 public void clickByname(String name) { try { driver.findElement(By.name(name)).click(); } catch (Exception e) { // TODO: handle exception } } //用xpath定位元素並點選 public void clickByxpath(String xpath) { try { driver.findElement(By.xpath(xpath)).click(); } catch (Exception e) { // TODO: handle exception } } /***********************************************************/ //選擇點選方法 public void clickSwitch(String method, String phase) { try { switch (method) { case "id": driver.findElement(By.id(phase)).click(); break; case "classname": driver.findElement(By.className(phase)).click(); break; case "name": driver.findElement(By.name(phase)).click(); break; case "tagname": driver.findElement(By.tagName(phase)).click(); break; case "linktext": driver.findElement(By.linkText(phase)).click(); break; case "partialLinktext": driver.findElement(By.partialLinkText(phase)).click(); break; case "xpath": driver.findElement(By.xpath(phase)).click(); break; case "css": driver.findElement(By.cssSelector(phase)).click(); break; default: driver.findElement(By.xpath(phase)).click(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //封裝強制等待 public void halt(String time) { int t = 1000; try { t = Integer.parseInt(time); Thread.sleep(t); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

註釋裡邊寫的很清楚了,此處封裝了選擇瀏覽器,關閉瀏覽器,訪問URL,name、id、xpath的定位元素(傳值)方式【當然可以增加其他的定位方式,下同】,name、id、xpath的定位元素並點選,選擇點選方法,等待。

同樣是上方示例程式碼的目的,再看實現方式:

/**
 * @author Richered
 **/
package com.sample;

public class TestBaidu {
    public static void main(String[] args) {

        KeyWord sm = new KeyWord();
        sm.openBrowser("chrome");
        sm.setWindow();
        sm.getURL("https://www.baidu.com");
        sm.halt("2000");
        sm.inputByxpath("//*[@id=\"kw\"]", "桃李不言、下自成蹊 部落格園");
        sm.halt("5000");
        sm.closeBrowser();
    }
}

嗯哼,是不是程式碼量簡潔了許多呢?選擇哪個瀏覽器可以靈活選擇;定位元素的方式被單獨伶了出來,傳值即可;動作也被伶了出來,當然等待方式等等關鍵字、動作都可以進行封裝。

後續~~~~~