Selenium初步環(huán)境部署

[圖片上傳失敗...(image-364cb5-1525661643320)]

selenium主要組件:

  • selenium-IDE:支持錄像和回放的功能
  • selenium-RC
    下載地址:selenium-RC
  • selenium-Grid

此處推薦蟲(chóng)師的書(shū)籍:《selenium2自動(dòng)測(cè)試實(shí)戰(zhàn)–基于Python語(yǔ)言》

一、selenium-RC安裝

  1. 打開(kāi)eclipes,新建一個(gè)Java project(ps:Java配置和eclipes教程將在之后分享)


    步驟-1.png
  2. 右擊項(xiàng)目選擇build path-link source選擇剛剛下載的內(nèi)容:selenium-java-2.44.0jar包如下圖顯示:
    步驟-2.png
  3. 右擊項(xiàng)目選擇添加Junit----properties


    步驟-3.png
  4. 選中導(dǎo)入的selenium-java-2.44.0.jar和selenium-server-standalone-2.44.0.jarr添加到項(xiàng)目引用,選中這兩個(gè)包---->Bulid Path --->add to Bulid Path 即可


    步驟-4.png

二、selenium-IDE安裝

  • 安裝Firefox(看網(wǎng)上別人都推薦安裝低版本的Firefox,后面我因?yàn)槌霈F(xiàn)問(wèn)題又重新安裝了和selenium server相配的Firefox瀏覽器)
  • 打開(kāi)Firefox,選擇菜單欄中的【附加組件】---搜索selenium IDE
  • 選擇安裝稍后重啟后就會(huì)在右上角出現(xiàn)一個(gè)icon,點(diǎn)擊打開(kāi)-如圖,


    selenium-IDE.png

三、錄制腳本并導(dǎo)出Java腳本

  • 打開(kāi)selenium-IDE,點(diǎn)擊右邊的紅色錄制按鈕,瀏覽器打開(kāi)錄制的界面(此處以百度為例)


    步驟-1.png

-打開(kāi)瀏覽器在百度輸入框中輸入selenium,點(diǎn)擊搜索按鈕,導(dǎo)出腳本:

步驟-2.png
步驟-2.1.png
  • 導(dǎo)出的Java腳本在eclipes中打開(kāi)并成功運(yùn)行:貼出導(dǎo)出的代碼如下顯示:
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.regex.Pattern;

public class love {
    private Selenium selenium;

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox C:/Program Files (x86)/Mozilla Firefox/firefox33.0/firefox.exe", "https://www.baidu.com/");
               //此處注意Firefox的路徑
        selenium.start();
    }

    @Test
    public void testLove() throws Exception {
        selenium.open("/");
        selenium.click("id=kw");
        selenium.type("id=kw", "selenium");
        selenium.click("id=su");
        selenium.click("link=selenium + python自動(dòng)化測(cè)試環(huán)境搭建 - 蟲(chóng)師 - 博客園");
    }

    @After
    public void tearDown() throws Exception {
        
    }
}

四、部署環(huán)境中會(huì)出現(xiàn)各種問(wèn)題,博主列下以下常見(jiàn)的問(wèn)題以及解決辦法:

報(bào)錯(cuò)一:Failed to start new browser session:java.lang.RuntimeException: Firefox 3 could not be found in the path!Please add the directory containing ''firefox.exe'' to your PATH environment.
造成原因:本地的Firefox路徑不一致造成的
解決辦法:首先嘗試將firefox.exe的路徑放到系統(tǒng)環(huán)境變量path中,重新運(yùn)行,錯(cuò)誤依舊。
正確解決辦法:在調(diào)用firefox處加上絕對(duì)路徑(selenium("localhost", 4444, "*firefox D:\Program Files\Mozilla Firefox\firefox.exe", "http://www.baidu.com"))

報(bào)錯(cuò)二:java.lang.RuntimeException: Could not contact SeleniumServer; have you started it on 'localhost:4444' ?
造成原因:selenium server未啟動(dòng)
解決方案:?jiǎn)?dòng)selenium server:java -jar selenium-server-standalone-2.24.1.jar
自己可以新建一個(gè)bat文件,雙擊即可

報(bào)錯(cuò)三:Firefox 無(wú)法在 chrome://src/content/RemoteRunner.html?sessionId=e4a9ff6710024147858b77bf8c3b7e6c&multiWindow=true&baseUrl=https://www.google.com.hk/&debugMode=false&driverUrl=http://localhost:4444/selenium-server/driver/ 找到該文件。
造成原因:Firefox版本與selenium server版本不一致導(dǎo)致的。
解決辦法:使用一致的版本
【Selenium】 -> 【FireFox】
  2.25.0 -> 18
  2.30.0 -> 19
  2.31.0 -> 20
  2.42.2 -> 29
  2.44.0 -> 33 (不支持31,2014/12/1) 升級(jí)selenium jar包,或者是升級(jí)firefox。
此處為老版本的Firefox下載地址

注意:火狐版本經(jīng)常會(huì)自動(dòng)更新,記得將自動(dòng)更新的勾選去掉,火狐:選項(xiàng)-高級(jí)-更新:不檢查更新,省的下次打開(kāi)自動(dòng)更新了,就懵逼了O(∩_∩)O哈哈~

好啦,博主今天剛剛學(xué)習(xí)到這里了,雖然只是一個(gè)環(huán)境的搭建和一個(gè)小demo但是還是經(jīng)歷了重重磨難才成功的,大家也去一步步的搭建自己的平臺(tái)吧,一切來(lái)于實(shí)踐才理解的深刻,加油(ω)
[圖片上傳失敗...(image-258fe-1525661643320)]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容