51Testing软件测试论坛
标题: 菜鸟学自动化测试(九)----WebDirver [打印本页]
作者: lsekfe 时间: 2015-12-15 10:20
标题: 菜鸟学自动化测试(九)----WebDirver
关于什么是WebDirver,上一节做了简单的描述,环境也在上一章中搭建完成。
下面我们拷贝了官网提供的一个实例。让其在我们的eclipse中运行。
Selenium WebDirver 代码如下:
package MySel20Proj;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Selenium2Example {
public static void main(String[] args) {
// 用Firefox driver创建一个新的的实例
//注意:其他的代码依赖于界面
//不执行
System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
WebDriver driver = new FirefoxDriver();// 这里我们可以使用firefox来运行测试用例
//WebDriver driver = new ChromeDriver(); //这是chrome浏览器的驱动
//WebDriver driver = new InternetExplorerDriver(); //这是IE浏览器的驱动
// WebDriver driver = new HtmlUnitDriver(); //这是一个无界面测试模式,不用打开浏览器,通过后台输入来判断测试用例是否通过
// 现在用这个来访问谷歌
driver.get("http://www.google.com");
// 也可以用下面的方式访问谷歌
// driver.navigate().to("http://www.google.com");
// 找到文本输入元件的名字
WebElement element = driver.findElement(By.name("q"));
// 在搜索框内输入“cheese!”
element.sendKeys("Cheese!");
// 现在递交表格. WebDriver会发现我们的形式元素
element.submit();
// 后台打印输出,检查网页的标题
System.out.println("Page title is: " + driver.getTitle());
// 谷歌的搜索是渲染过的动态JavaScript. 等待页面加载,暂停10秒
(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("cheese!");
}
});
// Should see: "cheese! - Google Search"
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”关键安的操作:
[url=][/url]
package com.test.hzh;
import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Test1 {
private WebDriver driver;
private String baseUrl;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
driver = new FirefoxDriver();
baseUrl = "http://www.google.com.hk/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test() throws 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
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
}
作者: 1094389572 时间: 2016-8-23 15:46
暂时看到有点吃力啊
作者: gdshine 时间: 2016-8-24 10:27
呜呜,还行,希望可以一起交流一下
作者: heep789 时间: 2017-4-1 13:49
对于菜鸟的我来说果然吃力啊~~
作者: 1348579687 时间: 2017-10-14 15:50
打开浏览器,接下来就不动了。
这个是什么原因?
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
icons":{},"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Firefox WebDriver","description":"WebDriver implementation for Firefox","creator":"Simon Stewart","homepageURL":null},"visible":true,"active":false,"userDisabled":false,"appDisabled":true,"descriptor":"C:\\Users\\admin\\AppData\\Local\\Temp\\anonymous2294112965566027085webdriver-profile\\extensions\\fxdriver@googlecode.com","installDate":1507967327998,"updateDate":1507967327998,"applyBackgroundUpdates":1,"bootstrap":false,"skinnable":false,"size":8445045,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":true,"hasBinaryComponents":true,"strictCompatibility":false,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"3.0","maxVersion":"66.*"}],"targetPlatforms":[{"os":"Darwin","abi":null},{"os":"SunOS","abi":null},{"os":"FreeBSD","abi":null},{"os":"OpenBSD","abi":null},{"os":"WINNT","abi":"x86-msvc"},{"os":"Linux","abi":null}],"multiprocessCompatible":false,"signedState":0,"seen":true,"dependencies":[],"hasEmbeddedWebExtension":false,"mpcOptedOut":false}
JavaScript error: resource://cpmanager-clv/CCLVData.jsm, line 46: NS_NOINTERFACE: Component returned failure code: 0x80004002 (NS_NOINTERFACE) [nsIFileURL.file]
1507967331478 addons.manager DEBUG Starting provider: <unnamed-provider>
1507967331478 addons.manager DEBUG Registering shutdown blocker for <unnamed-provider>
1507967331478 addons.manager DEBUG Provider finished startup: <unnamed-provider>
1507967331480 DeferredSave.extensions.json DEBUG Starting write
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 46: NS_NOINTERFACE: Component returned failure code: 0x80004002 (NS_NOINTERFACE) [nsIFileURL.file]
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
1507967331546 addons.webextension.wx-assistant@mozillaonline.com WARN Please specify whether you want browser_style or not in your browser_action options.
1507967331856 addons.repository DEBUG No addons.json found.
1507967331856 DeferredSave.addons.json DEBUG Save changes
1507967331859 DeferredSave.addons.json DEBUG Starting timer
1507967331878 addons.manager DEBUG Starting provider: PreviousExperimentProvider
1507967331879 addons.manager DEBUG Registering shutdown blocker for PreviousExperimentProvider
1507967331879 addons.manager DEBUG Provider finished startup: PreviousExperimentProvider
1507967331882 DeferredSave.extensions.json DEBUG Write succeeded
1507967331882 addons.xpi-utils DEBUG XPI Database saved, setting schema version preference to 19
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
1507967331912 DeferredSave.addons.json DEBUG Starting write
1507967331941 DeferredSave.addons.json DEBUG Write succeeded
Crash Annotation GraphicsCriticalError: |[C0][GFX1]: Potential driver version mismatch ignored due to missing DLLs 0.0.0.0 and 0.0.0.0 (t=1.29126) [GFX1]: Potential driver version mismatch ignored due to missing DLLs 0.0.0.0 and 0.0.0.0
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
JavaScript error: http://offlintab.firefoxchina.cn/static/preload.html, line 131: ReferenceError: mozIndexedDB is not defined
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
Crash Annotation GraphicsCriticalError: |[C0][GFX1]: Potential driver version mismatch ignored due to missing DLLs 0.0.0.0 and 0.0.0.0 (t=3.34085) [GFX1]: Potential driver version mismatch ignored due to missing DLLs 0.0.0.0 and 0.0.0.0
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
JavaScript error: resource://cmsafeflag/SkipSBData.jsm, line 85: TypeError: aFile is undefined
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |