安裝並完成你的第一個 Selenium/Java 測試程式

安裝並完成你的第一個 Selenium/Java 測試程式

這篇文章主要介紹如何準備一個 selenium的開發環境,以 Java 為例子。

並且簡單的模擬一個用 fireFox 到google Search 的瀏覽行為。

Step 1 : 安裝 JDK

首先必須安裝,JDK,可到下列網址安裝

http://www.oracle.com/technetwork/java/javase/downloads/index.html

 

Step 2: 安裝 Java Editor

安裝 Java Editor 主要用在開發 Java 的程式。這部分可以依照個人的使用習慣而定。

這個例子我們選擇安裝Eclipse IDE。http://www.eclipse.org/downloads

下載完後,解壓縮執行 ” eclipse.exe”

Step 3: 下載 Java WebDriver

這個部分主要下載 Selenium WebDriver 對於程式語言的套件。

每個程式語言都會有相對應的套件。可參考如下。這個例子中,我們主要下載的是 Java 的 Webdriver 套件。

解壓縮之後會有許多相對應的 JAR 檔案

Download

http://docs.seleniumhq.org/download

Download-Webdriver-Java-Client-1

Step 4: 設定 Eclipse

將步驟3,所解壓縮的 Selenium JAR 檔案,全部設定到 Eclipse 中。包含根目錄與\libs\目錄下所有的 Jar 檔案

Right click on Project Name > Select Properties > Java build path

Selenium-Jar-1

Step 5: 撰寫第一個 Selenium /Java 程式

[pastacode lang=”java” message=”Google Search Selenium Sample” highlight=”” provider=”manual”]

 
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstTestCase {
 
    private static WebDriver driver = null;
 
    public static void main(String[] args) {
 
        // Launch FireFox browser
         driver = new FirefoxDriver();
 
        //Implicit wait - 10 sec 
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 
        //Visit www.google.com 
        driver.get("https://www.google.com");
 

        // Locate the search input box and input "Testing Search"
         driver.findElement(By.xpath(".//*[@id='lst-ib']")).sendKeys("Testing Search ");
 
 
 
        // Close the browser
         driver.quit();
 
            }
 
    }

[/pastacode]

 

 

That’s all. 透過 Selenium WebDriver 程式語言 (Java, Python, C#等),就可以模擬使用者輸入與瀏覽器溝通

Leave a Reply

Your email address will not be published. Required fields are marked *