Selenium 1.0使用问题集锦
来源:互联网 发布:中国网络直播平台排名 编辑:程序博客网 时间:2024/06/08 02:18
Selenium 1.0使用问题集锦
1. API
Selenium 1.0主要API参考:http://release.seleniumhq.org/selenium-core/1.0.1/reference.html
2. 多窗口问题
在selenium模拟测试中,多窗口也是一个很关键的问题。例如,我们可能需要打开一个新窗口并在里面执行一些操作。或者我们点击了一个链接后弹出了新窗口,并且要对其做一些检查,等等。
2.1 利用窗口ID
下面的代码实现了打开一个新窗口来做一些操作,然后关闭新窗口并重新回到原来窗口的功能。其中原来窗口(默认窗口)没有ID的话,要选取该窗口可以用selectWindow(null)来执行,表示我们要回到主窗口来。
参考:http://sauceio.com/index.php/2010/05/selenium-tips-working-with-multiple-windows/
browser.openWindow("", renameWindow); browser.selectWindow(renameWindow); browser.open(spacesLocators.WEBCENTER_URL()); spacesNav.openSpacePage(groupSpaceName, locators.DEFAULT_DOCUMENT_PAGE_NAME()); String newFileName = "Sanity_NewVersion"; doclib.folderViewer().fileMenu().renameItem(fileName, newFileName); browser.close(); browser.selectWindow(null);2.2 利用窗口Title
如果我们点击了一个链接后弹出来新窗口,这时候原来的窗口和新窗口的ID都可能为null,因而我们需要利用Title来选窗口,下面是一段简单的节选代码:
conversationItem(converName).click(); SeleniumUtilities.sleep(ConnectConfig.LONGSLEEP); String[] titles= selenium.getAllWindowTitles(); for(int i=0; i<titles.length; i++){ Logger.logMessage("Title is " + titles[i]); if(titles[i].contains(converName)){ soWebClient.setConversationName(converName); selenium.selectWindow(titles[i]); result=true; break; } }
3. 拖拽功能(dragAndDrop)
Selenium有几个api实现了dragAndDrop功能,如dragAndDropToObject (locatorOfObjectToBeDragged,locatorOfDragDestinationObject)和dragAndDrop ( locator,movementsString )
4. 按键的使用
有些时候,我们可能在模拟访问网站的时候需要借助一些键来操作,如多选的Ctrl操作,Shift操作也经常用。Selenium中提供了这样的按键操作api,如shiftKeyDown()等。我们在代入测试代码的时候,只需要按我们操作的顺序在相应的位置加入按键操作即可。例如:
logger.logInfo("Press key down to view next object "+count); selenium.keyDown(rowLocator, "\\40");
5. 选择框的模拟
选择框一般是提供了一个可选的下拉列表,源码会类似于<select><option></option></select>,我之前一直尝试着把选择作为两次的点击来分解,比如先点击select的框的按钮,然后假定下拉列表出来了,再点击相应option的locator。不过最近的几次尝试都发现这种方式无法得到想要的结果,select几乎没有任何改变,也就是没有选择想要的值。
查找了不少资料,才知道原来selenium中有一个函数select能够专门用来处理选择框select ( selectLocator,optionLocator )。不过optionLocator还是有一定的要求的,不是任何的locator都行,api中给出了四种可以用的:
? label=labelPattern: matches options based on their labels, i.e. the visible text. (This is the default.)
o label=regexp:^[Oo]ther
? value=valuePattern: matches options based on their values.
o value=other
? id=id: matches options based on their ids.
o id=option1
? index=index: matches an option based on its index (offset from zero).
o index=2
我自己也尝试用如果locator用value就可以。比如:value=1,而且发现值没加引号是不会错的。或者直接用label value,比如选择项有USD、CHY等,可以直接用select(locator,"USD");
6. SelectFrame
经常会遇到一些编辑框,它可能是frame,并且无法直接type,这时候我们就必须要用到selectFrame。http://blog.csdn.net/smile_juan/article/details/6138730给出了一个例子。当需要选择父 frame时locator用"relative=parent",而选择top frame用 "relative=top"。如果编辑框中用type无法输入的时候,可以考虑用typeKey来输入。
7. Input框的输入问题
当遇到输入框需要输入的时候,有时候用type完后value却并没有改变。于是可能会尝试以下几种:
1) selenium.type(locator,value)
2) selenium.typeKey(locator,value)
3) 将鼠标移到输入框上再模拟按键
4) 利用运行脚本来输入
String numberFieldID = selenium.getAttribute(locators.INVOICE_SEARCH_INVOICE_NUMBER_FIELD(), "id"); //Use script instead String stmt = "document.getElementById('" +numberFieldID+ "').value=\"" + invoiceNumber + "\""; selenium.runScript(stmt);
8. Screenshot
往往测试人员不会监控selenium的运行状态,而是让它自己去跑。可是跑完了后如果有failure却难以知道原因。虽然运行的log可以提供一些信息,但是当时运行错误的时候的界面是怎么个情况呢?所以如果能记录下failure时候的界面将有助于分析。那么selenium提供了captureEntirePageScreenshot函数来捕捉当时的界面。有一些资料分别实现了failure时候自动触发捕捉界面的功能。如:
http://darrellgrainger.blogspot.com/2011/02/generating-screen-capture-on-exception.html
http://blogs.steeplesoft.com/2012/01/grabbing-screenshots-of-failed-selenium-tests/
8.1 TestRule触发
首先定义一个Rule,然后在需要screenshot的测试类中加入如下一个语句:
@Rulepublic TestRule screenshotTestRule = new ScreenshotTestRule();-----------------------------------------------------------------------------class ScreenshotTestRule implements TestRule{ public void captureScreenshot(String fileName){ try { String path = UITConfig.TEST_RESULT_DIR; new File(path+"/screenshot/").mkdirs(); FileOutputStream out = new FileOutputStream(path+"/screenshot/screenshot-" + fileName + ".png"); out.write(selenium.captureScreenshotToString().getBytes()); out.close(); } catch (Exception e) { // No need to crash the tests if the screenshot fails } } public Statement apply(final Statement base, final org.junit.runner.Description description) { return new Statement(){ @Override public void evaluate() throws Throwable{ try{ base.evaluate(); } catch(Throwable t){ captureScreenshot(description.getMethodName()); throw t; } } }; }}
8.2 函数触发
可以指定函数在某种情况下进行抓屏,例如当assert failure时
protected void assertTrue(boolean rc, String passMessage, String failMessage){ if(rc){ logger.logInfo(passMessage); } else{ screenShot.captureScreenShot(); Assert.assertTrue(failMessage, rc); } }
- Selenium 1.0使用问题集锦
- selenium问题集锦
- treeview使用问题集锦
- Hadoop使用问题集锦
- eclipse使用问题集锦
- 虚拟机使用问题集锦
- QTP使用问题集锦
- Xgboost使用问题集锦
- 使用selenium遇到的问题
- selenium rc 使用中的问题
- selenium使用IE 浏览器问题
- selenium使用遇到的问题
- selenium 错误集锦->ElementNotVisibleException
- selenium常见问题集锦
- Bernese使用中的问题集锦
- e63手机使用问题集锦
- wpf mvvm使用问题集锦
- Jmeter使用奇葩问题集锦
- Ubuntu 12.04安装openGL与opencv
- oracle数据库的使用及安装
- 一些简单的Linux命令
- JUnit中标注Annotation介绍
- 利用Firebug和XPath寻找locator
- Selenium 1.0使用问题集锦
- 多重背包问题解法简单分析(POJ 1276为例)
- 塔防游戏
- hdfs文件写入过程流程分析
- HDU 2065
- mac80211
- Canvas中svae()与restore()的使用
- Java接口中属性和方法的默认修饰符
- SQL Server 问题汇总