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

  1. package core;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.util.HashSet;
  5. import java.util.List;
  6. import java.util.Set;
  7. import java.util.concurrent.TimeUnit;
  8. import org.apache.commons.io.FileUtils;
  9. import org.openqa.selenium.By;
  10. import org.openqa.selenium.Cookie;
  11. import org.openqa.selenium.Dimension;
  12. import org.openqa.selenium.JavascriptExecutor;
  13. import org.openqa.selenium.Keys;
  14. import org.openqa.selenium.NoSuchElementException;
  15. import org.openqa.selenium.OutputType;
  16. import org.openqa.selenium.Point;
  17. import org.openqa.selenium.TakesScreenshot;
  18. import org.openqa.selenium.WebDriver;
  19. import org.openqa.selenium.WebElement;
  20. import org.openqa.selenium.WebDriver.Timeouts;
  21. import org.openqa.selenium.interactions.Actions;
  22. import org.openqa.selenium.support.ui.Select;
  23. public class ActionDriverHelper {
  24. protected WebDriver driver;
  25. public ActionDriverHelper(WebDriver driver){
  26. this.driver = driver ;
  27. }
  28. public void click(By by) {
  29. driver.findElement(by).click();
  30. }
  31. public void doubleClick(By by){
  32. new Actions(driver).doubleClick(driver.findElement(by)).perform();
  33. }
  34. public void contextMenu(By by) {
  35. new Actions(driver).contextClick(driver.findElement(by)).perform();
  36. }
  37. public void clickAt(By by,String coordString) {
  38. int index = coordString.trim().indexOf(',');
  39. int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  40. int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  41. new Actions(driver).moveToElement(driver.findElement(by), xOffset, yOffset).click().perform();
  42. }
  43. public void doubleClickAt(By by,String coordString){
  44. int index = coordString.trim().indexOf(',');
  45. int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  46. int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  47. new Actions(driver).moveToElement(driver.findElement(by), xOffset, yOffset)
  48. .doubleClick(driver.findElement(by))
  49. .perform();
  50. }
  51. public void contextMenuAt(By by,String coordString) {
  52. int index = coordString.trim().indexOf(',');
  53. int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  54. int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  55. new Actions(driver).moveToElement(driver.findElement(by), xOffset, yOffset)
  56. .contextClick(driver.findElement(by))
  57. .perform();
  58. }
  59. public void fireEvent(By by,String eventName) {
  60. System.out.println("webdriver 不建议使用这样的方法,所以没有实现。");
  61. }
  62. public void focus(By by) {
  63. System.out.println("webdriver 不建议使用这样的方法,所以没有实现。");
  64. }
  65. public void keyPress(By by,Keys theKey) {
  66. new Actions(driver).keyDown(driver.findElement(by), theKey).release().perform();
  67. }
  68. public void shiftKeyDown() {
  69. new Actions(driver).keyDown(Keys.SHIFT).perform();
  70. }
  71. public void shiftKeyUp() {
  72. new Actions(driver).keyUp(Keys.SHIFT).perform();
  73. }
  74. public void metaKeyDown() {
  75. new Actions(driver).keyDown(Keys.META).perform();
  76. }
  77. public void metaKeyUp() {
  78. new Actions(driver).keyUp(Keys.META).perform();
  79. }
  80. public void altKeyDown() {
  81. new Actions(driver).keyDown(Keys.ALT).perform();
  82. }
  83. public void altKeyUp() {
  84. new Actions(driver).keyUp(Keys.ALT).perform();
  85. }
  86. public void controlKeyDown() {
  87. new Actions(driver).keyDown(Keys.CONTROL).perform();
  88. }
  89. public void controlKeyUp() {
  90. new Actions(driver).keyUp(Keys.CONTROL).perform();
  91. }
  92. public void KeyDown(Keys theKey) {
  93. new Actions(driver).keyDown(theKey).perform();
  94. }
  95. public void KeyDown(By by,Keys theKey){
  96. new Actions(driver).keyDown(driver.findElement(by), theKey).perform();
  97. }
  98. public void KeyUp(Keys theKey){
  99. new Actions(driver).keyUp(theKey).perform();
  100. }
  101. public void KeyUp(By by,Keys theKey){
  102. new Actions(driver).keyUp(driver.findElement(by), theKey).perform();
  103. }
  104. public void mouseOver(By by) {
  105. new Actions(driver).moveToElement(driver.findElement(by)).perform();
  106. }
  107. public void mouseOut(By by) {
  108. System.out.println("没有实现!");
  109. //new Actions(driver).moveToElement((driver.findElement(by)), -10, -10).perform();
  110. }
  111. public void mouseDown(By by) {
  112. new Actions(driver).clickAndHold(driver.findElement(by)).perform();
  113. }
  114. public void mouseDownRight(By by) {
  115. System.out.println("没有实现!");
  116. }
  117. public void mouseDownAt(By by,String coordString) {
  118. System.out.println("没有实现!");
  119. }
  120. public void mouseDownRightAt(By by,String coordString) {
  121. System.out.println("没有实现!");
  122. }
  123. public void mouseUp(By by) {
  124. System.out.println("没有实现!");
  125. }
  126. public void mouseUpRight(By by) {
  127. System.out.println("没有实现!");
  128. }
  129. public void mouseUpAt(By by,String coordString) {
  130. System.out.println("没有实现!");
  131. }
  132. public void mouseUpRightAt(By by,String coordString) {
  133. System.out.println("没有实现!");
  134. }
  135. public void mouseMove(By by) {
  136. new Actions(driver).moveToElement(driver.findElement(by)).perform();
  137. }
  138. public void mouseMoveAt(By by,String coordString) {
  139. int index = coordString.trim().indexOf(',');
  140. int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  141. int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  142. new Actions(driver).moveToElement(driver.findElement(by),xOffset,yOffset).perform();
  143. }
  144. public void type(By by, String testdata) {
  145. driver.findElement(by).clear();
  146. driver.findElement(by).sendKeys(testdata);
  147. }
  148. public void typeKeys(By by, Keys key) {
  149. driver.findElement(by).sendKeys(key);
  150. }
  151. public void setSpeed(String value) {
  152. System.out.println("The methods to set the execution speed in WebDriver were deprecated");
  153. }
  154. public String getSpeed() {
  155. System.out.println("The methods to set the execution speed in WebDriver were deprecated");
  156. return null;
  157. }
  158. public void check(By by) {
  159. if(!isChecked(by))
  160. click(by);
  161. }
  162. public void uncheck(By by) {
  163. if(isChecked(by))
  164. click(by);
  165. }
  166. public void select(By by,String optionValue) {
  167. new Select(driver.findElement(by)).selectByValue(optionValue);
  168. }
  169. public void select(By by,int index) {
  170. new Select(driver.findElement(by)).selectByIndex(index);
  171. }
  172. public void addSelection(By by,String optionValue) {
  173. select(by,optionValue);
  174. }
  175. public void addSelection(By by,int index) {
  176. select(by,index);
  177. }
  178. public void removeSelection(By by,String value) {
  179. new Select(driver.findElement(by)).deselectByValue(value);
  180. }
  181. public void removeSelection(By by,int index) {
  182. new Select(driver.findElement(by)).deselectByIndex(index);
  183. }
  184. public void removeAllSelections(By by) {
  185. new Select(driver.findElement(by)).deselectAll();
  186. }
  187. public void submit(By by) {
  188. driver.findElement(by).submit();
  189. }
  190. public void open(String url) {
  191. driver.get(url);
  192. }
  193. public void openWindow(String url,String handler) {
  194. System.out.println("方法没有实现!");
  195. }
  196. public void selectWindow(String handler) {
  197. driver.switchTo().window(handler);
  198. }
  199. public String getCurrentHandler(){
  200. String currentHandler = driver.getWindowHandle();
  201. return currentHandler;
  202. }
  203. public String getSecondWindowHandler(){
  204. Set<String> handlers = driver.getWindowHandles();
  205. String reHandler = getCurrentHandler();
  206. for(String handler : handlers){
  207. if(reHandler.equals(handler))  continue;
  208. reHandler = handler;
  209. }
  210. return reHandler;
  211. }
  212. public void selectPopUp(String handler) {
  213. driver.switchTo().window(handler);
  214. }
  215. public void selectPopUp() {
  216. driver.switchTo().window(getSecondWindowHandler());
  217. }
  218. public void deselectPopUp() {
  219. driver.switchTo().window(getCurrentHandler());
  220. }
  221. public void selectFrame(int index) {
  222. driver.switchTo().frame(index);
  223. }
  224. public void selectFrame(String str) {
  225. driver.switchTo().frame(str);
  226. }
  227. public void selectFrame(By by) {
  228. driver.switchTo().frame(driver.findElement(by));
  229. }
  230. public void waitForPopUp(String windowID,String timeout) {
  231. System.out.println("没有实现");
  232. }
  233. public void accept(){
  234. driver.switchTo().alert().accept();
  235. }
  236. public void dismiss(){
  237. driver.switchTo().alert().dismiss();
  238. }
  239. public void chooseCancelOnNextConfirmation() {
  240. driver.switchTo().alert().dismiss();
  241. }
  242. public void chooseOkOnNextConfirmation() {
  243. driver.switchTo().alert().accept();
  244. }
  245. public void answerOnNextPrompt(String answer) {
  246. driver.switchTo().alert().sendKeys(answer);
  247. }
  248. public void goBack() {
  249. driver.navigate().back();
  250. }
  251. public void refresh() {
  252. driver.navigate().refresh();
  253. }
  254. public void forward() {
  255. driver.navigate().forward();
  256. }
  257. public void to(String urlStr){
  258. driver.navigate().to(urlStr);
  259. }
  260. public void close() {
  261. driver.close();
  262. }
  263. public boolean isAlertPresent() {
  264. Boolean b = true;
  265. try{
  266. driver.switchTo().alert();
  267. }catch(Exception e){
  268. b = false;
  269. }
  270. return b;
  271. }
  272. public boolean isPromptPresent() {
  273. return isAlertPresent();
  274. }
  275. public boolean isConfirmationPresent() {
  276. return isAlertPresent();
  277. }
  278. public String getAlert() {
  279. return driver.switchTo().alert().getText();
  280. }
  281. public String getConfirmation() {
  282. return getAlert();
  283. }
  284. public String getPrompt() {
  285. return getAlert();
  286. }
  287. public String getLocation() {
  288. return driver.getCurrentUrl();
  289. }
  290. public String getTitle(){
  291. return driver.getTitle();
  292. }
  293. public String getBodyText() {
  294. String str = "";
  295. List<WebElement> elements = driver.findElements(By.xpath("//body//*[contains(text(),*)]"));
  296. for(WebElement e : elements){
  297. str += e.getText()+" ";
  298. }
  299. return str;
  300. }
  301. public String getValue(By by) {
  302. return driver.findElement(by).getAttribute("value");
  303. }
  304. public String getText(By by) {
  305. return driver.findElement(by).getText();
  306. }
  307. public void highlight(By by) {
  308. WebElement element = driver.findElement(by);
  309. ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);
  310. }
  311. public Object getEval(String script,Object... args) {
  312. return ((JavascriptExecutor)driver).executeScript(script,args);
  313. }
  314. public Object getAsyncEval(String script,Object... args){
  315. return  ((JavascriptExecutor)driver).executeAsyncScript(script, args);
  316. }
  317. public boolean isChecked(By by) {
  318. return driver.findElement(by).isSelected();
  319. }
  320. public String getTable(By by,String tableCellAddress) {
  321. WebElement table = driver.findElement(by);
  322. int index = tableCellAddress.trim().indexOf('.');
  323. int row =  Integer.parseInt(tableCellAddress.substring(0, index));
  324. int cell = Integer.parseInt(tableCellAddress.substring(index+1));
  325. List<WebElement> rows = table.findElements(By.tagName("tr"));
  326. WebElement theRow = rows.get(row);
  327. String text = getCell(theRow, cell);
  328. return text;
  329. }
  330. private String getCell(WebElement Row,int cell){
  331. List<WebElement> cells;
  332. String text = 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. return text;
  342. }
  343. public String[] getSelectedLabels(By by) {
  344. Set<String> set = new HashSet<String>();
  345. List<WebElement> selectedOptions = new Select(driver.findElement(by))
  346. .getAllSelectedOptions();
  347. for(WebElement e : selectedOptions){
  348. set.add(e.getText());
  349. }
  350. return set.toArray(new String[set.size()]);
  351. }
  352. public String getSelectedLabel(By by) {
  353. return getSelectedOption(by).getText();
  354. }
  355. public String[] getSelectedValues(By by) {
  356. Set<String> set = new HashSet<String>();
  357. List<WebElement> selectedOptions = new Select(driver.findElement(by))
  358. .getAllSelectedOptions();
  359. for(WebElement e : selectedOptions){
  360. set.add(e.getAttribute("value"));
  361. }
  362. return set.toArray(new String[set.size()]);
  363. }
  364. public String getSelectedValue(By by) {
  365. return getSelectedOption(by).getAttribute("value");
  366. }
  367. public String[] getSelectedIndexes(By by) {
  368. Set<String> set = new HashSet<String>();
  369. List<WebElement> selectedOptions = new Select(driver.findElement(by))
  370. .getAllSelectedOptions();
  371. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  372. for(WebElement e : selectedOptions){
  373. set.add(String.valueOf(options.indexOf(e)));
  374. }
  375. return set.toArray(new String[set.size()]);
  376. }
  377. public String getSelectedIndex(By by) {
  378. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  379. return String.valueOf(options.indexOf(getSelectedOption(by)));
  380. }
  381. public String[] getSelectedIds(By by) {
  382. Set<String> ids = new HashSet<String>();
  383. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  384. for(WebElement option : options){
  385. if(option.isSelected()) {
  386. ids.add(option.getAttribute("id")) ;
  387. }
  388. }
  389. return ids.toArray(new String[ids.size()]);
  390. }
  391. public String getSelectedId(By by) {
  392. return getSelectedOption(by).getAttribute("id");
  393. }
  394. private WebElement getSelectedOption(By by){
  395. WebElement selectedOption = null;
  396. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  397. for(WebElement option : options){
  398. if(option.isSelected()) {
  399. selectedOption = option;
  400. }
  401. }
  402. return selectedOption;
  403. }
  404. public boolean isSomethingSelected(By by) {
  405. boolean b = false;
  406. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  407. for(WebElement option : options){
  408. if(option.isSelected()) {
  409. b = true ;
  410. break;
  411. }
  412. }
  413. return b;
  414. }
  415. public String[] getSelectOptions(By by) {
  416. Set<String> set = new HashSet<String>();
  417. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  418. for(WebElement e : options){
  419. set.add(e.getText());
  420. }
  421. return set.toArray(new String[set.size()]);
  422. }
  423. public String getAttribute(By by,String attributeLocator) {
  424. return driver.findElement(by).getAttribute(attributeLocator);
  425. }
  426. public boolean isTextPresent(String pattern) {
  427. String Xpath= "//*[contains(text(),\'"+pattern+"\')]" ;
  428. try {
  429. driver.findElement(By.xpath(Xpath));
  430. return true;
  431. } catch (NoSuchElementException e) {
  432. return false;
  433. }
  434. }
  435. public boolean isElementPresent(By by) {
  436. return driver.findElements(by).size() > 0;
  437. }
  438. public boolean isVisible(By by) {
  439. return driver.findElement(by).isDisplayed();
  440. }
  441. public boolean isEditable(By by) {
  442. return driver.findElement(by).isEnabled();
  443. }
  444. public List<WebElement> getAllButtons() {
  445. return driver.findElements(By.xpath("//input[@type='button']"));
  446. }
  447. public List<WebElement> getAllLinks() {
  448. return driver.findElements(By.tagName("a"));
  449. }
  450. public List<WebElement> getAllFields() {
  451. return driver.findElements(By.xpath("//input[@type='text']"));
  452. }
  453. public String[] getAttributeFromAllWindows(String attributeName) {
  454. System.out.println("不知道怎么实现");
  455. return null;
  456. }
  457. public void dragdrop(By by,String movementsString) {
  458. dragAndDrop(by, movementsString);
  459. }
  460. public void dragAndDrop(By by,String movementsString) {
  461. int index = movementsString.trim().indexOf('.');
  462. int xOffset = Integer.parseInt(movementsString.substring(0, index));
  463. int yOffset = Integer.parseInt(movementsString.substring(index+1));
  464. new Actions(driver).clickAndHold(driver.findElement(by)).moveByOffset(xOffset, yOffset).perform();
  465. }
  466. public void setMouseSpeed(String pixels) {
  467. System.out.println("不支持");
  468. }
  469. public Number getMouseSpeed() {
  470. System.out.println("不支持");
  471. return null;
  472. }
  473. public void dragAndDropToObject(By source,By target) {
  474. new Actions(driver).dragAndDrop(driver.findElement(source), driver.findElement(target)).perform();
  475. }
  476. public void windowFocus() {
  477. driver.switchTo().defaultContent();
  478. }
  479. public void windowMaximize() {
  480. driver.manage().window().setPosition(new Point(0,0));
  481. java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  482. Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
  483. driver.manage().window().setSize(dim);
  484. }
  485. public String[] getAllWindowIds() {
  486. System.out.println("不能实现!");
  487. return null;
  488. }
  489. public String[] getAllWindowNames() {
  490. System.out.println("不能实现!");
  491. return null;
  492. }
  493. public String[] getAllWindowTitles() {
  494. Set<String> handles = driver.getWindowHandles();
  495. Set<String> titles = new HashSet<String>();
  496. for(String handle : handles){
  497. titles.add(driver.switchTo().window(handle).getTitle());
  498. }
  499. return titles.toArray(new String[titles.size()]);
  500. }
  501. public String getHtmlSource() {
  502. return driver.getPageSource();
  503. }
  504. public void setCursorPosition(String locator,String position) {
  505. System.out.println("没能实现!");
  506. }
  507. public Number getElementIndex(String locator) {
  508. System.out.println("没能实现!");
  509. return null;
  510. }
  511. public Object isOrdered(By by1,By by2) {
  512. System.out.println("没能实现!");
  513. return null;
  514. }
  515. public Number getElementPositionLeft(By by) {
  516. return driver.findElement(by).getLocation().getX();
  517. }
  518. public Number getElementPositionTop(By by) {
  519. return driver.findElement(by).getLocation().getY();
  520. }
  521. public Number getElementWidth(By by) {
  522. return driver.findElement(by).getSize().getWidth();
  523. }
  524. public Number getElementHeight(By by) {
  525. return driver.findElement(by).getSize().getHeight();
  526. }
  527. public Number getCursorPosition(String locator) {
  528. System.out.println("没能实现!");
  529. return null;
  530. }
  531. public String getExpression(String expression) {
  532. System.out.println("没能实现!");
  533. return null;
  534. }
  535. public Number getXpathCount(By xpath) {
  536. return driver.findElements(xpath).size();
  537. }
  538. public void assignId(By by,String identifier) {
  539. System.out.println("不想实现!");
  540. }
  541. /*public void allowNativeXpath(String allow) {
  542. commandProcessor.doCommand("allowNativeXpath", new String[] {allow,});
  543. }*/
  544. /*public void ignoreAttributesWithoutValue(String ignore) {
  545. commandProcessor.doCommand("ignoreAttributesWithoutValue", new String[] {ignore,});
  546. }*/
  547. public void waitForCondition(String script,String timeout,Object... args) {
  548. Boolean b = false;
  549. int time = 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 (InterruptedException e) {
  556. // TODO Auto-generated catch block
  557. e.printStackTrace();
  558. }
  559. time += 1000;
  560. }
  561. }
  562. public void setTimeout(String timeout) {
  563. driver.manage().timeouts().implicitlyWait(Integer.parseInt(timeout), TimeUnit.SECONDS);
  564. }
  565. public void waitForPageToLoad(String timeout) {
  566. driver.manage().timeouts().pageLoadTimeout(Integer.parseInt(timeout), TimeUnit.SECONDS);
  567. }
  568. public void waitForFrameToLoad(String frameAddress,String timeout) {
  569. /*driver.switchTo().frame(frameAddress)
  570. .manage()
  571. .timeouts()
  572. .pageLoadTimeout(Integer.parseInt(timeout), TimeUnit.SECONDS);*/
  573. }
  574. public String getCookie() {
  575. String cookies = "";
  576. Set<Cookie> cookiesSet = driver.manage().getCookies();
  577. for(Cookie c : cookiesSet){
  578. cookies += c.getName()+"="+c.getValue()+";";
  579. }
  580. return cookies;
  581. }
  582. public String getCookieByName(String name) {
  583. return driver.manage().getCookieNamed(name).getValue();
  584. }
  585. public boolean isCookiePresent(String name) {
  586. boolean b = false ;
  587. if(driver.manage().getCookieNamed(name) != null || driver.manage().getCookieNamed(name).equals(null))
  588. b = true;
  589. return b;
  590. }
  591. public void createCookie(Cookie c) {
  592. driver.manage().addCookie(c);
  593. }
  594. public void deleteCookie(Cookie c) {
  595. driver.manage().deleteCookie(c);
  596. }
  597. public void deleteAllVisibleCookies() {
  598. driver.manage().getCookieNamed("fs").isSecure();
  599. }
  600. /*public void setBrowserLogLevel(String logLevel) {
  601. }*/
  602. /*public void runScript(String script) {
  603. commandProcessor.doCommand("runScript", new String[] {script,});
  604. }*/
  605. /*public void addLocationStrategy(String strategyName,String functionDefinition) {
  606. commandProcessor.doCommand("addLocationStrategy", new String[] {strategyName,functionDefinition,});
  607. }*/
  608. public void captureEntirePageScreenshot(String filename) {
  609. File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  610. try {
  611. FileUtils.copyFile(screenShotFile, new File(filename));
  612. } catch (IOException e) {
  613. // TODO Auto-generated catch block
  614. e.printStackTrace();
  615. }
  616. }
  617. /*public void rollup(String rollupName,String kwargs) {
  618. commandProcessor.doCommand("rollup", new String[] {rollupName,kwargs,});
  619. }
  620. public void addScript(String scriptContent,String scriptTagId) {
  621. commandProcessor.doCommand("addScript", new String[] {scriptContent,scriptTagId,});
  622. }
  623. public void removeScript(String scriptTagId) {
  624. commandProcessor.doCommand("removeScript", new String[] {scriptTagId,});
  625. }
  626. public void useXpathLibrary(String libraryName) {
  627. commandProcessor.doCommand("useXpathLibrary", new String[] {libraryName,});
  628. }
  629. public void setContext(String context) {
  630. commandProcessor.doCommand("setContext", new String[] {context,});
  631. }*/
  632. /*public void attachFile(String fieldLocator,String fileLocator) {
  633. commandProcessor.doCommand("attachFile", new String[] {fieldLocator,fileLocator,});
  634. }*/
  635. /*public void captureScreenshot(String filename) {
  636. commandProcessor.doCommand("captureScreenshot", new String[] {filename,});
  637. }*/
  638. public String captureScreenshotToString() {
  639. String screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
  640. return screen;
  641. }
  642. /* public String captureNetworkTraffic(String type) {
  643. return commandProcessor.getString("captureNetworkTraffic", new String[] {type});
  644. }
  645. */
  646. /*public void addCustomRequestHeader(String key, String value) {
  647. commandProcessor.getString("addCustomRequestHeader", new String[] {key, value});
  648. }*/
  649. /*public String captureEntirePageScreenshotToString(String kwargs) {
  650. return commandProcessor.getString("captureEntirePageScreenshotToString", new String[] {kwargs,});
  651. }*/
  652. public void shutDown() {
  653. driver.quit();
  654. }
  655. /*public String retrieveLastRemoteControlLogs() {
  656. return commandProcessor.getString("retrieveLastRemoteControlLogs", new String[] {});
  657. }*/
  658. public void keyDownNative(Keys keycode) {
  659. new Actions(driver).keyDown(keycode).perform();
  660. }
  661. public void keyUpNative(Keys keycode) {
  662. new Actions(driver).keyUp(keycode).perform();
  663. }
  664. public void keyPressNative(String keycode) {
  665. new Actions(driver).click().perform();
  666. }
  667. public void waitForElementPresent(By by) {
  668. for(int i=0; i<60; i++) {
  669. if (isElementPresent(by)) {
  670. break;
  671. } else {
  672. try {
  673. driver.wait(1000);
  674. } catch (InterruptedException e) {
  675. e.printStackTrace();
  676. }
  677. }
  678. }
  679. }
  680. public void clickAndWaitForElementPresent(By by, By waitElement) {
  681. click(by);
  682. waitForElementPresent(waitElement);
  683. }
  684. public Boolean VeryTitle(String exception,String actual){
  685. if(exception.equals(actual)) return true;
  686. else return false;
  687. }
  688. }

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

