[Selenium] 自動化網頁應用程式測試

Test Automation (自動化測試) 是CI (Continuous Integration, 持續整合) / CD (Continuous Deployment, 持續發佈) 的其中一環. 當程式修改後, 透過自動測試, 確保問題能盡快發現及修正.

Selenium 是一套Web Application Testing Framework, 對大好處是不用學習另一套Script Language 就可以寫到Test case / Test suit, 又可以透過不同的Web Driver 模擬不同的瀏覽器進行測試, 以確保JavaScript 在相同動作下於不同瀏覽器是否相容. 

建立測試伺服器

  1. 安裝JDK.
    於command prompt 中輸入以下指令, 檢查是否已經安裝JDK.

    java

    若出現以下畫面, 即是JDK 已經安裝. 若沒有的話, 則需要自行下載及安裝.
     

  2. 下載Selenium Server.
    進入https://www.seleniumhq.org/download/, 選取Download version, 以取得最新版本.
    在示範中會將檔案放到c:\Selenium\jar 中.
  3. 建立啟動Selenium Server 指令檔.
    於c:\Selenium 中建立指令檔, 命名為startup.bat, 並輸入以下指令.

    @echo off
    set SeleniumPath="C:\Selenium\jar\selenium-server-standalone-3.11.0.jar"
    java -jar %SeleniumPath%
  4. 執行指令檔.
    於command prompt 中輸入以下指令.

    C:\Selenium\startup.bat

    若見到以下畫面, 表示執行成功.

建立單元測試

Selenium 本身支援Java, C# 等程式語言. 而在此示範中, 會利用Java 叫用Chrome, 並讀取其title 及URL.

  1. 建立 Test Project.
    在 Eclipse中選取 File > New > Project,  並於Project Name 中輸入 TestProject.
  2. 下載Selenium Client.
    進入https://www.seleniumhq.org/download/, 選取Java 並按Download, 以取得最新版本.
    在示範中會將檔案解壓到c:\Selenium\webDriver 中.
  3. 下載Web Driver.
    進入https://www.seleniumhq.org/download/, 選取Google Chrome Driver, 並  並下載最新版本.

  4. 在Project 中引用Selenium Client.
    於Test Project 中按右鍵, 選取Property > Java Build Path > Add External JARs…, 選取所有c:\Selenium\webDriver 的檔案後, 然後按OK.
  5. 建立Builder class. 用作處理所有WebDriver 的建立工作.
    於Test Project /src 中右鍵, 選取New > Class, 於 Name 中輸入 WebDriverBuilder, 然後輸入以下代碼. 

    import java.nio.file.Files;
    import java.nio.file.InvalidPathException;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class WebDriverBuilder {
    	protected static final String WEBDRIVER_CHROME_DRIVER = "webdriver.chrome.driver";
    
    	private static final String WEBDRIVE_CHROME_DEFAULT_PATH = "C:\\Selenium\\webDrivers\\chromedriver_win32\\chromedriver.exe";
    
    	public static ChromeDriver BuildChromeDriver() {
    		return BuildChromeDriver(WEBDRIVE_CHROME_DEFAULT_PATH);
    	}
    
    	public static ChromeDriver BuildChromeDriver(String driverPath) {
    		Path _driverPath = Paths.get(driverPath);
    		if (!Files.exists(_driverPath))
    			throw new InvalidPathException(_driverPath.toString(), "Invalid file path found.");
    		System.setProperty(WEBDRIVER_CHROME_DRIVER, _driverPath.toString());
    		ChromeDriver driver = new ChromeDriver();
    		return driver;
    	}
    }
  6. 建立Test class, 以啟動瀏覽器進行測試.
    於Test Project /src 中右鍵, 選取 New > Class, 於 Name 中輸入 SampleTest, 然後輸入以下代碼. 

    import WebDriverBuilder;
    import org.openqa.selenium.WebDriver;
    
    public class SampleTest  {
    	private static final String TEST_URL="http://www.google.com";
    	
    	public static void main(String[] args) {
    		WebDriver driver = WebDriverBuilder.BuildChromeDriver();
    		System.out.println("Test URL: "+TEST_URL);
    		driver.get(TEST_URL);
    		
    		String title=driver.getTitle();
    		String currentUrl=driver.getCurrentUrl();
    		
    		System.out.println("Current page title: "+title);
    		System.out.println("Current URL: "+currentUrl);
    		driver.close();
    	}
    }
    
  7. 測試程式.
    於Test Project 中選取SampleTest.java, 按右鍵並選取Run As > Java Application , 於Console 見到以結果, 代表測試成功.

參考資料

  1. Java SE Download, Oracle,
    http://www.oracle.com/technetwork/java/javase/downloads/index.html
  2. Selenium Project, 
    https://www.seleniumhq.org/
  3. Chrome Driver, Google,
    https://sites.google.com/a/chromium.org/chromedriver/
  4. Selenium Tutorial, TutorialsPoint,
    https://www.tutorialspoint.com/selenium/index.htm
  5. Selenium Tutorial, ToolSQA,
    http://toolsqa.com/selenium-tutorial/
About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.