java_selenium 開發環境搭建


java selenium 開發環境搭建

很多同學問我java selenium的開發環境怎么搭建,在這里簡要說明一下。

安裝jdk

這個自己一定要會

下載IDE

對於初學者來說java IDE無疑是消除初學者恐懼症的絕佳工具。很誠實的說intellij比eclipse要好用,不過對於初學者來說eclipse已經夠用了。所以首先下載安裝eclipse就好。這里是下載地址,解壓以后就可以用了。注意,如果你的系統是64位的,請下載64位的版本。

下載selenium的jar包

點擊這里下載,本文寫作的時候,最新版本是2.48.2。

新建項目並引入jar包

打開Eclpse,選擇菜單File-New-Java Project,然后出現如下圖所示:

Project Name我們輸入java webdriver startpoint,點擊Next,結果如下圖所示

點擊Add External JARs,選擇剛才下載的selenium jar包。

然后點擊Finish就好。

新建java類文件並測試安裝情況

接下來在src目錄下點擊右鍵,選擇New-Class

class name里寫StartPoint,注意大小寫,然后勾選上public static void main(String args[]),如圖所示

接下來鍵入下面的代碼:

  1. import java.io.File;
    
    
    import java.io.IOException;
    
    import org.openqa.selenium.OutputType;
    import org.openqa.selenium.firefox.*;
    import org.openqa.selenium.phantomjs.*;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.apache.commons.io.FileUtils;
    
    public class StartPoint {
    
    	public static void main(String[] args) throws IOException {
    		// 啟動firefox
    		File whereIsFF = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
    		FirefoxBinary ffBin= new FirefoxBinary(whereIsFF);
    		FirefoxProfile profile = new FirefoxProfile();
    		FirefoxDriver dr = new FirefoxDriver(ffBin, profile);
    		dr.get("http://www.qq.com");
    		takeScreenShot(dr, "./qq.png");
    		dr.quit();
    		
    		// 如果你安裝了phantomjs
    		// PhantomJSDriver phantom_dr = new PhantomJSDriver();
    		// phantom_dr.get("http://www.baidu.com");
    		// takeScreenShot(phantom_dr, "./baidu.png");
    		// phantom_dr.quit();
    		
    	}
    	
    	public static void takeScreenShot(RemoteWebDriver dr, String fileName) throws IOException {
    		File tmpFile = dr.getScreenshotAs(OutputType.FILE);
    		FileUtils.copyFile(tmpFile, new File(fileName));
    	}
    }
    

      


敲不動就直接復制粘貼吧。
注意,請將代碼中的File whereIsFF = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");這一行替換成你的firefox的安裝路徑!

接下來按ctrl+F11或者是點擊運行圖標來運行一下,如果你安裝了firefox(這里其實有點麻煩,最好安裝firefox國際版,並且版本不要太高,我的版本是windows 41.02版本)的話,那么你會看到firefox打開並訪問qq.com,然后截圖留念。

總結

總結一下,其實只要做下面幾件事情就可以搞定selenium的環境搭建了

  • 安裝jdk
  • 安裝eclipse
  • 安裝firefox
  • 下載selenium jar包
  • 按照上面的教程配置項目
  • 運行測試代碼
  • Done






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM