<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經(jīng)轉(zhuǎn)移至github,大家請訪問 http://qaseven.github.io/

    WebDriver 測試開發(fā)(一)

    用過好多自動化測試工具,對于一顆擁有程序員心的測試工程師來說,選擇webdriver絕對能滿足你的要求。使用Webdriver不要求你把一門語言研究的多精通,你只要知道語法,和常用的包,常用的類,常用的方法就足夠。

      說明一下,我使用的的是java。所以,在開始前,你的電腦上正確安裝了jdk,然后有個使用習(xí)慣的開發(fā)工具,如eclipse。最好再裝個maven,我的項目都是maven工程。下面我們開始:

       到selenium的官方網(wǎng)站上下載幾個包。一個是selenium-server-standalone.jar;還有一個是selenium- java.jar。如果你選擇使用firefox(你就使用firefox吧,你會慢慢發(fā)現(xiàn)它的好處。)再下載個selenium-firefox- driver.jar

      把它引用到你創(chuàng)建的maven工程中:下面是我pom.xml部分內(nèi)容。

     

    <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>2.26.0</version>
     </dependency>
     <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-server-standalone</artifactId>
         <version>2.26.0</version>
     </dependency>
     <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-firefox-driver</artifactId>
         <version>2.25.0</version>
     </dependency>

     

      如果在 maven dependencies中存在你引的包,并且沒有奇奇怪怪的符號,那么,您就可以開始第一個webdriver自動化程序了。

      我們就當(dāng)你已經(jīng)成功創(chuàng)建了需要的project并且默認你有一些selenium的相關(guān)知識。我們就用webdriver干些事吧,哈哈。

      創(chuàng)建一個Login類。把下面代碼拷到文件中,然后運行一下。就能看到打開www.lovo.cn,跳轉(zhuǎn)到登陸頁面,然后登陸成功。

    package com.test.login;


    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;


    public class Login {
     
     private WebDriver webDriver;
     private String baseUrl;
     private Logger logger = LoggerFactory.getLogger(this.getClass());
     private WebElement element;
     
     public void openBrowser() throws Exception{
      webDriver = new FirefoxDriver();
      webDriver.get(baseUrl);
     }
     
     public void clickLoginLink(){
      try {
       baseUrl = "   this.openBrowser();
       element = webDriver.findElement(By.linkText("登錄"));
       if(element != null){
        element.click();
        if(webDriver.findElement(By.id("logusername")) != null){
         logger.info("正常跳轉(zhuǎn)到登陸頁");
        }else{
         logger.error("打開登陸頁面失敗");
        }
       }else{
        logger.error("沒有找到登陸鏈接!!!");
       }
      } catch (Exception e) {
       e.printStackTrace();
       logger.error("發(fā)生未知錯誤!");
      }
     }
     
     public void login(){
      this.webDriver.findElement(By.id("logusername")).clear();
      this.webDriver.findElement(By.id("logusername")).sendKeys("138****035");
      this.webDriver.findElement(By.id("logpassword")).clear();
      this.webDriver.findElement(By.id("logpassword")).sendKeys("123456");
      this.webDriver.findElement(By.id("logimageCheck")).clear();
      this.webDriver.findElement(By.id("logimageCheck")).sendKeys("5rkz");
      this.webDriver.findElement(By.cssSelector("span.btntext")).click();
      this.webDriver.findElement(By.cssSelector("div.text")).click();
      if(this.webDriver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]* 劉建東[\\s\\S]*$")){
       this.logger.info("登陸成功!");
      }else{
       this.logger.error("登陸失敗!");
      }
     }
     
     
     public static void main(String[] args){
      Login login = new Login();
      login.clickLoginLink();
      login.login();
     }
     
    }

      有時候打開firefox的時候會報錯,說沒有安裝firefox之類的錯誤,這是因為你改變了firefox的默認安裝路徑。這種情況下,你可以根據(jù)FirefoxBinary類實現(xiàn)。

      方法如下:

    public WebDriver openFirefox() throws Exception{
     File file = new File("你的firefox的安裝路徑+firefox.exe"); //這里注意對\進行轉(zhuǎn)義
     FirefoxBinary firefoxBin = new FirefoxBinary(file);
     WebDriver webDriver = new FirefoxDriver(firefoxBin,null);
     return webDriver;
    }

     

      或者使用setCapabilit來設(shè)置

      方法如下:

    public WebDriver openFirefox() throws Exception{
     DesiredCapabilities des = DesiredCapabilities.firefox();
     des.setCapability("firefox_binary", "你的firefox的安裝路徑+firefox.exe");
     WebDirver webDriver = new FirefoxDriver(des);
     return webDriver;
    }

     

      總結(jié);

      FirefoxDriver類有7個構(gòu)造方法,意味著可以用7中方法打開firefox瀏覽器(其實比7種多),

    FirefoxDriver() 
    FirefoxDriver(Capabilities desiredCapabilities) 
    FirefoxDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities) 
    FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile) 
    FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities capabilities) 
    FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities desiredCapabilities, Capabilities requiredCapabilities) 
    FirefoxDriver(FirefoxProfile profile) 

    posted on 2013-01-21 10:22 順其自然EVO 閱讀(3481) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學(xué)習(xí)

    <2013年1月>
    303112345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产精品成人AV在线| 久久精品国产亚洲一区二区| 亚洲毛片在线免费观看| 久久国产精品免费观看| 亚洲中文字幕无码一区| 国产va免费精品| 中文字幕精品无码亚洲字| 成人免费无码H在线观看不卡| 久久精品亚洲福利| 99精品全国免费观看视频..| 亚洲一区爱区精品无码| 无码一区二区三区免费| 亚洲日本在线播放| 色吊丝永久在线观看最新免费| 亚洲精品国产综合久久久久紧| 四虎永久免费影院| a级黄色毛片免费播放视频| 亚洲精品天天影视综合网| 中文字幕无码成人免费视频| 亚洲AV成人片无码网站| 中文字幕无码精品亚洲资源网| 中文无码成人免费视频在线观看| 久久丫精品国产亚洲av| 久久综合AV免费观看| 羞羞漫画小舞被黄漫免费| 亚洲无人区午夜福利码高清完整版| 免费精品99久久国产综合精品| 亚洲系列国产精品制服丝袜第| 成年人在线免费看视频| 一级做a爱过程免费视频高清| 亚洲日本一区二区| 成在线人永久免费视频播放| 女人隐私秘视频黄www免费| 亚洲不卡中文字幕| 国产亚洲精品AA片在线观看不加载| 亚洲成人免费在线| 国产成人亚洲精品91专区高清 | 亚洲综合精品网站| 日本免费大黄在线观看| 亚洲精品美女久久久久久久| 亚洲国产精品无码久久久秋霞2|