51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1212|回复: 4
打印 上一主题 下一主题

[转贴] selenium webdriver学习 16 – 用selenium webdriver实现selenium RC中的类似的方法

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2017-7-18 14:24:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近想总结一下学习selenium webdriver的情况,于是就想用selenium webdriver里面的方法来实现selenium RC中操作的一些方法。目前封装了一个ActionDriverHelper类,来实现RC中Selenium.java和 DefaultSelenium.java中的方法。有一些方法还没有实现,写的方法大多没有经过测试,仅供参考。代码如下:
CODE:
  1. <font size="3">package core;
  2.   
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8. import java.util.concurrent.TimeUnit;
  9.   
  10. import org.apache.commons.io.FileUtils;
  11. import org.openqa.selenium.By;
  12. import org.openqa.selenium.Cookie;
  13. import org.openqa.selenium.Dimension;
  14. import org.openqa.selenium.JavascriptExecutor;
  15. import org.openqa.selenium.Keys;
  16. import org.openqa.selenium.NoSuchElementException;
  17. import org.openqa.selenium.OutputType;
  18. import org.openqa.selenium.Point;
  19. import org.openqa.selenium.TakesScreenshot;
  20. import org.openqa.selenium.WebDriver;
  21. import org.openqa.selenium.WebElement;
  22. import org.openqa.selenium.WebDriver.Timeouts;
  23. import org.openqa.selenium.interactions.Actions;
  24. import org.openqa.selenium.support.ui.Select;
  25.   
  26. public class ActionDriverHelper {
  27.         protected WebDriver driver;
  28.         public ActionDriverHelper(WebDriver driver){
  29.             this.driver = driver ;
  30.         }
  31.          
  32.          
  33.         publicvoid click(By by) {
  34.             driver.findElement(by).click();
  35.         }
  36.          
  37.         publicvoid doubleClick(By by){
  38.             new Actions(driver).doubleClick(driver.findElement(by)).perform();
  39.         }
  40.          
  41.         publicvoid contextMenu(By by) {
  42.             new Actions(driver).contextClick(driver.findElement(by)).perform();
  43.         }
  44.          
  45.         publicvoid clickAt(By by,String coordString) {
  46.             int index = coordString.trim().indexOf(',');
  47.             int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  48.             int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  49.             new Actions(driver).moveToElement(driver.findElement(by), xOffset, yOffset).click().perform();
  50.         }
  51.          
  52.         publicvoid doubleClickAt(By by,String coordString){
  53.             int index = coordString.trim().indexOf(',');
  54.             int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  55.             int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  56.             new Actions(driver).moveToElement(driver.findElement(by), xOffset, yOffset)
  57.                                 .doubleClick(driver.findElement(by))
  58.                                 .perform();
  59.         }
  60.          
  61.         publicvoid contextMenuAt(By by,String coordString) {
  62.             int index = coordString.trim().indexOf(',');
  63.             int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  64.             int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  65.             new Actions(driver).moveToElement(driver.findElement(by), xOffset, yOffset)
  66.                                 .contextClick(driver.findElement(by))
  67.                                 .perform();
  68.         }
  69.          
  70.         publicvoid fireEvent(By by,String eventName) {
  71.             System.out.println("webdriver 不建议使用这样的方法,所以没有实现。");
  72.         }
  73.          
  74.         publicvoid focus(By by) {
  75.             System.out.println("webdriver 不建议使用这样的方法,所以没有实现。");
  76.         }
  77.          
  78.         publicvoid keyPress(By by,Keys theKey) {
  79.             new Actions(driver).keyDown(driver.findElement(by), theKey).release().perform();         
  80.         }
  81.          
  82.         publicvoid shiftKeyDown() {
  83.             new Actions(driver).keyDown(Keys.SHIFT).perform();
  84.         }
  85.          
  86.         publicvoid shiftKeyUp() {
  87.             new Actions(driver).keyUp(Keys.SHIFT).perform();
  88.         }
  89.          
  90.         publicvoid metaKeyDown() {
  91.             new Actions(driver).keyDown(Keys.META).perform();
  92.         }
  93.   
  94.         publicvoid metaKeyUp() {
  95.             new Actions(driver).keyUp(Keys.META).perform();
  96.         }
  97.   
  98.         publicvoid altKeyDown() {
  99.             new Actions(driver).keyDown(Keys.ALT).perform();
  100.         }
  101.   
  102.         publicvoid altKeyUp() {
  103.             new Actions(driver).keyUp(Keys.ALT).perform();
  104.         }
  105.   
  106.         publicvoid controlKeyDown() {
  107.             new Actions(driver).keyDown(Keys.CONTROL).perform();
  108.         }
  109.   
  110.         publicvoid controlKeyUp() {
  111.             new Actions(driver).keyUp(Keys.CONTROL).perform();
  112.         }
  113.          
  114.         publicvoid KeyDown(Keys theKey) {
  115.             new Actions(driver).keyDown(theKey).perform();
  116.         }
  117.         publicvoid KeyDown(By by,Keys theKey){
  118.             new Actions(driver).keyDown(driver.findElement(by), theKey).perform();
  119.         }
  120.          
  121.         publicvoid KeyUp(Keys theKey){
  122.             new Actions(driver).keyUp(theKey).perform();
  123.         }
  124.          
  125.         publicvoid KeyUp(By by,Keys theKey){
  126.             new Actions(driver).keyUp(driver.findElement(by), theKey).perform();
  127.         }
  128.          
  129.         publicvoid mouseOver(By by) {
  130.             new Actions(driver).moveToElement(driver.findElement(by)).perform();
  131.         }
  132.          
  133.         publicvoid mouseOut(By by) {
  134.             System.out.println("没有实现!");
  135.             //new Actions(driver).moveToElement((driver.findElement(by)), -10, -10).perform();
  136.         }
  137.          
  138.         publicvoid mouseDown(By by) {
  139.             new Actions(driver).clickAndHold(driver.findElement(by)).perform();
  140.         }
  141.          
  142.         publicvoid mouseDownRight(By by) {
  143.             System.out.println("没有实现!");
  144.         }
  145.          
  146.         publicvoid mouseDownAt(By by,String coordString) {
  147.             System.out.println("没有实现!");
  148.         }
  149.   
  150.         publicvoid mouseDownRightAt(By by,String coordString) {
  151.             System.out.println("没有实现!");
  152.         }
  153.   
  154.         publicvoid mouseUp(By by) {
  155.             System.out.println("没有实现!");
  156.         }
  157.   
  158.         publicvoid mouseUpRight(By by) {
  159.             System.out.println("没有实现!");
  160.         }
  161.   
  162.         publicvoid mouseUpAt(By by,String coordString) {
  163.             System.out.println("没有实现!");
  164.         }
  165.   
  166.         publicvoid mouseUpRightAt(By by,String coordString) {
  167.             System.out.println("没有实现!");
  168.         }
  169.   
  170.         publicvoid mouseMove(By by) {
  171.             new Actions(driver).moveToElement(driver.findElement(by)).perform();
  172.         }
  173.   
  174.         publicvoid mouseMoveAt(By by,String coordString) {
  175.             int index = coordString.trim().indexOf(',');
  176.             int xOffset = Integer.parseInt(coordString.trim().substring(0, index));
  177.             int yOffset = Integer.parseInt(coordString.trim().substring(index+1));
  178.             new Actions(driver).moveToElement(driver.findElement(by),xOffset,yOffset).perform();
  179.         }
  180.         publicvoid type(By by, String testdata) {
  181.             driver.findElement(by).clear();  
  182.             driver.findElement(by).sendKeys(testdata);
  183.         }
  184.   
  185.         publicvoid typeKeys(By by, Keys key) {
  186.             driver.findElement(by).sendKeys(key);
  187.         }
  188.         publicvoid setSpeed(String value) {
  189.              System.out.println("The methods to set the execution speed in WebDriver were deprecated");
  190.         }
  191.   
  192.         public String getSpeed() {
  193.             System.out.println("The methods to set the execution speed in WebDriver were deprecated");
  194.             returnnull;
  195.               
  196.         }
  197.         publicvoid check(By by) {
  198.             if(!isChecked(by))
  199.                 click(by);      
  200.         }
  201.   
  202.         publicvoid uncheck(By by) {
  203.             if(isChecked(by))  
  204.                 click(by);      
  205.         }
  206.          
  207.         publicvoid select(By by,String optionValue) {
  208.             new Select(driver.findElement(by)).selectByValue(optionValue);
  209.         }
  210.          
  211.         publicvoid select(By by,int index) {
  212.             new Select(driver.findElement(by)).selectByIndex(index);
  213.         }
  214.   
  215.         publicvoid addSelection(By by,String optionValue) {
  216.             select(by,optionValue);
  217.         }
  218.         publicvoid addSelection(By by,int index) {
  219.             select(by,index);
  220.         }
  221.          
  222.         publicvoid removeSelection(By by,String value) {
  223.             new Select(driver.findElement(by)).deselectByValue(value);
  224.         }
  225.          
  226.         publicvoid removeSelection(By by,int index) {
  227.             new Select(driver.findElement(by)).deselectByIndex(index);
  228.         }
  229.   
  230.         publicvoid removeAllSelections(By by) {
  231.             new Select(driver.findElement(by)).deselectAll();
  232.         }
  233.          
  234.         publicvoid submit(By by) {
  235.             driver.findElement(by).submit();
  236.         }
  237.          
  238.         publicvoid open(String url) {
  239.             driver.get(url);
  240.         }
  241.          
  242.         publicvoid openWindow(String url,String handler) {
  243.             System.out.println("方法没有实现!");
  244.         }
  245.   
  246.         publicvoid selectWindow(String handler) {
  247.             driver.switchTo().window(handler);
  248.         }
  249.          
  250.         public String getCurrentHandler(){
  251.             String currentHandler = driver.getWindowHandle();
  252.             return currentHandler;
  253.         }
  254.          
  255.         public String getSecondWindowHandler(){
  256.             Set<String> handlers = driver.getWindowHandles();
  257.             String reHandler = getCurrentHandler();
  258.             for(String handler : handlers){
  259.                 if(reHandler.equals(handler)) continue;
  260.                 reHandler = handler;
  261.             }
  262.             return reHandler;
  263.         }

  264. </font>
复制代码

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2017-7-18 14:26:24 | 只看该作者
  1. publicvoid selectPopUp(String handler) {
  2. driver.switchTo().window(handler);
  3. }

  4. publicvoid selectPopUp() {
  5. driver.switchTo().window(getSecondWindowHandler());
  6. }

  7. publicvoid deselectPopUp() {
  8. driver.switchTo().window(getCurrentHandler());
  9. }

  10. publicvoid selectFrame(int index) {
  11. driver.switchTo().frame(index);
  12. }

  13. publicvoid selectFrame(String str) {
  14. driver.switchTo().frame(str);
  15. }

  16. publicvoid selectFrame(By by) {
  17. driver.switchTo().frame(driver.findElement(by));
  18. }
  19. publicvoid waitForPopUp(String windowID,String timeout) {
  20. System.out.println("没有实现");
  21. }
  22. publicvoid accept(){
  23. driver.switchTo().alert().accept();
  24. }
  25. publicvoid dismiss(){
  26. driver.switchTo().alert().dismiss();
  27. }
  28. publicvoid chooseCancelOnNextConfirmation() {
  29. driver.switchTo().alert().dismiss();
  30. }

  31. publicvoid chooseOkOnNextConfirmation() {
  32. driver.switchTo().alert().accept();
  33. }

  34. publicvoid answerOnNextPrompt(String answer) {
  35. driver.switchTo().alert().sendKeys(answer);
  36. }

  37. publicvoid goBack() {
  38. driver.navigate().back();
  39. }

  40. publicvoid refresh() {
  41. driver.navigate().refresh();
  42. }

  43. publicvoid forward() {
  44. driver.navigate().forward();
  45. }

  46. publicvoid to(String urlStr){
  47. driver.navigate().to(urlStr);
  48. }

  49. publicvoid close() {
  50. driver.close();
  51. }

  52. publicboolean isAlertPresent() {
  53. Boolean b = true;
  54. try{
  55. driver.switchTo().alert();
  56. }catch(Exception e){
  57. b = false;
  58. }
  59. return b;
  60. }

  61. publicboolean isPromptPresent() {
  62. return isAlertPresent();
  63. }

  64. publicboolean isConfirmationPresent() {
  65. return isAlertPresent();
  66. }

  67. public String getAlert() {
  68. return driver.switchTo().alert().getText();
  69. }

  70. public String getConfirmation() {
  71. return getAlert();
  72. }

  73. public String getPrompt() {
  74. return getAlert();
  75. }

  76. public String getLocation() {
  77. return driver.getCurrentUrl();
  78. }

  79. public String getTitle(){
  80. return driver.getTitle();
  81. }

  82. public String getBodyText() {
  83. String str = "";
  84. List<WebElement> elements = driver.findElements(By.xpath("//body//*[contains(text(),*)]"));
  85. for(WebElement e : elements){
  86. str += e.getText()+" ";
  87. }
  88. return str;
  89. }

  90. public String getValue(By by) {
  91. return driver.findElement(by).getAttribute("value");
  92. }

  93. public String getText(By by) {
  94. return driver.findElement(by).getText();
  95. }

  96. publicvoid highlight(By by) {
  97. WebElement element = driver.findElement(by);
  98. ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);
  99. }

  100. public Object getEval(String script,Object... args) {
  101. return ((JavascriptExecutor)driver).executeScript(script,args);
  102. }
  103. public Object getAsyncEval(String script,Object... args){
  104. return ((JavascriptExecutor)driver).executeAsyncScript(script, args);
  105. }
  106. publicboolean isChecked(By by) {
  107. return driver.findElement(by).isSelected();
  108. }
  109. public String getTable(By by,String tableCellAddress) {
  110. WebElement table = driver.findElement(by);
  111. int index = tableCellAddress.trim().indexOf('.');
  112. int row = Integer.parseInt(tableCellAddress.substring(0, index));
  113. int cell = Integer.parseInt(tableCellAddress.substring(index+1));
  114. List<WebElement> rows = table.findElements(By.tagName("tr"));
  115. WebElement theRow = rows.get(row);
  116. String text = getCell(theRow, cell);
  117. return text;
  118. }
  119. private String getCell(WebElement Row,int cell){
  120. List<WebElement> cells;
  121. String text = null;
  122. if(Row.findElements(By.tagName("th")).size()>0){
  123. cells = Row.findElements(By.tagName("th"));
  124. text = cells.get(cell).getText();
  125. }
  126. if(Row.findElements(By.tagName("td")).size()>0){
  127. cells = Row.findElements(By.tagName("td"));
  128. text = cells.get(cell).getText();
  129. }
  130. return text;

  131. }

  132. public String[] getSelectedLabels(By by) {
  133. Set<String> set = new HashSet<String>();
  134. List<WebElement> selectedOptions =new Select(driver.findElement(by))
  135. .getAllSelectedOptions();
  136. for(WebElement e : selectedOptions){
  137. set.add(e.getText());
  138. }
  139. return set.toArray(new String[set.size()]);
  140. }

  141. public String getSelectedLabel(By by) {
  142. return getSelectedOption(by).getText();
  143. }

  144. public String[] getSelectedValues(By by) {
  145. Set<String> set = new HashSet<String>();
  146. List<WebElement> selectedOptions =new Select(driver.findElement(by))
  147. .getAllSelectedOptions();
  148. for(WebElement e : selectedOptions){
  149. set.add(e.getAttribute("value"));
  150. }
  151. return set.toArray(new String[set.size()]);
  152. }

  153. public String getSelectedValue(By by) {
  154. return getSelectedOption(by).getAttribute("value");
  155. }

  156. public String[] getSelectedIndexes(By by) {
  157. Set<String> set = new HashSet<String>();
  158. List<WebElement> selectedOptions =new Select(driver.findElement(by))
  159. .getAllSelectedOptions();
  160. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  161. for(WebElement e : selectedOptions){
  162. set.add(String.valueOf(options.indexOf(e)));
  163. }
  164. return set.toArray(new String[set.size()]);
  165. }

  166. public String getSelectedIndex(By by) {
  167. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  168. return String.valueOf(options.indexOf(getSelectedOption(by)));
  169. }

  170. public String[] getSelectedIds(By by) {
  171. Set<String> ids = new HashSet<String>();
  172. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  173. for(WebElement option : options){
  174. if(option.isSelected()) {
  175. ids.add(option.getAttribute("id")) ;
  176. }
  177. }
  178. return ids.toArray(new String[ids.size()]);
  179. }

  180. public String getSelectedId(By by) {
  181. return getSelectedOption(by).getAttribute("id");
  182. }
  183. private WebElement getSelectedOption(By by){
  184. WebElement selectedOption = null;
  185. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  186. for(WebElement option : options){
  187. if(option.isSelected()) {
  188. selectedOption = option;
  189. }
  190. }
  191. return selectedOption;
  192. }

  193. publicboolean isSomethingSelected(By by) {
  194. boolean b =false;
  195. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  196. for(WebElement option : options){
  197. if(option.isSelected()) {
  198. b = true ;
  199. break;
  200. }
  201. }
  202. return b;
  203. }

  204. public String[] getSelectOptions(By by) {
  205. Set<String> set = new HashSet<String>();
  206. List<WebElement> options = new Select(driver.findElement(by)).getOptions();
  207. for(WebElement e : options){
  208. set.add(e.getText());
  209. }
  210. return set.toArray(new String[set.size()]);
  211. }
  212. public String getAttribute(By by,String attributeLocator) {
  213. return driver.findElement(by).getAttribute(attributeLocator);
  214. }

  215. publicboolean isTextPresent(String pattern) {
  216. String Xpath= "//*[contains(text(),\'"+pattern+"\')]" ;
  217. try {
  218. driver.findElement(By.xpath(Xpath));
  219. returntrue;
  220. } catch (NoSuchElementException e) {
  221. returnfalse;
  222. }
  223. }

  224. publicboolean isElementPresent(By by) {
  225. return driver.findElements(by).size() >0;
  226. }

  227. publicboolean isVisible(By by) {
  228. return driver.findElement(by).isDisplayed();
  229. }

  230. publicboolean isEditable(By by) {
  231. return driver.findElement(by).isEnabled();
  232. }
  233. public List<WebElement> getAllButtons() {
  234. return driver.findElements(By.xpath("//input[@type='button']"));
  235. }

  236. public List<WebElement> getAllLinks() {
  237. return driver.findElements(By.tagName("a"));
  238. }

  239. public List<WebElement> getAllFields() {
  240. return driver.findElements(By.xpath("//input[@type='text']"));
  241. }

  242. public String[] getAttributeFromAllWindows(String attributeName) {
  243. System.out.println("不知道怎么实现");
  244. returnnull;
  245. }
  246. publicvoid dragdrop(By by,String movementsString) {
  247. dragAndDrop(by, movementsString);
  248. }
  249. publicvoid dragAndDrop(By by,String movementsString) {
  250. int index = movementsString.trim().indexOf('.');
  251. int xOffset = Integer.parseInt(movementsString.substring(0, index));
  252. int yOffset = Integer.parseInt(movementsString.substring(index+1));
  253. new Actions(driver).clickAndHold(driver.findElement(by)).moveByOffset(xOffset, yOffset).perform();
  254. }
  255. publicvoid setMouseSpeed(String pixels) {
  256. System.out.println("不支持");
  257. }

  258. public Number getMouseSpeed() {
  259. System.out.println("不支持");
  260. returnnull;
  261. }
  262. publicvoid dragAndDropToObject(By source,By target) {
  263. new Actions(driver).dragAndDrop(driver.findElement(source), driver.findElement(target)).perform();
  264. }

  265. publicvoid windowFocus() {
  266. driver.switchTo().defaultContent();
  267. }

  268. publicvoid windowMaximize() {
  269. driver.manage().window().setPosition(new Point(0,0));
  270. java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  271. Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
  272. driver.manage().window().setSize(dim);
  273. }

