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

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. 出现AttributeError: module ‘selenium.webdriver‘ has no attribute ‘PhantomJS异常的解决方法

    python3.6 安装最新版的Selenium 调用 PhantomJS总是报错 交互窗口中完全不能用 把PhantomJS.exe放到Python根目录后运行命令行程序总是有一行 这时需要下载旧版 ...

  2. Selenium Webdriver原理终于搞清楚了

    目录 1. Selenium的历史 2. WebDriver协议 3. Selenium驱动浏览器原理 4. 测试代码与Webdriver的交互 5. Webdriver与浏览器的关系 6. Sele ...

  3. Selenium Webdriver概述(转)

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

  4. Selenium WebDriver架构

    什么是Selenium? (What is Selenium?) Selenium is an Opensource Automation testing tool which is only mea ...

  5. 开源应用架构之​Selenium WebDriver(上)

    前不久,InfoQ向大家推荐了几本有关软件架构的新书,引起了国内读者的广泛兴趣.​其中一本是<开源应用架构(The Architecture of Open Source Application ...

  6. 开源应用架构之​Selenium WebDriver

    这几篇文章有些看不懂,不过先存了,再细细品. (上) http://www.infoq.com/cn/news/2011/06/selenium-arch 前不久,InfoQ向大家推荐了几本有关软件架 ...

  7. Selenium WebDriver简介

    Selenium WebDriver简介 Selenium WebDriver简介 是Selenium工具箱中功能最强大且最受欢迎的工具之一.WebDriver是Selenium RC的扩展版本,具有 ...

  8. c 后台代码调用ajax,.NET Selenium WebDriver操作调用浏览器后台执行Js(JavaScript)代码...

    1.Selenium WebDriver安装引用 注意:要用使用的浏览器肯定要装,并且Selenium.Chrome.WebDriver版本要和浏览器版一致. 如果要操作其它浏览器,则安装对应其它浏览 ...

  9. 使用Selenium WebDriver测试自动化的22条实用技巧

    使用Selenium进行测试自动化已使全球的网站测试人员能够轻松执行自动化的网站测试. Webdriver是Selenium框架的核心组件,通过它您可以针对不同类型的浏览器(例如Google Chro ...

最新文章

  1. SAP MM 外部采购流程里的如同鸡肋一样的Advanced Returns Management功能
  2. c++服务器开发学习--01--c++基础,socket
  3. DNA repair - HDU 2457(自动机+dp)
  4. redis简单运用,数据类型,适合入门
  5. 计算机Excel电子表格处理文件,2018计算机应用基础-Excel电子表格题目
  6. 浙江新华书店的数字化新尝试
  7. workbench应力应变曲线_ANSYS WORKBENCH后处理中各种应力结果的详细说明
  8. codeforces 707c
  9. 登录网易云显示服务器地址,[网易云音乐]登录流程还原
  10. 屁孩君儿子讲解 2022 【例4.7】最小n值
  11. 基于LSTM-Attention模型的光伏电站发电量预估(1)
  12. 《工程伦理与学术道德》第二章习题
  13. 体育直播android,500体育直播
  14. 王东岳《东西方文化溯源与东西方哲学》
  15. 什么是共享办公室,你想知道的都在这
  16. UserControl关闭事件
  17. linux 蓝牙设备,Ubuntu8.04下蓝牙设备连接管理
  18. VALSE 2020线上大会学生论坛【VALSE Student Seminar】Panel实录
  19. python计算正方体和长方体_定义一个接口,计算正方体和长方体的体积,并写一个测试类进行测试...
  20. 最常用计算机机箱,好看又实用 给你的电脑选一个好机箱

热门文章

  1. flutter弹起键盘页面布局超限问题
  2. 神策数据张涛:微信生态数字化运营解决方案
  3. 企业级java springboot b2bc商城系统开源源码二次开发-云架构代码结构构建(五)...
  4. 干货!不得不知的UI界面中“行为召唤按钮”设计秘诀
  5. 【记录】vmware fusion 7 windows 10 unidentified network
  6. dubbo使用遇到的问题
  7. 《架构师(“拥抱2015”特刊)》发布
  8. Java知多少(15)字符串
  9. Java虚拟机——类加载机制
  10. 浏览器的内核及版本的判断