Selenium+ Webdriver+JAVA 自動化測試 環境搭建( SELENIUM自動化測試入門基礎)

Selenium+ Webdriver+JAVA 自動化測試 環境搭建( SELENIUM自動化測試入門基礎)


 Qa不止是點點點,偶爾寫點代碼,讓測試更加的輕鬆,解放更多的時間去學習交流。

 

一.Java環境的搭建


JDK的安裝


1.訪問oracle的官網下載最新版本的jdk


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

 

103819httzann266d7u6su.png

 

進去後選擇電腦配置對應版本的JDK版本。

下載成功以後直接下一步,安裝默認的路徑。這裏注意:安裝的過程中會提示一個jre的安裝路徑,需要注意一下,一個是運行環境,一個是編譯的環境。


2.配置環境變量

打開電腦中的系統屬性中的高級系統配置中的環境變量。系統變量中新建一個變量名稱爲Java_Home,存放的路徑式jdk的安裝目錄的路徑:C:\Program Files\Java\jdk1.8.0_45

新建變量Path%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;

新建變量Classpath.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;

(注意Classpath中的.;和中間分隔的; ps:別整成中文的了,根據自己的目錄可以自行調整路徑)

驗證是否安裝成功,windows cmd:輸入java and javac 如果看見大串出現,恭喜你安裝成功了。

 

3.編譯工具eclipse


我的網盤工具版本比較低了:http://pan.baidu.com/s/1sjuUHPj jdk6eclipse,直接打開就行。


4.WebDriver的三個jar

 

http://pan.baidu.com/s/1qWJtpjm

 

103849oyzaaze5e7j8ajje.png

 

 

5.瀏覽器驅動,WebDriver支持多個瀏覽器,目前用的比較多的Chrome  Ie  FireFox

http://pan.baidu.com/s/1pJj3yZL Chrome和ie的驅動

Firefox安裝在默認的路徑就可以直接調用。

 

二.新建第一個程序


1.新建一個java的project,填寫project和finish就好。

103910way3weofyefmbyq0.png

 

2.導入java包,右擊你的工程,選擇buildpathadd external Archives 導入Selenium的三個jar包。

 

3.新建一個類 class,輸入名稱,點擊finish即可。

103928vpdre63nbnssd3es.png


4.上代碼和selenium say hello

firefox say hello

package com.cxy.controller;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class TestBaidu {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

WebDriver dr =  new FirefoxDriver();


dr.get("http://www.baidu.com");


dr.findElement(By.id("kw")).sendKeys("hello Selenium");


dr.findElement(By.id("su")).click();


try {

Thread.sleep(3000);

catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

      

dr.quit();

}

 

}

 

chrome say hello

 

package com.cxy.controller;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class TestBaidu {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

System.setProperty("webdriver.chrome.driver"C:\\browser\\chromedriver.exe");

 

WebDriver driver3 = new ChromeDriver();

dr.get("http://www.baidu.com");


dr.findElement(By.id("kw")).sendKeys("hello Selenium");


dr.findElement(By.id("su")).click();


try {

Thread.sleep(3000);

catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

dr.quit();

}

}

和 ie say hello

 

package com.cxy.controller;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class TestBaidu {

 

public static void main(String[] args) {

// TODO Auto-generated method stub

 System.setProperty("webdriver.ie.driver""D:\\browser\\IEDriverServer.exe");

//ie瀏覽器安全設置的問題 

DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINStrue);   

WebDriver driver2 =new InternetExplorerDriver(ieCapabilities);

dr.get("http://www.baidu.com");


dr.findElement(By.id("kw")).sendKeys("hello Selenium");


dr.findElement(By.id("su")).click();


try {

Thread.sleep(3000);

catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

dr.quit();

}

}

文章轉載自WEBDRIVER 中文社區

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