selenium webdriver学习(十六)----------用selenium webdriver实现selenium RC中的类似的方法

最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法。目前封装了一个ActionDriverHelper类,来实现RC中Selenium.java和DefaultSelenium.java中的方法。有一些方法还没有实现,写的方法大多没有经过测试,仅供参考。代码如下:

Java代码 收藏代码
  1. packagecore;
  2. importjava.io.File;
  3. importjava.io.IOException;
  4. importjava.util.HashSet;
  5. importjava.util.List;
  6. importjava.util.Set;
  7. importjava.util.concurrent.TimeUnit;
  8. importorg.apache.commons.io.FileUtils;
  9. importorg.openqa.selenium.By;
  10. importorg.openqa.selenium.Cookie;
  11. importorg.openqa.selenium.Dimension;
  12. importorg.openqa.selenium.JavascriptExecutor;
  13. importorg.openqa.selenium.Keys;
  14. importorg.openqa.selenium.NoSuchElementException;
  15. importorg.openqa.selenium.OutputType;
  16. importorg.openqa.selenium.Point;
  17. importorg.openqa.selenium.TakesScreenshot;
  18. importorg.openqa.selenium.WebDriver;
  19. importorg.openqa.selenium.WebElement;
  20. importorg.openqa.selenium.WebDriver.Timeouts;
  21. importorg.openqa.selenium.interactions.Actions;
  22. importorg.openqa.selenium.support.ui.Select;
  23. publicclassActionDriverHelper{
  24. protectedWebDriverdriver;
  25. publicActionDriverHelper(WebDriverdriver){
  26. this.driver=driver;
  27. }
  28. publicvoidclick(Byby){
  29. driver.findElement(by).click();
  30. }
  31. publicvoiddoubleClick(Byby){
  32. newActions(driver).doubleClick(driver.findElement(by)).perform();
  33. }
  34. publicvoidcontextMenu(Byby){
  35. newActions(driver).contextClick(driver.findElement(by)).perform();
  36. }
  37. publicvoidclickAt(Byby,StringcoordString){
  38. intindex=coordString.trim().indexOf(',');
  39. intxOffset=Integer.parseInt(coordString.trim().substring(0,index));
  40. intyOffset=Integer.parseInt(coordString.trim().substring(index+1));
  41. newActions(driver).moveToElement(driver.findElement(by),xOffset,yOffset).click().perform();
  42. }
  43. publicvoiddoubleClickAt(Byby,StringcoordString){
  44. intindex=coordString.trim().indexOf(',');
  45. intxOffset=Integer.parseInt(coordString.trim().substring(0,index));
  46. intyOffset=Integer.parseInt(coordString.trim().substring(index+1));
  47. newActions(driver).moveToElement(driver.findElement(by),xOffset,yOffset)
  48. .doubleClick(driver.findElement(by))
  49. .perform();
  50. }
  51. publicvoidcontextMenuAt(Byby,StringcoordString){
  52. intindex=coordString.trim().indexOf(',');
  53. intxOffset=Integer.parseInt(coordString.trim().substring(0,index));
  54. intyOffset=Integer.parseInt(coordString.trim().substring(index+1));
  55. newActions(driver).moveToElement(driver.findElement(by),xOffset,yOffset)
  56. .contextClick(driver.findElement(by))
  57. .perform();
  58. }
  59. publicvoidfireEvent(Byby,StringeventName){
  60. System.out.println("webdriver不建议使用这样的方法,所以没有实现。");
  61. }
  62. publicvoidfocus(Byby){
  63. System.out.println("webdriver不建议使用这样的方法,所以没有实现。");
  64. }
  65. publicvoidkeyPress(Byby,KeystheKey){
  66. newActions(driver).keyDown(driver.findElement(by),theKey).release().perform();
  67. }
  68. publicvoidshiftKeyDown(){
  69. newActions(driver).keyDown(Keys.SHIFT).perform();
  70. }
  71. publicvoidshiftKeyUp(){
  72. newActions(driver).keyUp(Keys.SHIFT).perform();
  73. }
  74. publicvoidmetaKeyDown(){
  75. newActions(driver).keyDown(Keys.META).perform();
  76. }
  77. publicvoidmetaKeyUp(){
  78. newActions(driver).keyUp(Keys.META).perform();
  79. }
  80. publicvoidaltKeyDown(){
  81. newActions(driver).keyDown(Keys.ALT).perform();
  82. }
  83. publicvoidaltKeyUp(){
  84. newActions(driver).keyUp(Keys.ALT).perform();
  85. }
  86. publicvoidcontrolKeyDown(){
  87. newActions(driver).keyDown(Keys.CONTROL).perform();
  88. }
  89. publicvoidcontrolKeyUp(){
  90. newActions(driver).keyUp(Keys.CONTROL).perform();
  91. }
  92. publicvoidKeyDown(KeystheKey){
  93. newActions(driver).keyDown(theKey).perform();
  94. }
  95. publicvoidKeyDown(Byby,KeystheKey){
  96. newActions(driver).keyDown(driver.findElement(by),theKey).perform();
  97. }
  98. publicvoidKeyUp(KeystheKey){
  99. newActions(driver).keyUp(theKey).perform();
  100. }
  101. publicvoidKeyUp(Byby,KeystheKey){
  102. newActions(driver).keyUp(driver.findElement(by),theKey).perform();
  103. }
  104. publicvoidmouseOver(Byby){
  105. newActions(driver).moveToElement(driver.findElement(by)).perform();
  106. }
  107. publicvoidmouseOut(Byby){
  108. System.out.println("没有实现!");
  109. //newActions(driver).moveToElement((driver.findElement(by)),-10,-10).perform();
  110. }
  111. publicvoidmouseDown(Byby){
  112. newActions(driver).clickAndHold(driver.findElement(by)).perform();
  113. }
  114. publicvoidmouseDownRight(Byby){
  115. System.out.println("没有实现!");
  116. }
  117. publicvoidmouseDownAt(Byby,StringcoordString){
  118. System.out.println("没有实现!");
  119. }
  120. publicvoidmouseDownRightAt(Byby,StringcoordString){
  121. System.out.println("没有实现!");
  122. }
  123. publicvoidmouseUp(Byby){
  124. System.out.println("没有实现!");
  125. }
  126. publicvoidmouseUpRight(Byby){
  127. System.out.println("没有实现!");
  128. }
  129. publicvoidmouseUpAt(Byby,StringcoordString){
  130. System.out.println("没有实现!");
  131. }
  132. publicvoidmouseUpRightAt(Byby,StringcoordString){
  133. System.out.println("没有实现!");
  134. }
  135. publicvoidmouseMove(Byby){
  136. newActions(driver).moveToElement(driver.findElement(by)).perform();
  137. }
  138. publicvoidmouseMoveAt(Byby,StringcoordString){
  139. intindex=coordString.trim().indexOf(',');
  140. intxOffset=Integer.parseInt(coordString.trim().substring(0,index));
  141. intyOffset=Integer.parseInt(coordString.trim().substring(index+1));
  142. newActions(driver).moveToElement(driver.findElement(by),xOffset,yOffset).perform();
  143. }
  144. publicvoidtype(Byby,Stringtestdata){
  145. driver.findElement(by).clear();
  146. driver.findElement(by).sendKeys(testdata);
  147. }
  148. publicvoidtypeKeys(Byby,Keyskey){
  149. driver.findElement(by).sendKeys(key);
  150. }
  151. publicvoidsetSpeed(Stringvalue){
  152. System.out.println("ThemethodstosettheexecutionspeedinWebDriverweredeprecated");
  153. }
  154. publicStringgetSpeed(){
  155. System.out.println("ThemethodstosettheexecutionspeedinWebDriverweredeprecated");
  156. returnnull;
  157. }
  158. publicvoidcheck(Byby){
  159. if(!isChecked(by))
  160. click(by);
  161. }
  162. publicvoiduncheck(Byby){
  163. if(isChecked(by))
  164. click(by);
  165. }
  166. publicvoidselect(Byby,StringoptionValue){
  167. newSelect(driver.findElement(by)).selectByValue(optionValue);
  168. }
  169. publicvoidselect(Byby,intindex){
  170. newSelect(driver.findElement(by)).selectByIndex(index);
  171. }
  172. publicvoidaddSelection(Byby,StringoptionValue){
  173. select(by,optionValue);
  174. }
  175. publicvoidaddSelection(Byby,intindex){
  176. select(by,index);
  177. }
  178. publicvoidremoveSelection(Byby,Stringvalue){
  179. newSelect(driver.findElement(by)).deselectByValue(value);
  180. }
  181. publicvoidremoveSelection(Byby,intindex){
  182. newSelect(driver.findElement(by)).deselectByIndex(index);
  183. }
  184. publicvoidremoveAllSelections(Byby){
  185. newSelect(driver.findElement(by)).deselectAll();
  186. }
  187. publicvoidsubmit(Byby){
  188. driver.findElement(by).submit();
  189. }
  190. publicvoidopen(Stringurl){
  191. driver.get(url);
  192. }
  193. publicvoidopenWindow(Stringurl,Stringhandler){
  194. System.out.println("方法没有实现!");
  195. }
  196. publicvoidselectWindow(Stringhandler){
  197. driver.switchTo().window(handler);
  198. }
  199. publicStringgetCurrentHandler(){
  200. StringcurrentHandler=driver.getWindowHandle();
  201. returncurrentHandler;
  202. }
  203. publicStringgetSecondWindowHandler(){
  204. Set<String>handlers=driver.getWindowHandles();
  205. StringreHandler=getCurrentHandler();
  206. for(Stringhandler:handlers){
  207. if(reHandler.equals(handler))continue;
  208. reHandler=handler;
  209. }
  210. returnreHandler;
  211. }
  212. publicvoidselectPopUp(Stringhandler){
  213. driver.switchTo().window(handler);
  214. }
  215. publicvoidselectPopUp(){
  216. driver.switchTo().window(getSecondWindowHandler());
  217. }
  218. publicvoiddeselectPopUp(){
  219. driver.switchTo().window(getCurrentHandler());
  220. }
  221. publicvoidselectFrame(intindex){
  222. driver.switchTo().frame(index);
  223. }
  224. publicvoidselectFrame(Stringstr){
  225. driver.switchTo().frame(str);
  226. }
  227. publicvoidselectFrame(Byby){
  228. driver.switchTo().frame(driver.findElement(by));
  229. }
  230. publicvoidwaitForPopUp(StringwindowID,Stringtimeout){
  231. System.out.println("没有实现");
  232. }
  233. publicvoidaccept(){
  234. driver.switchTo().alert().accept();
  235. }
  236. publicvoiddismiss(){
  237. driver.switchTo().alert().dismiss();
  238. }
  239. publicvoidchooseCancelOnNextConfirmation(){
  240. driver.switchTo().alert().dismiss();
  241. }
  242. publicvoidchooseOkOnNextConfirmation(){
  243. driver.switchTo().alert().accept();
  244. }
  245. publicvoidanswerOnNextPrompt(Stringanswer){
  246. driver.switchTo().alert().sendKeys(answer);
  247. }
  248. publicvoidgoBack(){
  249. driver.navigate().back();
  250. }
  251. publicvoidrefresh(){
  252. driver.navigate().refresh();
  253. }
  254. publicvoidforward(){
  255. driver.navigate().forward();
  256. }
  257. publicvoidto(StringurlStr){
  258. driver.navigate().to(urlStr);
  259. }
  260. publicvoidclose(){
  261. driver.close();
  262. }
  263. publicbooleanisAlertPresent(){
  264. Booleanb=true;
  265. try{
  266. driver.switchTo().alert();
  267. }catch(Exceptione){
  268. b=false;
  269. }
  270. returnb;
  271. }
  272. publicbooleanisPromptPresent(){
  273. returnisAlertPresent();
  274. }
  275. publicbooleanisConfirmationPresent(){
  276. returnisAlertPresent();
  277. }
  278. publicStringgetAlert(){
  279. returndriver.switchTo().alert().getText();
  280. }
  281. publicStringgetConfirmation(){
  282. returngetAlert();
  283. }
  284. publicStringgetPrompt(){
  285. returngetAlert();
  286. }
  287. publicStringgetLocation(){
  288. returndriver.getCurrentUrl();
  289. }
  290. publicStringgetTitle(){
  291. returndriver.getTitle();
  292. }
  293. publicStringgetBodyText(){
  294. Stringstr="";
  295. List<WebElement>elements=driver.findElements(By.xpath("//body//*[contains(text(),*)]"));
  296. for(WebElemente:elements){
  297. str+=e.getText()+"";
  298. }
  299. returnstr;
  300. }
  301. publicStringgetValue(Byby){
  302. returndriver.findElement(by).getAttribute("value");
  303. }
  304. publicStringgetText(Byby){
  305. returndriver.findElement(by).getText();
  306. }
  307. publicvoidhighlight(Byby){
  308. WebElementelement=driver.findElement(by);
  309. ((JavascriptExecutor)driver).executeScript("arguments[0].style.border=\"5pxsolidyellow\"",element);
  310. }
  311. publicObjectgetEval(Stringscript,Object...args){
  312. return((JavascriptExecutor)driver).executeScript(script,args);
  313. }
  314. publicObjectgetAsyncEval(Stringscript,Object...args){
  315. return((JavascriptExecutor)driver).executeAsyncScript(script,args);
  316. }
  317. publicbooleanisChecked(Byby){
  318. returndriver.findElement(by).isSelected();
  319. }
  320. publicStringgetTable(Byby,StringtableCellAddress){
  321. WebElementtable=driver.findElement(by);
  322. intindex=tableCellAddress.trim().indexOf('.');
  323. introw=Integer.parseInt(tableCellAddress.substring(0,index));
  324. intcell=Integer.parseInt(tableCellAddress.substring(index+1));
  325. List<WebElement>rows=table.findElements(By.tagName("tr"));
  326. WebElementtheRow=rows.get(row);
  327. Stringtext=getCell(theRow,cell);
  328. returntext;
  329. }
  330. privateStringgetCell(WebElementRow,intcell){
  331. List<WebElement>cells;
  332. Stringtext=null;
  333. if(Row.findElements(By.tagName("th")).size()>0){
  334. cells=Row.findElements(By.tagName("th"));
  335. text=cells.get(cell).getText();
  336. }
  337. if(Row.findElements(By.tagName("td")).size()>0){
  338. cells=Row.findElements(By.tagName("td"));
  339. text=cells.get(cell).getText();
  340. }
  341. returntext;
  342. }
  343. publicString[]getSelectedLabels(Byby){
  344. Set<String>set=newHashSet<String>();
  345. List<WebElement>selectedOptions=newSelect(driver.findElement(by))
  346. .getAllSelectedOptions();
  347. for(WebElemente:selectedOptions){
  348. set.add(e.getText());
  349. }
  350. returnset.toArray(newString[set.size()]);
  351. }
  352. publicStringgetSelectedLabel(Byby){
  353. returngetSelectedOption(by).getText();
  354. }
  355. publicString[]getSelectedValues(Byby){
  356. Set<String>set=newHashSet<String>();
  357. List<WebElement>selectedOptions=newSelect(driver.findElement(by))
  358. .getAllSelectedOptions();
  359. for(WebElemente:selectedOptions){
  360. set.add(e.getAttribute("value"));
  361. }
  362. returnset.toArray(newString[set.size()]);
  363. }
  364. publicStringgetSelectedValue(Byby){
  365. returngetSelectedOption(by).getAttribute("value");
  366. }
  367. publicString[]getSelectedIndexes(Byby){
  368. Set<String>set=newHashSet<String>();
  369. List<WebElement>selectedOptions=newSelect(driver.findElement(by))
  370. .getAllSelectedOptions();
  371. List<WebElement>options=newSelect(driver.findElement(by)).getOptions();
  372. for(WebElemente:selectedOptions){
  373. set.add(String.valueOf(options.indexOf(e)));
  374. }
  375. returnset.toArray(newString[set.size()]);
  376. }
  377. publicStringgetSelectedIndex(Byby){
  378. List<WebElement>options=newSelect(driver.findElement(by)).getOptions();
  379. returnString.valueOf(options.indexOf(getSelectedOption(by)));
  380. }
  381. publicString[]getSelectedIds(Byby){
  382. Set<String>ids=newHashSet<String>();
  383. List<WebElement>options=newSelect(driver.findElement(by)).getOptions();
  384. for(WebElementoption:options){
  385. if(option.isSelected()){
  386. ids.add(option.getAttribute("id"));
  387. }
  388. }
  389. returnids.toArray(newString[ids.size()]);
  390. }
  391. publicStringgetSelectedId(Byby){
  392. returngetSelectedOption(by).getAttribute("id");
  393. }
  394. privateWebElementgetSelectedOption(Byby){
  395. WebElementselectedOption=null;
  396. List<WebElement>options=newSelect(driver.findElement(by)).getOptions();
  397. for(WebElementoption:options){
  398. if(option.isSelected()){
  399. selectedOption=option;
  400. }
  401. }
  402. returnselectedOption;
  403. }
  404. publicbooleanisSomethingSelected(Byby){
  405. booleanb=false;
  406. List<WebElement>options=newSelect(driver.findElement(by)).getOptions();
  407. for(WebElementoption:options){
  408. if(option.isSelected()){
  409. b=true;
  410. break;
  411. }
  412. }
  413. returnb;
  414. }
  415. publicString[]getSelectOptions(Byby){
  416. Set<String>set=newHashSet<String>();
  417. List<WebElement>options=newSelect(driver.findElement(by)).getOptions();
  418. for(WebElemente:options){
  419. set.add(e.getText());
  420. }
  421. returnset.toArray(newString[set.size()]);
  422. }
  423. publicStringgetAttribute(Byby,StringattributeLocator){
  424. returndriver.findElement(by).getAttribute(attributeLocator);
  425. }
  426. publicbooleanisTextPresent(Stringpattern){
  427. StringXpath="//*[contains(text(),\'"+pattern+"\')]";
  428. try{
  429. driver.findElement(By.xpath(Xpath));
  430. returntrue;
  431. }catch(NoSuchElementExceptione){
  432. returnfalse;
  433. }
  434. }
  435. publicbooleanisElementPresent(Byby){
  436. returndriver.findElements(by).size()>0;
  437. }
  438. publicbooleanisVisible(Byby){
  439. returndriver.findElement(by).isDisplayed();
  440. }
  441. publicbooleanisEditable(Byby){
  442. returndriver.findElement(by).isEnabled();
  443. }
  444. publicList<WebElement>getAllButtons(){
  445. returndriver.findElements(By.xpath("//input[@type='button']"));
  446. }
  447. publicList<WebElement>getAllLinks(){
  448. returndriver.findElements(By.tagName("a"));
  449. }
  450. publicList<WebElement>getAllFields(){
  451. returndriver.findElements(By.xpath("//input[@type='text']"));
  452. }
  453. publicString[]getAttributeFromAllWindows(StringattributeName){
  454. System.out.println("不知道怎么实现");
  455. returnnull;
  456. }
  457. publicvoiddragdrop(Byby,StringmovementsString){
  458. dragAndDrop(by,movementsString);
  459. }
  460. publicvoiddragAndDrop(Byby,StringmovementsString){
  461. intindex=movementsString.trim().indexOf('.');
  462. intxOffset=Integer.parseInt(movementsString.substring(0,index));
  463. intyOffset=Integer.parseInt(movementsString.substring(index+1));
  464. newActions(driver).clickAndHold(driver.findElement(by)).moveByOffset(xOffset,yOffset).perform();
  465. }
  466. publicvoidsetMouseSpeed(Stringpixels){
  467. System.out.println("不支持");
  468. }
  469. publicNumbergetMouseSpeed(){
  470. System.out.println("不支持");
  471. returnnull;
  472. }
  473. publicvoiddragAndDropToObject(Bysource,Bytarget){
  474. newActions(driver).dragAndDrop(driver.findElement(source),driver.findElement(target)).perform();
  475. }
  476. publicvoidwindowFocus(){
  477. driver.switchTo().defaultContent();
  478. }
  479. publicvoidwindowMaximize(){
  480. driver.manage().window().setPosition(newPoint(0,0));
  481. java.awt.DimensionscreenSize=java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  482. Dimensiondim=newDimension((int)screenSize.getWidth(),(int)screenSize.getHeight());
  483. driver.manage().window().setSize(dim);
  484. }
  485. publicString[]getAllWindowIds(){
  486. System.out.println("不能实现!");
  487. returnnull;
  488. }
  489. publicString[]getAllWindowNames(){
  490. System.out.println("不能实现!");
  491. returnnull;
  492. }
  493. publicString[]getAllWindowTitles(){
  494. Set<String>handles=driver.getWindowHandles();
  495. Set<String>titles=newHashSet<String>();
  496. for(Stringhandle:handles){
  497. titles.add(driver.switchTo().window(handle).getTitle());
  498. }
  499. returntitles.toArray(newString[titles.size()]);
  500. }
  501. publicStringgetHtmlSource(){
  502. returndriver.getPageSource();
  503. }
  504. publicvoidsetCursorPosition(Stringlocator,Stringposition){
  505. System.out.println("没能实现!");
  506. }
  507. publicNumbergetElementIndex(Stringlocator){
  508. System.out.println("没能实现!");
  509. returnnull;
  510. }
  511. publicObjectisOrdered(Byby1,Byby2){
  512. System.out.println("没能实现!");
  513. returnnull;
  514. }
  515. publicNumbergetElementPositionLeft(Byby){
  516. returndriver.findElement(by).getLocation().getX();
  517. }
  518. publicNumbergetElementPositionTop(Byby){
  519. returndriver.findElement(by).getLocation().getY();
  520. }
  521. publicNumbergetElementWidth(Byby){
  522. returndriver.findElement(by).getSize().getWidth();
  523. }
  524. publicNumbergetElementHeight(Byby){
  525. returndriver.findElement(by).getSize().getHeight();
  526. }
  527. publicNumbergetCursorPosition(Stringlocator){
  528. System.out.println("没能实现!");
  529. returnnull;
  530. }
  531. publicStringgetExpression(Stringexpression){
  532. System.out.println("没能实现!");
  533. returnnull;
  534. }
  535. publicNumbergetXpathCount(Byxpath){
  536. returndriver.findElements(xpath).size();
  537. }
  538. publicvoidassignId(Byby,Stringidentifier){
  539. System.out.println("不想实现!");
  540. }
  541. /*publicvoidallowNativeXpath(Stringallow){
  542. commandProcessor.doCommand("allowNativeXpath",newString[]{allow,});
  543. }*/
  544. /*publicvoidignoreAttributesWithoutValue(Stringignore){
  545. commandProcessor.doCommand("ignoreAttributesWithoutValue",newString[]{ignore,});
  546. }*/
  547. publicvoidwaitForCondition(Stringscript,Stringtimeout,Object...args){
  548. Booleanb=false;
  549. inttime=0;
  550. while(time<=Integer.parseInt(timeout)){
  551. b=(Boolean)((JavascriptExecutor)driver).executeScript(script,args);
  552. if(b==true)break;
  553. try{
  554. Thread.sleep(1000);
  555. }catch(InterruptedExceptione){
  556. //TODOAuto-generatedcatchblock
  557. e.printStackTrace();
  558. }
  559. time+=1000;
  560. }
  561. }
  562. publicvoidsetTimeout(Stringtimeout){
  563. driver.manage().timeouts().implicitlyWait(Integer.parseInt(timeout),TimeUnit.SECONDS);
  564. }
  565. publicvoidwaitForPageToLoad(Stringtimeout){
  566. driver.manage().timeouts().pageLoadTimeout(Integer.parseInt(timeout),TimeUnit.SECONDS);
  567. }
  568. publicvoidwaitForFrameToLoad(StringframeAddress,Stringtimeout){
  569. /*driver.switchTo().frame(frameAddress)
  570. .manage()
  571. .timeouts()
  572. .pageLoadTimeout(Integer.parseInt(timeout),TimeUnit.SECONDS);*/
  573. }
  574. publicStringgetCookie(){
  575. Stringcookies="";
  576. Set<Cookie>cookiesSet=driver.manage().getCookies();
  577. for(Cookiec:cookiesSet){
  578. cookies+=c.getName()+"="+c.getValue()+";";
  579. }
  580. returncookies;
  581. }
  582. publicStringgetCookieByName(Stringname){
  583. returndriver.manage().getCookieNamed(name).getValue();
  584. }
  585. publicbooleanisCookiePresent(Stringname){
  586. booleanb=false;
  587. if(driver.manage().getCookieNamed(name)!=null||driver.manage().getCookieNamed(name).equals(null))
  588. b=true;
  589. returnb;
  590. }
  591. publicvoidcreateCookie(Cookiec){
  592. driver.manage().addCookie(c);
  593. }
  594. publicvoiddeleteCookie(Cookiec){
  595. driver.manage().deleteCookie(c);
  596. }
  597. publicvoiddeleteAllVisibleCookies(){
  598. driver.manage().getCookieNamed("fs").isSecure();
  599. }
  600. /*publicvoidsetBrowserLogLevel(StringlogLevel){
  601. }*/
  602. /*publicvoidrunScript(Stringscript){
  603. commandProcessor.doCommand("runScript",newString[]{script,});
  604. }*/
  605. /*publicvoidaddLocationStrategy(StringstrategyName,StringfunctionDefinition){
  606. commandProcessor.doCommand("addLocationStrategy",newString[]{strategyName,functionDefinition,});
  607. }*/
  608. publicvoidcaptureEntirePageScreenshot(Stringfilename){
  609. FilescreenShotFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  610. try{
  611. FileUtils.copyFile(screenShotFile,newFile(filename));
  612. }catch(IOExceptione){
  613. //TODOAuto-generatedcatchblock
  614. e.printStackTrace();
  615. }
  616. }
  617. /*publicvoidrollup(StringrollupName,Stringkwargs){
  618. commandProcessor.doCommand("rollup",newString[]{rollupName,kwargs,});
  619. }
  620. publicvoidaddScript(StringscriptContent,StringscriptTagId){
  621. commandProcessor.doCommand("addScript",newString[]{scriptContent,scriptTagId,});
  622. }
  623. publicvoidremoveScript(StringscriptTagId){
  624. commandProcessor.doCommand("removeScript",newString[]{scriptTagId,});
  625. }
  626. publicvoiduseXpathLibrary(StringlibraryName){
  627. commandProcessor.doCommand("useXpathLibrary",newString[]{libraryName,});
  628. }
  629. publicvoidsetContext(Stringcontext){
  630. commandProcessor.doCommand("setContext",newString[]{context,});
  631. }*/
  632. /*publicvoidattachFile(StringfieldLocator,StringfileLocator){
  633. commandProcessor.doCommand("attachFile",newString[]{fieldLocator,fileLocator,});
  634. }*/
  635. /*publicvoidcaptureScreenshot(Stringfilename){
  636. commandProcessor.doCommand("captureScreenshot",newString[]{filename,});
  637. }*/
  638. publicStringcaptureScreenshotToString(){
  639. Stringscreen=((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
  640. returnscreen;
  641. }
  642. /*publicStringcaptureNetworkTraffic(Stringtype){
  643. returncommandProcessor.getString("captureNetworkTraffic",newString[]{type});
  644. }
  645. */
  646. /*publicvoidaddCustomRequestHeader(Stringkey,Stringvalue){
  647. commandProcessor.getString("addCustomRequestHeader",newString[]{key,value});
  648. }*/
  649. /*publicStringcaptureEntirePageScreenshotToString(Stringkwargs){
  650. returncommandProcessor.getString("captureEntirePageScreenshotToString",newString[]{kwargs,});
  651. }*/
  652. publicvoidshutDown(){
  653. driver.quit();
  654. }
  655. /*publicStringretrieveLastRemoteControlLogs(){
  656. returncommandProcessor.getString("retrieveLastRemoteControlLogs",newString[]{});
  657. }*/
  658. publicvoidkeyDownNative(Keyskeycode){
  659. newActions(driver).keyDown(keycode).perform();
  660. }
  661. publicvoidkeyUpNative(Keyskeycode){
  662. newActions(driver).keyUp(keycode).perform();
  663. }
  664. publicvoidkeyPressNative(Stringkeycode){
  665. newActions(driver).click().perform();
  666. }
  667. publicvoidwaitForElementPresent(Byby){
  668. for(inti=0;i<60;i++){
  669. if(isElementPresent(by)){
  670. break;
  671. }else{
  672. try{
  673. driver.wait(1000);
  674. }catch(InterruptedExceptione){
  675. e.printStackTrace();
  676. }
  677. }
  678. }
  679. }
  680. publicvoidclickAndWaitForElementPresent(Byby,BywaitElement){
  681. click(by);
  682. waitForElementPresent(waitElement);
  683. }
  684. publicBooleanVeryTitle(Stringexception,Stringactual){
  685. if(exception.equals(actual))returntrue;
  686. elsereturnfalse;
  687. }
  688. }



原文来自:http://jarvi.iteye.com/blog/1523737

你可能感兴趣的:(webdriver)