Selenium_用selenium webdriver实现selenium RC中的类似的方法的更多相关文章

  1. selenium.webdriver.common.keys 模块中常用的变量

    表11-5 selenium.webdriver.common.keys 模块中常用的变量属性 含义Keys.DOWN, Keys.UP, Keys.LEFT,Keys.RIGHT 键盘箭头键Keys ...

  2. Selenium WebDriver VS Selenium RC

      WebDriver到底是什么? WebDriver是一个Web的自动化测试框架,它支持你执行你的测试用例在不同的浏览器上面,并不像Selenium一样只支持Firefox.     WebDriv ...

  3. selenium自动化测试在富文本中输入信息的方法

    第一次用selenium+python编写自动测试脚本,因为页面中插入了富文本编辑,开始怎么都无法输入进去,度娘好多方法都无效,分享踩坑的经历一是为了记录一下自己的成长,二是为了给同样摸索seleni ...

  4. webdriver下拉框中选择option的方法提醒

    select这个标签比较特殊 下面的option不能用点击下拉框,再点击选中这种方法 前端代码: <html> <body> <select id="Shipp ...

  5. Selenium Webdriver概述(转)

    Selenium Webdriver https://www.yiibai.com/selenium/selenium_overview.html# webdriver自动化俗称Selenium 2. ...

  6. Selenium webdriver 学习总结-元素定位

    Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...

  7. Selenium Tutorial (1) - Starting with Selenium WebDriver

    Starting with Selenium WebDriver Selenium WebDriver - Introduction & Features How Selenium WebDr ...

  8. Selenium WebDriver原理(一):Selenium WebDriver 是怎么工作的?

    首先我们来看一个经典的例子: 搭出租车 在出租车驾驶中,通常有3个角色: 乘客 : 他告诉出租车司机他想去哪里以及如何到达那里 对出租车司机说: 1.去阳光棕榈园东门 2.从这里转左 3.然后直行 2 ...

  9. 【零基础】Selenium:Webdriver图文入门教程java篇(附相关包下载)

    一.selenium2.0简述 与一般的浏览器测试框架(爬虫框架)不同,Selenium2.0实际上由两个部分组成Selenium+webdriver,Selenium负责用户指令的解释(code), ...

