qileilove

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

          在Eclipse中使用Maven配置WebDriver+Testng(2)

          建立一個簡單的測試項目
          package net.Maventest;
          import org.openqa.selenium.*;
          import org.openqa.selenium.firefox.FirefoxDriver;
          import org.openqa.selenium.support.ui.ExpectedCondition;
          import org.openqa.selenium.support.ui.WebDriverWait;
          import org.testng.Assert;
          import org.testng.annotations.AfterClass;
          import org.testng.annotations.BeforeClass;
          import org.testng.annotations.Test;
          public class WebDriverDemoTest{
          private WebDriver driver;
          private String baseUrl;
          @BeforeClass
          public void setUp() throws Exception {
          System.setProperty("webdriver.firefox.bin", "D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
          driver = new FirefoxDriver();
          baseUrl = "http://www.baidu.com/";
          }
          @Test
          public void testSearch() throws Exception {
          driver.get(baseUrl);
          Thread.sleep(1000);
          WebElement input = driver.findElement(By.id("kw1"));
          input.sendKeys("天氣預(yù)報");
          Thread.sleep(1000);
          WebElement search = driver.findElement(By.id("su1"));
          search.click();
          Thread.sleep(1000);
          Assert.assertTrue(driver.getTitle().contains("天氣預(yù)報"),"Title is incorrect.");
          System.out.println(driver.getTitle());
          //      (new WebDriverWait(driver,10)).until(new ExpectedCondition<Boolean>(){
          //          public Boolean apply(WebDriver d){
          //                 return d.getTitle().startsWith("天氣預(yù)報");
          //          }
          //      });
          }
          @AfterClass
          public void tearDown() throws Exception {
          driver.quit();
          }
          }

            加入Thread.sleep是由于有時候執(zhí)行的太快了以至于頁面還沒顯示完整,以至于報錯。應(yīng)該有更好的方法,稍后再編輯這里。
            注釋掉的部分是另外一種判斷方法,關(guān)于WebDriver的等待方式,可以參考這篇文章http://blog.csdn.net/pf20050904/article/details/20052485
            testng.xml配置如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <suite name ="MavenTest" verbose="2">
          <test name ="baiduTest">
          <classes>
          <class name="net.Maventest.WebDriverDemoTest">
          <methods>
          <includ name="setUp"/>
          <includ name="testSearch"/>
          <includ name="tearDown"/>
          </methods>
          </class>
          </classes>
          </test>
          </suite>
          相關(guān)文章:
          在Eclipse中使用Maven配置WebDriver+Testng(1)

          posted on 2014-03-11 11:02 順其自然EVO 閱讀(557) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學(xué)習(xí)

          <2014年3月>
          2324252627281
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新宾| 咸阳市| 万盛区| 湘潭县| 广宗县| 孟连| 冷水江市| 蕲春县| 津南区| 霍邱县| 关岭| 崇礼县| 舞阳县| 兴业县| 屏东县| 盐城市| 贵溪市| 潢川县| 美姑县| 区。| 洮南市| 白城市| 开化县| 四子王旗| 拜泉县| 临澧县| 江阴市| 琼结县| 苍溪县| 阜城县| 两当县| 南华县| 肇州县| 北流市| 海阳市| 荔波县| 奎屯市| 保定市| 枣阳市| 波密县| 盐池县|