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

建立一個簡單的測試項目

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("天氣預報");
		Thread.sleep(1000);
		
		WebElement search = driver.findElement(By.id("su1"));
		search.click();
		Thread.sleep(1000);
		
		Assert.assertTrue(driver.getTitle().contains("天氣預報"),"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("天氣預報");
//			}
//		});
	}
 
	@AfterClass
	public void tearDown() throws Exception {
		driver.quit();
	}
}

加入Thread.sleep是由於有時候執行的太快了以至於頁面還沒顯示完整,以至於報錯。應該有更好的方法,稍後再編輯這裏。

註釋掉的部分是另外一種判斷方法,關於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>
TestNG的學習看這裏http://blog.sina.com.cn/s/blog_6966650401012dz1.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章