随机推荐

  1. AngularJS 之 Factory、Service、Provider

    当你初试 Angular 时,很自然地就会往 controller 和 scope 里堆满不必要的逻辑.一定要早点意识到,controller 这一层应该很薄:也就是说,应用里大部分的业务逻辑和持久化 ...

  2. c++ 的 坑真多之头文件

    我发现类在做参数时,是可以不引用头文件,即不用#include"xxx.h"的,比如下面这样是没有问题的 #pragma once #include <string> ...

  3. jquery中ajax的简单使用

    一.load() 这是最简单的一个函数,传入一个url他会异步加载该url的内容,然后将内容插入每一个选中的元素中,替换掉其中已经存在的内容. 所以最简单的用法是: $("#myDiv&qu ...

  4. MySQL5.7更改密码时出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'

    转自:http://blog.csdn.net/u010603691/article/details/50379282 新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过 ...

  5. 第二十一篇:SOUI中的控件注册机制

    Win32编程中,用户需要一个新控件时,需要向系统注册一个新的控件类型.注册以后,调用::CreateWindow时才能根据标识控件类型的字符串创建出一个新的控件窗口对象. 为了能够从XML描述的字符 ...

  6. BuildFilePath 及打开文件对话框

    也许以后就主要在这里发SOUI的介绍了. 贴一段文件相关的helper, 测试一下贴代码是不是方便. /** * Copyright (C) 2014-2050 * All rights reserv ...

  7. hdu 5833 Zhu and 772002 高斯消元

    Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...

  8. Linux学习笔记(19) Linux服务管理

    1. 服务的分类 Linux服务可分为RPM包默认安装的服务和源码包安装的服务.前者可细分为独立的服务(直接作用于内存中)和基于xinetd服务.xinetd本身是独立的服务,其唯一的功能是管理其他服 ...

  9. sql 根据指定条件获取一个字段批量获取数据插入另外一张表字段中+MD5加密

    /****** Object: StoredProcedure [dbo].[getSplitValue] Script Date: 03/13/2014 13:58:12 ******/ SET A ...

  10. SQL..如何用命令删除数据库中所有的表?

    要删除所有的用户表: declare @sql varchar(8000) SELECT @sql='drop table ' + name FROM sysobjects WHERE (type = ...