<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,大家請(qǐng)?jiān)L問 http://qaseven.github.io/

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

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

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

       到selenium的官方網(wǎng)站上下載幾個(gè)包。一個(gè)是selenium-server-standalone.jar;還有一個(gè)是selenium- java.jar。如果你選擇使用firefox(你就使用firefox吧,你會(huì)慢慢發(fā)現(xiàn)它的好處。)再下載個(gè)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中存在你引的包,并且沒有奇奇怪怪的符號(hào),那么,您就可以開始第一個(gè)webdriver自動(dòng)化程序了。

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

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

    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)到登陸頁(yè)");
        }else{
         logger.error("打開登陸頁(yè)面失敗");
        }
       }else{
        logger.error("沒有找到登陸鏈接!!!");
       }
      } catch (Exception e) {
       e.printStackTrace();
       logger.error("發(fā)生未知錯(cuò)誤!");
      }
     }
     
     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();
     }
     
    }

      有時(shí)候打開firefox的時(shí)候會(huì)報(bào)錯(cuò),說沒有安裝firefox之類的錯(cuò)誤,這是因?yàn)槟愀淖兞薴irefox的默認(rèn)安裝路徑。這種情況下,你可以根據(jù)FirefoxBinary類實(shí)現(xiàn)。

      方法如下:

    public WebDriver openFirefox() throws Exception{
     File file = new File("你的firefox的安裝路徑+firefox.exe"); //這里注意對(duì)\進(jìn)行轉(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è)構(gòu)造方法,意味著可以用7中方法打開firefox瀏覽器(其實(shí)比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 閱讀(3485) 評(píng)論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動(dòng)化測(cè)試學(xué)習(xí)

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

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 国产成人亚洲午夜电影| 久久亚洲AV成人无码电影| 亚洲AV无码专区国产乱码不卡| 最近2022中文字幕免费视频| 久久精品国产亚洲| 日韩免费在线观看视频| 国产∨亚洲V天堂无码久久久| 美女被免费网站91色| 亚洲午夜久久久久久噜噜噜| 草久免费在线观看网站| 亚洲一区二区视频在线观看| 中文在线观看国语高清免费| 国产AⅤ无码专区亚洲AV| 你是我的城池营垒免费观看完整版| 亚洲无线码一区二区三区| 国产婷婷成人久久Av免费高清| 国产亚洲人成网站观看| 男女午夜24式免费视频| 亚洲码在线中文在线观看| 免费观看美女用震蛋喷水的视频 | 黄色大片免费网站| 一本色道久久88综合亚洲精品高清| 又大又硬又粗又黄的视频免费看| 91麻豆国产自产在线观看亚洲| 成人免费视频88| 污视频网站在线观看免费| 国产亚洲AV无码AV男人的天堂 | 99精品热线在线观看免费视频| 亚洲沟沟美女亚洲沟沟| 免费黄网在线观看| eeuss影院免费92242部| 亚洲黄色免费电影| 欧洲精品免费一区二区三区| 国产VA免费精品高清在线| 亚洲韩国—中文字幕| 精品国产一区二区三区免费看| 一级毛片免费毛片毛片| 久久精品国产亚洲AV电影| 免费无码又爽又刺激高潮的视频| jizz中国免费| 国产99在线|亚洲|