菜鸟学自动化测试(九)—-WebDirver
关于什么是WebDirver,上一节做了简单的描述,环境也在上一章中搭建完成。
下面我们拷贝了官网提供的一个实例。让其在我们的eclipse中运行。
Selenium WebDirver 代码如下:
MySel20Proj; org.openqa.selenium.By; org.openqa.selenium.WebDriver; org.openqa.selenium.WebElement; org.openqa.selenium.firefox.FirefoxDriver; org.openqa.selenium.htmlunit.HtmlUnitDriver; org.openqa.selenium.support.ui.ExpectedCondition; org.openqa.selenium.support.ui.WebDriverWait; Selenium2Example { main(String[] args) { System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" ); WebDriver driver = FirefoxDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); ( WebDriverWait(driver, 10)).until( ExpectedCondition<Boolean>() { Boolean apply(WebDriver d) { d.getTitle().toLowerCase().startsWith("cheese!"); } }); System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } }
运行时报出了错误;
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP
Build info: version: '2.16.1', revision: '15405', time: '2012-01-05 12:30:12'
我们只要在WebDriver driver = new FirefoxDriver(); 前面指定我们浏览器的具体信息即可:
System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
WebDriver driver = new FirefoxDriver();
WebDirver 的实现:
驱动名称 | 对操作系统的支持 | 调用的接口 |
FireFox Driver | ALL | org.openqa.selenium.firefox.FirefoxDriver |
Chrome Driver | ALL | org.openqa.selenium.chrome.ChromeDriver |
InternetExplorer Driver | Windows | org.openqa.selenium.ie.InternetExplorerDriver |
HtmlUnit Driver | ALL | org.openqa.selenium.htmlunit.HtmlUnitDriver |
什么情况下选用WebDirver ?
(1)Selenium-1.0不支持的浏览器功能。
(2)multiple frames, multiple browser windows, popups, and alerts.
(3)页面导航。
(4)下拉。
(5)基于AJAX的UI元素。
同样,我们的selenium IDE也支持WebDriver类型脚本的导出。
将我们录制好的脚本 导出为junit(WebDriver) 类型
下面代码是我录制的一个google搜索“selenium”关键安的操作:
com.test.hzh; java.util.regex.Pattern; java.util.concurrent.TimeUnit; org.junit.*; org.junit.Assert.*; org.openqa.selenium.*; org.openqa.selenium.firefox.FirefoxDriver; org.openqa.selenium.support.ui.Select; Test1 { WebDriver driver; String baseUrl; StringBuffer verificationErrors = StringBuffer(); @Before setUp() Exception { System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" ); driver = FirefoxDriver(); baseUrl = "http://www.google.com.hk/"; driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } @Test test() Exception { driver.get(baseUrl + "/"); driver.findElement(By.id("lst-ib")).clear(); driver.findElement(By.id("lst-ib")).sendKeys("selenium"); driver.findElement(By.name("btnK")).click(); } @After tearDown() Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } isElementPresent(By by) { { driver.findElement(by); ; } (NoSuchElementException e) { ; } } }