selenium2010年最火的歌曲火吗

&&&&&&&&&&&&&&&&&&
posts - 15,comments - 7,trackbacks - 0
Selenium&严格说来,Selenium是一套完整的Web应用程序测试系统,它包含了测试的录制(Selenium IDE)、编写及运行(Selenium Remote Control)和测试的并行处理(Selenium Grid)。Selenium的核心Selenium Core基于JsUnit,完全由JavaScript编写,因此可运行于任何支持JavaScript的浏览器上。Selenium Core由一种指定格式的HTML文件驱动,在一定程度上增强了测试套件(Test Suite)的可读性。Selenium Remote Control允许测试人员使用常见的语言(自然包括C#等.NET语言)编写测试代码,并支持不同操作系统下的各种主流浏览器。Selenium Grid的作用是将测试分发至多台机器,这样便可大大加快测试速度。与WatiN相同,Selenium也是一款同样使用Apache License 2.0协议发布的开源框架。&&&Selenium IDE&&&&&&& Selenium IDE是基于FIREFOX浏览器的一个插件,提供GUI界面来运行Selenium测试。Selenium IDE提供脚本录制功能,可以将用户在浏览器中执行的操作记录下来,生成各种形式的脚本,可以将这些脚本保存供以后使用。&&&&&&& 安装Selenium IDE的步骤如下:&&&& * 从www.openqa.org/selenium-ide/download.action下载Selenimu IDE(一个XPI后缀的文件)。&&&& * 启动FIREFOX浏览器,打开刚才下载的文件。&&&& * 重启FIREFOX浏览器,在工具菜条下应该就可以看到Selenium IDE菜单项&&---------------------------------------------------------------------------------------------------------&一 Selenium-RC 是 selenium-remote control 缩写,是使用具体的语言来编写测试类。二 准备工作: 1,下载 selenium 了,到 http://www.openqa.org/selenium/ 下载就可以了,记得选择selenium-rc 的版本&&&&&&&&&&&&& 2, 学习一下xpath 的知识。有个教程:http://www.zvon.org/xxl/XPathTutorial/General_chi/examples.html&&&&&&&&&&&&& 3, 安装 jdk1.5&三 selenium-rc 一些使用方法&&&& 1,解压selenium-rc压缩包&&&& 2,启动服务器&&&&&&&&& Selenium Server是用JAVA实现的,相应的库文件在HOME/server/selenium-server.jar。运行如下代码从命令行启动:&&&&&&& java 代码 : java -jar selunium-server.jar&四&& 编写测试用例&&&& 需要的JAR: selenium-java-client-driver.junit&&&& 编写一个JUNIT的单元测试,要构建一个Selenium,包括如下步骤:&&&& * 构建一个Selenium实例&&&& * 启动Selenium实例&&&& * 执行Selenium命令,并验证结果。&&&& * 关闭Selenium实例&如下是一个示例,用来测试/,查找selenium,期望结果中包含"OpenQA: Selenium"&&&代码package&com.thoughtworks.&&&&&&import&junit.framework.*;&&&import&org.openqa.selenium.server.*;&&&public&class&GoogleTest&extends&TestCase&&&&{&&&&&&&private&Selenium&&&&&&&&public&void&setUp()&throws&Exception&{&&&&&&&&&&&&String&url&=&"";&&&&&&&&&&&&selenium&=&new&DefaultSelenium("localhost",&SeleniumServer.getDefaultPort(),&"*firefox",&url);&&&&&&&&&&&&selenium.start();&&&&&&&}&&&&&&&&&&&&&&&&protected&void&tearDown()&throws&Exception&{&&&&&&&&&&&&&selenium.stop();&&&&&&&}&&&&&&&&&&&&&&&&public&void&testGoogleTestSearch()&throws&Throwable&{&&&&&&&&&&&&&selenium.open("/intl/zh-CN/");&&&&&&&&&&&&&selenium.type("q",&"selenium");&&&&&&&&&&&&&selenium.click("btnG");&&&&&&&&&&&&&selenium.waitForPageToLoad("30000");&&&&&&&&&&&&&assertEquals("selenium&-&Google&搜索",&selenium.getTitle());&&&&&&&&&&&&&&&}&&&}&&&&&五 多环境测试&&&代码&&&六 如何选取元素&& selenium提供如下强大的定位元素的方法。&&& * id=id&&& * name=name&&& * dom=javascriptExpression&&& * xpath=xpathExpression&&& * link=textPattern&&& * css=cssSelectorSyntax1 通过ID,name选择元素& :& 如 selenium.type("id=q","百度"); selenium.type("name=search","百度")2 link= 根据链接文字来操作:如 selenium.click("link=个人资料");3 根据XPath来选择元素& : XPath Checker&&& * xpath=//img[@alt='The image alt text']&&& * xpath=//table[@id='table1']//tr[4]/td[2]&&& * xpath=//a[contains(@href,'#id1')]&&& * xpath=//a[contains(@href,'#id1')]/@class&&& * xpath=(//table[@class='stylee'])//th[text()='theHeaderText']/../td&&& * xpath=//input[@name='name2' and @value='yes']&&& * xpath=//*[text()="right"]&&&&& 如: selenium.type("xpath=//input[@name='user.email']", ""); // 向input中type为text的栏位键入信息&&&&&&&&& selenium.check("xpath=//input[(@name='user.sex')and(@value='男')]");// 向input中type为radiod的 选取4 dom选择&&& * dom=document.forms['myForm'].myDropdown&&& * dom=document.images[56]&&& * dom=function foo() { return document.links[1]; }; foo();5 css选择器这个不常用,它可以支持css2, css3选择器&&& * css=a[href="#id3"]&&& * css=span#firstChild + span七 使用selenium 这个对象来进行测试1& 获取标 : assertEquals("Insert title here", selenium.getTitle());2& 判断页面是否存在一个user.email元素& :assertTrue(selenium.isElementPresent("xpath=//input[@name='user.email']"));3& 得到文本框里的文字:&& assertEquals(selenium.getValue("xpath=//input[@name='user.username']"),"xxxaas");4& 测试check box& :&&& assertTrue(selenium.isChecked("xpath=//input[(@name='user.sex')and(@value='男')]"));5& 点击提交按钮&& : selenium.click("xpath=//input[@type='button']");6& 等待页面载入&& : selenium.waitForPageToLoad("2000");7& 验证指定文本出现在提交给用户的页面上: assertTrue(selenium.isTextPresent("验证码输入有误,请核实后再输入"));8& 判断下拉框里选择了哪个选项 :assertEquals(selenium.getSelectedIndex("xpath=//SELECT[@name='HATIMING']"), "1");&9& 如何测试一些错误消息的显示? assertTrue(selenium.getBodyText().indexOf("错误消息")&=0);&&& getBodyText 返回的时浏览器页面上的文字,不回包含html 代码的,如果要显示html 代码,用下面这个:selenium.getHtmlSource();&八&& Firefox 的插件&&&& 1 XPath Checker :可以用这个工具测试简化我们的xpath表达式&&&& 2 Firebug&&&& 3 Selenium IDE&&&& 4 Execute JS
阅读(...) 评论()8156人阅读
(一) To Handle Ajax
Selenium WebDriver在加载页面的时候,无论是driver.get(url)或者driver.getPageSource(),会立即返回当时页面的数据。但当代的网页技术都大量使用了异步Ajax调用,这使得许多DOM元素的创建和加载,都分布在页面load结束后的许多零散的时间点,让WebDriver的findElement经常无功而返。这个时候,一般有两种方式来解决问题:
1. 利用Selenium WebDriver原生的API,进行预判等待元素出现
演示代码如下:
使用FluentWait + Predicate接口
FluentWait&By& fluentWait = new FluentWait&By&(By.tagName(&TEXTAREA&));
fluentWait.pollingEvery(100, TimeUnit.MILLISECONDS);
fluentWait.withTimeout(1000, TimeUnit.MILLISECONDS);
fluentWait.until(new Predicate&By&() {
public boolean apply(By by) {
return browser.findElement(by).isDisplayed();
} catch (NoSuchElementException ex) {
browser.findElement(By.tagName(&TEXTAREA&)).sendKeys(&text to enter&);
或者也可以直接使用Wait接口的until方法,传入一个ExpectedCondition
public ExpectedCondition&WebElement& visibilityOfElementLocated(final By by) {
return new ExpectedCondition&WebElement&() {
public WebElement apply(WebDriver driver) {
WebElement element = driver.findElement(by);
return element.isDisplayed() ? element :
public void performSomeAction() {
Wait&WebDriver& wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(visibilityOfElementLocated(By.tagName(&a&)));
2. 利用Javascript注入,来读取不同Ajax调用框架的Ajax request status,一直等到Ajax调用全部返回才开始分析操作Dom元素
演示代码如下:
protected void syncAjaxByJQuery(String timeout) {
boolean isSucceed =
selenium.waitForCondition(
&selenium.browserbot.getCurrentWindow().jQuery.active == 0&, timeout);
isSucceed =
} catch (SeleniumException se) {
LOG.error(se);
} catch (Exception re) {
throw new RuntimeException(re.getMessage());
operationCheck(isSucceed);
protected void syncAjaxByPrototype(String timeout) {
boolean isSucceed =
selenium.waitForCondition(
&selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount == 0&,
isSucceed =
} catch (SeleniumException se) {
LOG.error(se);
} catch (Exception re) {
throw new RuntimeException(re.getMessage());
operationCheck(isSucceed);
protected void syncAjaxByDojo(String timeout) {
boolean isSucceed =
selenium.waitForCondition(
&selenium.browserbot.getCurrentWindow().dojo.io.XMLHTTPTransport.inFlight.length == 0&,
isSucceed =
} catch (SeleniumException se) {
LOG.error(se);
} catch (Exception re) {
throw new RuntimeException(re.getMessage());
operationCheck(isSucceed);
(二) To Handle Alert
最近遇到许多网页用Selenium操作的时候,网页本身会弹出许多Alert模态窗口,这种模态窗口会阻断driver继续执行。虽然WebDriver已经提供了些许方法来处理模态Alert窗口,但是依然会在不同的浏览器上存在未修复的Bug。先来看看WebDriver提供的处理Alert的方式,基本就是通过driver.switchTo()方法获得Alert模态窗口的句柄,然后调用Alert.accept()或者Alert.dismiss()方法接受或者拒绝,同样的操作也适用于Confirm窗口。我为了防止意外情况下WebDriver内置Alert处理的代码会崩溃,所以特意设计了一个超时机制,一旦超时就转而使用原始键盘模拟输入回车来消除窗口,使用的也是java.awt.Robot类来模拟操作系统的键盘输入动作。
演示代码如下: & & &
public T handleAlert(){
KeyboardUtils.enter();
long clockTime = System.currentTimeMillis();
long timeout = 3 * 1000;
while(true){
if((System.currentTimeMillis()-clockTime) & timeout){
KeyboardUtils.enter();
}else if(WebDriverUtils.isAlertExist(driver)){
Alert alert = driver.switchTo().alert();
alert.getText();
alert.accept();
}catch(Exception ex){
KeyboardUtils.enter();
waiting(300);
//In WebDriverUtils.class
public static boolean isAlertExist(WebDriver driver){
driver.switchTo().alert();
}catch(NoAlertPresentException
(三) To Handle more than Alert
还有一种情况,当我用Firefox打开某些游戏网页的时候,网页本身可能是Flash展示,它会自动触发Firefox弹出收藏窗口,添加本页进浏览器的Bookmark,这种窗口一旦出现,driver直接停止响应,代码本身都不会再继续往下走了。所以更别提你下面还有处理Alert窗口的代码,那都是浮云,本线程直接就阻塞了,这一度让我非常的火大。后来实在没办法,想了一种折中的方式,设计了另外一个线程进行心跳检测。主线程如果一直都持续运行,定期汇报心跳。一旦超过一段时间没有收到心跳信号,那么独立的心跳检测线程就会试图去模拟回车输入,或者其他动作,来进一步确保浏览器不会被各种奇怪的现象阻塞不能继续。
(四) Other Tips
1. 加载IEDriver的时候,通常会因为兼容模式的设置问题,而无法启动,尝试在创建IEDriver对象的时候,加入合适的参数设置:
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
return new InternetExplorerDriver(ieCapabilities);
2. 关闭浏览器的时候,例如新浪主页,本身会弹出一个独立的广告窗口,如果调用driver.close的时候句柄焦点在主窗口上,那么广告窗口是无法关闭的。所以在关闭driver之前,试图检查一下还有无别的独立窗口句柄存在:
public static void closeWebDriver(WebDriver driver){
if(driver == null)
String current = driver.getWindowHandle();
Set&String& otherWins = driver.getWindowHandles();
for(String winId : otherWins)
if(winId.equals(current))
driver.switchTo().window(winId).close();
}catch(Exception ex){
driver.quit();
}catch(Exception ex){}
3. 如何使用WebDriver进行网页截图,提示一点在Flash页面中截图可能会得到一片黑色的屏幕,另外WebDriver截图会把当时整个网页哪怕是需要滚动才显示出来的部分,都会包含在截图里面。
public static String takeScreenshot(WebDriver driver, String savePath, long delay){
CommonUtils.waiting(delay * 1000);
File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String path = &screenshot/&+saveP
if(!savePath.endsWith(&/.png&))
path = path + &.png&;
FileUtils.copyFile(screenShotFile, new File(path));
(&Take screenshot at &+savePath);
} catch (IOException ex) {
LOG.warn(&failed to take screenshot for current page, caused by &+ex.getMessage());
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:438430次
积分:5742
积分:5742
排名:第2737名
原创:119篇
转载:74篇
评论:138条
(2)(3)(2)(4)(1)(2)(2)(1)(2)(1)(1)(1)(1)(6)(2)(7)(3)(3)(3)(1)(7)(6)(5)(4)(3)(1)(1)(2)(1)(2)(1)(2)(2)(3)(2)(6)(3)(7)(6)(5)(2)(5)(10)(6)(3)(2)(5)(5)(8)(6)(2)(5)(4)(4)(12)selenium处理rich text(富文本框)
WordPress 的 rich &text
采用js,先让selenium切换到iframe中
driver.switchTo().frame(&content_ifr&);
然后执行JS
WebElement editor = driver.findElement(By.tagName(&body&));
& &scriptExecutor jsExecutor = (JavascriptExecutor)
& &jsExecutor.executeScript(&arguments[0].innerHTML = '&h1&Selenium Test &/h1&I love Selenium &br& this article Post By Selenium WebDriver&br&&h2&Create By Young&/h2&'&, editor);
为什么是innerHTML?
最后再切换出iframe
& &driver.switchTo().defaultContent();
&public void post()
& & & //login();
& & & WebElement article=driver.findElement(By.xpath(&//*[@id='menu-posts']/a/div[3]&));
& & & article.click();
& & & try {
& & & & Thread.sleep(2000);
& & } catch (InterruptedException e) {
& & & & // TODO Auto-generated catch block
& & & & e.printStackTrace();
& & & WebElement newArticle=driver.findElement(By.linkText(&写文章&));
& & & newArticle.click();
& & & WebElement title=driver.findElement(By.xpath(&//*[@id='title']&));
& & & title.sendKeys(&I love Selenium&);
& & & driver.switchTo().frame(&content_ifr&);
& & & WebElement editor = driver.findElement(By.tagName(&body&));
& & & JavascriptExecutor jsExecutor = (JavascriptExecutor)
& & & jsExecutor.executeScript(&arguments[0].innerHTML = '&h1&Selenium Test &/h1&I love Selenium &br& this article Post By Selenium WebDriver&br&&h2&Create By Young&/h2&'&, editor);
& & & driver.switchTo().defaultContent();
& & & WebElement submit =driver.findElement(By.xpath(&//*[@id='publish']&));
& & & submit.click();
& & & try {
& & & & Thread.sleep(3000);
& & } catch (InterruptedException e) {
& & & & // TODO Auto-generated catch block
& & & & e.printStackTrace();
& & &WebElement check=driver.findElement(By.xpath(&//*[@id='message']/p&));
& & &Assert.assertTrue(check.getText().contains(&文章已发布&));
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'Selenium技巧-如何做到随机选择
问题场景:
表单里有很多选项,如果想在脚本中加入随机选择一些输入值怎么做
比如性别选项,想随机选择男或者女
解决办法:
#找到该选择项元素
c_SEX = driver.find_element_by_id(&c_SEX&)
c_SEX.click()
#.options方法获得所有可选项,是一个元素对象列表&&&&&&&&&&&&&&&&&&&&&&&&&&&
ret = Select(c_SEX).options
#ret[1:]表示从列表中剔除第一个选项,因为第一个一般为请选择,不是实际的选项;random.Random().choice方法是python自带的,可以从一个序列中随机选择一个值
srand = random.Random().choice(ret[1:])
#srand.get_attribute(&value&)得到选项的value,用Select.select_by_value方法选择该等于该值的选项
Select(c_SEX).select_by_value(srand.get_attribute(&value&))
看一看效果:
作者:snowshine_2001
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'君,已阅读到文档的结尾了呢~~
在VS2010中用C#使用Selenium..
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
VS2010+Selenium测试脚本设计V1.0
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 2010年最火网页游戏 的文章

 

随机推荐