复制代码

回复 支持 反对

使用道具 举报

该用户从未签到

3#
 楼主| 发表于 2017-7-18 14:27:10 | 只看该作者
  1. public String[] getAllWindowIds() {
  2. System.out.println("不能实现!");
  3. returnnull;
  4. }

  5. public String[] getAllWindowNames() {
  6. System.out.println("不能实现!");
  7. returnnull;
  8. }

  9. public String[] getAllWindowTitles() {
  10. Set<String> handles = driver.getWindowHandles();
  11. Set<String> titles = new HashSet<String>();
  12. for(String handle : handles){
  13. titles.add(driver.switchTo().window(handle).getTitle());
  14. }
  15. return titles.toArray(new String[titles.size()]);
  16. }
  17. public String getHtmlSource() {
  18. return driver.getPageSource();
  19. }

  20. publicvoid setCursorPosition(String locator,String position) {
  21. System.out.println("没能实现!");
  22. }

  23. public Number getElementIndex(String locator) {
  24. System.out.println("没能实现!");
  25. returnnull;
  26. }

  27. public Object isOrdered(By by1,By by2) {
  28. System.out.println("没能实现!");
  29. returnnull;
  30. }

  31. public Number getElementPositionLeft(By by) {
  32. return driver.findElement(by).getLocation().getX();
  33. }

  34. public Number getElementPositionTop(By by) {
  35. return driver.findElement(by).getLocation().getY();
  36. }

  37. public Number getElementWidth(By by) {
  38. return driver.findElement(by).getSize().getWidth();
  39. }

  40. public Number getElementHeight(By by) {
  41. return driver.findElement(by).getSize().getHeight();
  42. }

  43. public Number getCursorPosition(String locator) {
  44. System.out.println("没能实现!");
  45. returnnull;
  46. }

  47. public String getExpression(String expression) {
  48. System.out.println("没能实现!");
  49. returnnull;
  50. }

  51. public Number getXpathCount(By xpath) {
  52. return driver.findElements(xpath).size();
  53. }

  54. publicvoid assignId(By by,String identifier) {
  55. System.out.println("不想实现!");
  56. }

  57. /*publicvoid allowNativeXpath(String allow) {
  58. commandProcessor.doCommand("allowNativeXpath",new String[] {allow,});
  59. }*/

  60. /*public void ignoreAttributesWithoutValue(String ignore) {
  61. commandProcessor.doCommand("ignoreAttributesWithoutValue", new String[] {ignore,});

  62. }*/

  63. publicvoid waitForCondition(String script,String timeout,Object... args) {
  64. Boolean b = false;
  65. int time =0;
  66. while(time <= Integer.parseInt(timeout)){
  67. b = (Boolean) ((JavascriptExecutor)driver).executeScript(script,args);
  68. if(b==true)break;
  69. try {
  70. Thread.sleep(1000);
  71. } catch (InterruptedException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. time += 1000;
  76. }
  77. }

  78. publicvoid setTimeout(String timeout) {
  79. driver.manage().timeouts().implicitlyWait(Integer.parseInt(timeout), TimeUnit.SECONDS);
  80. }

  81. publicvoid waitForPageToLoad(String timeout) {
  82. driver.manage().timeouts().pageLoadTimeout(Integer.parseInt(timeout), TimeUnit.SECONDS);
  83. }

  84. publicvoid waitForFrameToLoad(String frameAddress,String timeout) {
  85. /*driver.switchTo().frame(frameAddress)
  86. .manage()
  87. .timeouts()
  88. .pageLoadTimeout(Integer.parseInt(timeout), TimeUnit.SECONDS);*/
  89. }

  90. public String getCookie() {
  91. String cookies = "";
  92. Set<Cookie> cookiesSet = driver.manage().getCookies();
  93. for(Cookie c : cookiesSet){
  94. cookies += c.getName()+"="+c.getValue()+";";
  95. }
  96. return cookies;
  97. }

  98. public String getCookieByName(String name) {
  99. return driver.manage().getCookieNamed(name).getValue();
  100. }

  101. publicboolean isCookiePresent(String name) {
  102. boolean b =false ;
  103. if(driver.manage().getCookieNamed(name) !=null || driver.manage().getCookieNamed(name).equals(null))
  104. b = true;
  105. return b;
  106. }

  107. publicvoid createCookie(Cookie c) {

  108. driver.manage().addCookie(c);
  109. }

  110. publicvoid deleteCookie(Cookie c) {
  111. driver.manage().deleteCookie(c);
  112. }

  113. publicvoid deleteAllVisibleCookies() {
  114. driver.manage().getCookieNamed("fs").isSecure();
  115. }

  116. /*public void setBrowserLogLevel(String logLevel) {

  117. }*/

  118. /*public void runScript(String script) {
  119. commandProcessor.doCommand("runScript", new String[] {script,});
  120. }*/

  121. /*public void addLocationStrategy(String strategyName,String functionDefinition) {
  122. commandProcessor.doCommand("addLocationStrategy", new String[] {strategyName,functionDefinition,});
  123. }*/

  124. publicvoid captureEntirePageScreenshot(String filename) {
  125. File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
  126. try {
  127. FileUtils.copyFile(screenShotFile,new File(filename));
  128. } catch (IOException e) {
  129. // TODO Auto-generated catch block
  130. e.printStackTrace();
  131. }
  132. }

  133. /*public void rollup(String rollupName,String kwargs) {
  134. commandProcessor.doCommand("rollup", new String[] {rollupName,kwargs,});
  135. }
  136. public void addScript(String scriptContent,String scriptTagId) {
  137. commandProcessor.doCommand("addScript", new String[] {scriptContent,scriptTagId,});
  138. }
  139. public void removeScript(String scriptTagId) {
  140. commandProcessor.doCommand("removeScript", new String[] {scriptTagId,});
  141. }
  142. public void useXpathLibrary(String libraryName) {
  143. commandProcessor.doCommand("useXpathLibrary", new String[] {libraryName,});
  144. }
  145. public void setContext(String context) {
  146. commandProcessor.doCommand("setContext", new String[] {context,});
  147. }*/

  148. /*public void attachFile(String fieldLocator,String fileLocator) {
  149. commandProcessor.doCommand("attachFile", new String[] {fieldLocator,fileLocator,});
  150. }*/

  151. /*public void captureScreenshot(String filename) {
  152. commandProcessor.doCommand("captureScreenshot", new String[] {filename,});
  153. }*/

  154. public String captureScreenshotToString() {
  155. String screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
  156. return screen;
  157. }

  158. /* public String captureNetworkTraffic(String type) {
  159. return commandProcessor.getString("captureNetworkTraffic", new String[] {type});
  160. }
  161. */
  162. /*public void addCustomRequestHeader(String key, String value) {
  163. commandProcessor.getString("addCustomRequestHeader", new String[] {key, value});
  164. }*/

  165. /*public String captureEntirePageScreenshotToString(String kwargs) {
  166. return commandProcessor.getString("captureEntirePageScreenshotToString", new String[] {kwargs,});
  167. }*/

  168. publicvoid shutDown() {
  169. driver.quit();
  170. }

  171. /*public String retrieveLastRemoteControlLogs() {
  172. return commandProcessor.getString("retrieveLastRemoteControlLogs", new String[] {});
  173. }*/

  174. publicvoid keyDownNative(Keys keycode) {
  175. new Actions(driver).keyDown(keycode).perform();
  176. }

  177. publicvoid keyUpNative(Keys keycode) {
  178. new Actions(driver).keyUp(keycode).perform();
  179. }

  180. publicvoid keyPressNative(String keycode) {
  181. new Actions(driver).click().perform();
  182. }


  183. publicvoid waitForElementPresent(By by) {
  184. for(int i=0; i<60; i++) {
  185. if (isElementPresent(by)) {
  186. break;
  187. } else {
  188. try {
  189. driver.wait(1000);
  190. } catch (InterruptedException e) {
  191. e.printStackTrace();
  192. }
  193. }
  194. }
  195. }


  196. publicvoid clickAndWaitForElementPresent(By by, By waitElement) {
  197. click(by);
  198. waitForElementPresent(waitElement);
  199. }


  200. public Boolean VeryTitle(String exception,String actual){
  201. if(exception.equals(actual))returntrue;
  202. elsereturnfalse;
  203. }
  204. }
复制代码
PS:有什么建议,欢迎评论,一起交流!
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-11-18 02:21 , Processed in 0.066733 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表