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

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

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

    qileilove

    blog已經轉移至github,大家請訪問 http://qaseven.github.io/

    WebDriver 測試開發(一)

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

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

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

      把它引用到你創建的maven工程中:下面是我pom.xml部分內容。

     

    <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自動化程序了。

      我們就當你已經成功創建了需要的project并且默認你有一些selenium的相關知識。我們就用webdriver干些事吧,哈哈。

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

    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("正常跳轉到登陸頁");
        }else{
         logger.error("打開登陸頁面失敗");
        }
       }else{
        logger.error("沒有找到登陸鏈接!!!");
       }
      } catch (Exception e) {
       e.printStackTrace();
       logger.error("發生未知錯誤!");
      }
     }
     
     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的默認安裝路徑。這種情況下,你可以根據FirefoxBinary類實現。

      方法如下:

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

     

      或者使用setCapabilit來設置

      方法如下:

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

     

      總結;

      FirefoxDriver類有7個構造方法,意味著可以用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 自動化測試學習

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

    導航

    統計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久亚洲精品成人无码网站| 色窝窝免费一区二区三区 | 日本红怡院亚洲红怡院最新| 污污网站免费观看| 337P日本欧洲亚洲大胆艺术图| 亚洲AV无码乱码在线观看裸奔| 成年女人免费视频播放体验区| 久久青青草原国产精品免费| 亚洲午夜无码久久久久小说| 亚洲成AV人片一区二区| 国产高清免费在线| 无码国产精品一区二区免费式直播 | 亚洲国产精品无码久久久秋霞1| 亚洲春色在线视频| 免费在线一级毛片| 动漫黄网站免费永久在线观看| caoporn成人免费公开| 亚洲精品理论电影在线观看| 亚洲午夜视频在线观看| 久久亚洲精品无码播放| 成年轻人网站色免费看| 99精品视频在线观看免费播放| 免费看又黄又爽又猛的视频软件| 久久亚洲精品国产亚洲老地址| 亚洲国产精品一区第二页 | 国产三级在线观看免费| 久爱免费观看在线网站| igao激情在线视频免费| 亚洲AV无码成人网站在线观看| 亚洲一区二区三区高清视频| 久久亚洲国产精品五月天| 日韩一卡2卡3卡4卡新区亚洲| 免费久久精品国产片香蕉| 在线观看免费成人| 日本视频一区在线观看免费| 久久久久久久岛国免费播放 | 免费中文字幕一级毛片| 毛片免费全部播放无码| 久久国产免费福利永久| 免费精品国偷自产在线在线| 一二三四免费观看在线电影|