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

来源:互联网 发布:小语网络加速器下载 编辑:程序博客网 时间:2024/05/17 07:52

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

  1. 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.          
  265.         publicvoid selectPopUp(String handler) { 
  266.             driver.switchTo().window(handler); 
  267.         } 
  268.          
  269.         publicvoid selectPopUp() { 
  270.             driver.switchTo().window(getSecondWindowHandler()); 
  271.         } 
  272.          
  273.         publicvoid deselectPopUp() { 
  274.             driver.switchTo().window(getCurrentHandler()); 
  275.         } 
  276.          
  277.         publicvoid selectFrame(int index) { 
  278.             driver.switchTo().frame(index); 
  279.         } 
  280.          
  281.         publicvoid selectFrame(String str) { 
  282.             driver.switchTo().frame(str); 
  283.         } 
  284.          
  285.         publicvoid selectFrame(By by) { 
  286.             driver.switchTo().frame(driver.findElement(by)); 
  287.         } 
  288.         publicvoid waitForPopUp(String windowID,String timeout) { 
  289.             System.out.println("没有实现"); 
  290.         } 
  291.         publicvoid accept(){ 
  292.             driver.switchTo().alert().accept(); 
  293.         } 
  294.         publicvoid dismiss(){ 
  295.             driver.switchTo().alert().dismiss(); 
  296.         } 
  297.         publicvoid chooseCancelOnNextConfirmation() { 
  298.             driver.switchTo().alert().dismiss(); 
  299.         } 
  300.          
  301.         publicvoid chooseOkOnNextConfirmation() { 
  302.             driver.switchTo().alert().accept(); 
  303.         } 
  304.  
  305.         publicvoid answerOnNextPrompt(String answer) { 
  306.             driver.switchTo().alert().sendKeys(answer); 
  307.         } 
  308.          
  309.         publicvoid goBack() { 
  310.             driver.navigate().back(); 
  311.         } 
  312.  
  313.         publicvoid refresh() { 
  314.             driver.navigate().refresh(); 
  315.         } 
  316.          
  317.         publicvoid forward() { 
  318.             driver.navigate().forward(); 
  319.         } 
  320.          
  321.         publicvoid to(String urlStr){ 
  322.             driver.navigate().to(urlStr); 
  323.         } 
  324.          
  325.         publicvoid close() { 
  326.             driver.close(); 
  327.         } 
  328.          
  329.         publicboolean isAlertPresent() { 
  330.             Boolean b = true
  331.             try
  332.                 driver.switchTo().alert(); 
  333.             }catch(Exception e){ 
  334.                 b = false
  335.             } 
  336.             return b; 
  337.         } 
  338.  
  339.         publicboolean isPromptPresent() { 
  340.             return isAlertPresent(); 
  341.         } 
  342.  
  343.         publicboolean isConfirmationPresent() { 
  344.             return isAlertPresent(); 
  345.         } 
  346.  
  347.         public String getAlert() { 
  348.             return driver.switchTo().alert().getText(); 
  349.         } 
  350.  
  351.         public String getConfirmation() { 
  352.             return getAlert(); 
  353.         } 
  354.  
  355.         public String getPrompt() { 
  356.             return getAlert(); 
  357.         } 
  358.  
  359.         public String getLocation() { 
  360.             return driver.getCurrentUrl(); 
  361.         } 
  362.          
  363.         public String getTitle(){ 
  364.             return driver.getTitle();        
  365.         } 
  366.          
  367.         public String getBodyText() { 
  368.             String str = ""
  369.             List<WebElement> elements = driver.findElements(By.xpath("//body//*[contains(text(),*)]")); 
  370.             for(WebElement e : elements){ 
  371.                 str += e.getText()+" "
  372.             } 
  373.              return str; 
  374.         } 
  375.  
  376.         public String getValue(By by) { 
  377.             return driver.findElement(by).getAttribute("value"); 
  378.         } 
  379.  
  380.         public String getText(By by) { 
  381.             return driver.findElement(by).getText(); 
  382.         } 
  383.  
  384.         publicvoid highlight(By by) { 
  385.             WebElement element = driver.findElement(by); 
  386.             ((JavascriptExecutor)driver).executeScript("arguments[0].style.border = \"5px solid yellow\"",element);  
  387.         } 
  388.  
  389.         public Object getEval(String script,Object... args) { 
  390.             return ((JavascriptExecutor)driver).executeScript(script,args); 
  391.         } 
  392.         public Object getAsyncEval(String script,Object... args){ 
  393.             return  ((JavascriptExecutor)driver).executeAsyncScript(script, args); 
  394.         } 
  395.         publicboolean isChecked(By by) { 
  396.             return driver.findElement(by).isSelected(); 
  397.         } 
  398.         public String getTable(By by,String tableCellAddress) { 
  399.             WebElement table = driver.findElement(by); 
  400.             int index = tableCellAddress.trim().indexOf('.'); 
  401.             int row =  Integer.parseInt(tableCellAddress.substring(0, index)); 
  402.             int cell = Integer.parseInt(tableCellAddress.substring(index+1)); 
  403.              List<WebElement> rows = table.findElements(By.tagName("tr")); 
  404.              WebElement theRow = rows.get(row); 
  405.              String text = getCell(theRow, cell); 
  406.              return text; 
  407.         } 
  408.         private String getCell(WebElement Row,int cell){ 
  409.              List<WebElement> cells; 
  410.              String text = null
  411.              if(Row.findElements(By.tagName("th")).size()>0){ 
  412.                 cells = Row.findElements(By.tagName("th")); 
  413.                 text = cells.get(cell).getText(); 
  414.              } 
  415.              if(Row.findElements(By.tagName("td")).size()>0){ 
  416.                 cells = Row.findElements(By.tagName("td")); 
  417.                 text = cells.get(cell).getText(); 
  418.              } 
  419.             return text; 
  420.               
  421.         } 
  422.  
  423.         public String[] getSelectedLabels(By by) { 
  424.                 Set<String> set = new HashSet<String>(); 
  425.                 List<WebElement> selectedOptions =new Select(driver.findElement(by)) 
  426.                                                                                 .getAllSelectedOptions(); 
  427.                 for(WebElement e : selectedOptions){ 
  428.                     set.add(e.getText()); 
  429.                 } 
  430.             return set.toArray(new String[set.size()]); 
  431.         } 
  432.  
  433.         public String getSelectedLabel(By by) { 
  434.             return getSelectedOption(by).getText(); 
  435.         } 
  436.  
  437.         public String[] getSelectedValues(By by) { 
  438.             Set<String> set = new HashSet<String>(); 
  439.             List<WebElement> selectedOptions =new Select(driver.findElement(by)) 
  440.                                                                             .getAllSelectedOptions(); 
  441.             for(WebElement e : selectedOptions){ 
  442.                 set.add(e.getAttribute("value")); 
  443.             } 
  444.         return set.toArray(new String[set.size()]); 
  445.         } 
  446.  
  447.         public String getSelectedValue(By by) { 
  448.             return getSelectedOption(by).getAttribute("value"); 
  449.         } 
  450.  
  451.         public String[] getSelectedIndexes(By by) { 
  452.             Set<String> set = new HashSet<String>(); 
  453.             List<WebElement> selectedOptions =new Select(driver.findElement(by)) 
  454.                                                                             .getAllSelectedOptions(); 
  455.            List<WebElement> options = new Select(driver.findElement(by)).getOptions(); 
  456.             for(WebElement e : selectedOptions){ 
  457.                 set.add(String.valueOf(options.indexOf(e))); 
  458.             } 
  459.             return set.toArray(new String[set.size()]); 
  460.         } 
  461.  
  462.         public String getSelectedIndex(By by) { 
  463.             List<WebElement> options = new Select(driver.findElement(by)).getOptions(); 
  464.             return String.valueOf(options.indexOf(getSelectedOption(by))); 
  465.         } 
  466.  
  467.         public String[] getSelectedIds(By by) { 
  468.             Set<String> ids = new HashSet<String>(); 
  469.             List<WebElement> options = new Select(driver.findElement(by)).getOptions(); 
  470.             for(WebElement option : options){ 
  471.                 if(option.isSelected()) { 
  472.                     ids.add(option.getAttribute("id")) ; 
  473.                 } 
  474.             } 
  475.             return ids.toArray(new String[ids.size()]); 
  476.         } 
  477.  
  478.         public String getSelectedId(By by) { 
  479.             return getSelectedOption(by).getAttribute("id"); 
  480.         } 
  481.         private WebElement getSelectedOption(By by){ 
  482.             WebElement selectedOption = null
  483.             List<WebElement> options = new Select(driver.findElement(by)).getOptions(); 
  484.             for(WebElement option : options){ 
  485.                 if(option.isSelected()) { 
  486.                     selectedOption = option; 
  487.                 } 
  488.             } 
  489.             return selectedOption; 
  490.         } 
  491.          
  492.         publicboolean isSomethingSelected(By by) { 
  493.             boolean b =false
  494.             List<WebElement> options = new Select(driver.findElement(by)).getOptions(); 
  495.             for(WebElement option : options){ 
  496.                 if(option.isSelected()) { 
  497.                     b = true
  498.                     break
  499.                 } 
  500.             } 
  501.             return b; 
  502.         } 
  503.  
  504.         public String[] getSelectOptions(By by) { 
  505.             Set<String> set = new HashSet<String>(); 
  506.             List<WebElement> options = new Select(driver.findElement(by)).getOptions(); 
  507.             for(WebElement e : options){ 
  508.                 set.add(e.getText()); 
  509.             } 
  510.         return set.toArray(new String[set.size()]); 
  511.         } 
  512.         public String getAttribute(By by,String attributeLocator) { 
  513.             return driver.findElement(by).getAttribute(attributeLocator); 
  514.         } 
  515.  
  516.         publicboolean isTextPresent(String pattern) { 
  517.             String Xpath= "//*[contains(text(),\'"+pattern+"\')]"
  518.             try {  
  519.                 driver.findElement(By.xpath(Xpath)); 
  520.                 returntrue;  
  521.             } catch (NoSuchElementException e) {  
  522.                 returnfalse;  
  523.             }    
  524.         } 
  525.  
  526.         publicboolean isElementPresent(By by) { 
  527.             return driver.findElements(by).size() >0
  528.         } 
  529.  
  530.         publicboolean isVisible(By by) { 
  531.             return driver.findElement(by).isDisplayed(); 
  532.         } 
  533.  
  534.         publicboolean isEditable(By by) { 
  535.             return driver.findElement(by).isEnabled(); 
  536.         } 
  537.         public List<WebElement> getAllButtons() { 
  538.             return driver.findElements(By.xpath("//input[@type='button']"));             
  539.         } 
  540.  
  541.         public List<WebElement> getAllLinks() { 
  542.             return driver.findElements(By.tagName("a")); 
  543.         }    
  544.  
  545.         public List<WebElement> getAllFields() { 
  546.             return driver.findElements(By.xpath("//input[@type='text']")); 
  547.         } 
  548.  
  549.         public String[] getAttributeFromAllWindows(String attributeName) { 
  550.             System.out.println("不知道怎么实现"); 
  551.             returnnull
  552.         } 
  553.         publicvoid dragdrop(By by,String movementsString) { 
  554.             dragAndDrop(by, movementsString); 
  555.         } 
  556.         publicvoid dragAndDrop(By by,String movementsString) { 
  557.             int index = movementsString.trim().indexOf('.'); 
  558.             int xOffset = Integer.parseInt(movementsString.substring(0, index)); 
  559.             int yOffset = Integer.parseInt(movementsString.substring(index+1)); 
  560.             new Actions(driver).clickAndHold(driver.findElement(by)).moveByOffset(xOffset, yOffset).perform(); 
  561.         } 
  562.         publicvoid setMouseSpeed(String pixels) { 
  563.             System.out.println("不支持"); 
  564.         } 
  565.  
  566.         public Number getMouseSpeed() { 
  567.             System.out.println("不支持"); 
  568.             returnnull
  569.         } 
  570.         publicvoid dragAndDropToObject(By source,By target) { 
  571.             new Actions(driver).dragAndDrop(driver.findElement(source), driver.findElement(target)).perform(); 
  572.         } 
  573.  
  574.         publicvoid windowFocus() { 
  575.             driver.switchTo().defaultContent(); 
  576.         } 
  577.  
  578.         publicvoid windowMaximize() { 
  579.             driver.manage().window().setPosition(new Point(0,0)); 
  580.             java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize(); 
  581.             Dimension dim = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight()); 
  582.             driver.manage().window().setSize(dim); 
  583.         } 
  584.  
  585.         public String[] getAllWindowIds() { 
  586.             System.out.println("不能实现!"); 
  587.             returnnull
  588.         } 
  589.  
  590.         public String[] getAllWindowNames() { 
  591.             System.out.println("不能实现!"); 
  592.             returnnull
  593.         } 
  594.  
  595.         public String[] getAllWindowTitles() { 
  596.             Set<String> handles = driver.getWindowHandles(); 
  597.             Set<String> titles = new HashSet<String>(); 
  598.             for(String handle : handles){ 
  599.                 titles.add(driver.switchTo().window(handle).getTitle()); 
  600.             } 
  601.             return titles.toArray(new String[titles.size()]); 
  602.         } 
  603.         public String getHtmlSource() { 
  604.             return driver.getPageSource(); 
  605.         } 
  606.  
  607.         publicvoid setCursorPosition(String locator,String position) { 
  608.             System.out.println("没能实现!"); 
  609.         } 
  610.  
  611.         public Number getElementIndex(String locator) { 
  612.             System.out.println("没能实现!"); 
  613.             returnnull
  614.         } 
  615.  
  616.         public Object isOrdered(By by1,By by2) { 
  617.             System.out.println("没能实现!"); 
  618.             returnnull
  619.         } 
  620.  
  621.         public Number getElementPositionLeft(By by) { 
  622.             return driver.findElement(by).getLocation().getX(); 
  623.         } 
  624.  
  625.         public Number getElementPositionTop(By by) { 
  626.             return driver.findElement(by).getLocation().getY(); 
  627.         } 
  628.  
  629.         public Number getElementWidth(By by) { 
  630.             return driver.findElement(by).getSize().getWidth(); 
  631.         } 
  632.  
  633.         public Number getElementHeight(By by) { 
  634.             return driver.findElement(by).getSize().getHeight(); 
  635.         } 
  636.  
  637.         public Number getCursorPosition(String locator) { 
  638.             System.out.println("没能实现!"); 
  639.             returnnull
  640.         } 
  641.  
  642.         public String getExpression(String expression) { 
  643.             System.out.println("没能实现!"); 
  644.             returnnull
  645.         } 
  646.  
  647.         public Number getXpathCount(By xpath) { 
  648.             return driver.findElements(xpath).size(); 
  649.         } 
  650.  
  651.         publicvoid assignId(By by,String identifier) { 
  652.             System.out.println("不想实现!"); 
  653.         } 
  654.  
  655.         /*publicvoid allowNativeXpath(String allow) { 
  656.             commandProcessor.doCommand("allowNativeXpath",new String[] {allow,}); 
  657.         }*/ 
  658.  
  659.         /*public void ignoreAttributesWithoutValue(String ignore) {
  660.             commandProcessor.doCommand("ignoreAttributesWithoutValue", new String[] {ignore,});
  661.            
  662.         }*/ 
  663.  
  664.         publicvoid waitForCondition(String script,String timeout,Object... args) { 
  665.             Boolean b = false
  666.             int time =0
  667.             while(time <= Integer.parseInt(timeout)){ 
  668.                 b = (Boolean) ((JavascriptExecutor)driver).executeScript(script,args); 
  669.                 if(b==true)break
  670.                 try
  671.                     Thread.sleep(1000); 
  672.                 } catch (InterruptedException e) { 
  673.                     // TODO Auto-generated catch block 
  674.                     e.printStackTrace(); 
  675.                 } 
  676.                 time += 1000
  677.             }    
  678.         } 
  679.  
  680.         publicvoid setTimeout(String timeout) { 
  681.             driver.manage().timeouts().implicitlyWait(Integer.parseInt(timeout), TimeUnit.SECONDS); 
  682.         } 
  683.  
  684.         publicvoid waitForPageToLoad(String timeout) { 
  685.             driver.manage().timeouts().pageLoadTimeout(Integer.parseInt(timeout), TimeUnit.SECONDS); 
  686.         } 
  687.  
  688.         publicvoid waitForFrameToLoad(String frameAddress,String timeout) { 
  689.             /*driver.switchTo().frame(frameAddress)
  690.                                 .manage()
  691.                                 .timeouts()
  692.                                 .pageLoadTimeout(Integer.parseInt(timeout), TimeUnit.SECONDS);*/ 
  693.         } 
  694.  
  695.         public String getCookie() { 
  696.             String cookies = ""
  697.             Set<Cookie> cookiesSet = driver.manage().getCookies(); 
  698.             for(Cookie c : cookiesSet){ 
  699.                 cookies += c.getName()+"="+c.getValue()+";"
  700.                 } 
  701.             return cookies; 
  702.         } 
  703.  
  704.         public String getCookieByName(String name) { 
  705.             return driver.manage().getCookieNamed(name).getValue(); 
  706.         } 
  707.  
  708.         publicboolean isCookiePresent(String name) { 
  709.             boolean b =false
  710.             if(driver.manage().getCookieNamed(name) !=null || driver.manage().getCookieNamed(name).equals(null)) 
  711.                 b = true
  712.             return b; 
  713.         } 
  714.  
  715.         publicvoid createCookie(Cookie c) { 
  716.              
  717.             driver.manage().addCookie(c); 
  718.         } 
  719.  
  720.         publicvoid deleteCookie(Cookie c) { 
  721.             driver.manage().deleteCookie(c); 
  722.         } 
  723.  
  724.         publicvoid deleteAllVisibleCookies() { 
  725.             driver.manage().getCookieNamed("fs").isSecure(); 
  726.         } 
  727.  
  728.         /*public void setBrowserLogLevel(String logLevel) {
  729.            
  730.         }*/ 
  731.  
  732.         /*public void runScript(String script) {
  733.             commandProcessor.doCommand("runScript", new String[] {script,});
  734.         }*/ 
  735.  
  736.         /*public void addLocationStrategy(String strategyName,String functionDefinition) {
  737.             commandProcessor.doCommand("addLocationStrategy", new String[] {strategyName,functionDefinition,});
  738.         }*/ 
  739.  
  740.         publicvoid captureEntirePageScreenshot(String filename) { 
  741.             File screenShotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); 
  742.             try
  743.                 FileUtils.copyFile(screenShotFile,new File(filename)); 
  744.             } catch (IOException e) { 
  745.                 // TODO Auto-generated catch block 
  746.                 e.printStackTrace(); 
  747.             } 
  748.         } 
  749.  
  750.         /*public void rollup(String rollupName,String kwargs) {
  751.             commandProcessor.doCommand("rollup", new String[] {rollupName,kwargs,});
  752.         }
  753.         public void addScript(String scriptContent,String scriptTagId) {
  754.             commandProcessor.doCommand("addScript", new String[] {scriptContent,scriptTagId,});
  755.         }
  756.         public void removeScript(String scriptTagId) {
  757.             commandProcessor.doCommand("removeScript", new String[] {scriptTagId,});
  758.         }
  759.         public void useXpathLibrary(String libraryName) {
  760.             commandProcessor.doCommand("useXpathLibrary", new String[] {libraryName,});
  761.         }
  762.         public void setContext(String context) {
  763.             commandProcessor.doCommand("setContext", new String[] {context,});
  764.         }*/ 
  765.  
  766.         /*public void attachFile(String fieldLocator,String fileLocator) {
  767.             commandProcessor.doCommand("attachFile", new String[] {fieldLocator,fileLocator,});
  768.         }*/ 
  769.  
  770.         /*public void captureScreenshot(String filename) {
  771.             commandProcessor.doCommand("captureScreenshot", new String[] {filename,});
  772.         }*/ 
  773.  
  774.         public String captureScreenshotToString() { 
  775.              String screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64); 
  776.              return screen; 
  777.         } 
  778.  
  779.        /* public String captureNetworkTraffic(String type) {
  780.             return commandProcessor.getString("captureNetworkTraffic", new String[] {type});
  781.         }
  782. */ 
  783.         /*public void addCustomRequestHeader(String key, String value) {
  784.             commandProcessor.getString("addCustomRequestHeader", new String[] {key, value});
  785.         }*/ 
  786.  
  787.         /*public String captureEntirePageScreenshotToString(String kwargs) {
  788.             return commandProcessor.getString("captureEntirePageScreenshotToString", new String[] {kwargs,});
  789.         }*/ 
  790.  
  791.         publicvoid shutDown() { 
  792.             driver.quit(); 
  793.         } 
  794.  
  795.         /*public String retrieveLastRemoteControlLogs() {
  796.             return commandProcessor.getString("retrieveLastRemoteControlLogs", new String[] {});
  797.         }*/ 
  798.  
  799.         publicvoid keyDownNative(Keys keycode) { 
  800.             new Actions(driver).keyDown(keycode).perform(); 
  801.         } 
  802.  
  803.         publicvoid keyUpNative(Keys keycode) { 
  804.             new Actions(driver).keyUp(keycode).perform(); 
  805.         } 
  806.  
  807.         publicvoid keyPressNative(String keycode) { 
  808.             new Actions(driver).click().perform(); 
  809.         } 
  810.              
  811.  
  812.             publicvoid waitForElementPresent(By by) { 
  813.                 for(int i=0; i<60; i++) { 
  814.                 if (isElementPresent(by)) { 
  815.                 break
  816.                 } else
  817.                 try
  818.                 driver.wait(1000); 
  819.                 } catch (InterruptedException e) { 
  820.                 e.printStackTrace(); 
  821.                         } 
  822.                     } 
  823.                 } 
  824.                 } 
  825.              
  826.  
  827.             publicvoid clickAndWaitForElementPresent(By by, By waitElement) { 
  828.                 click(by); 
  829.                 waitForElementPresent(waitElement); 
  830.                 } 
  831.              
  832.              
  833.             public Boolean VeryTitle(String exception,String actual){ 
  834.                 if(exception.equals(actual))returntrue
  835.                 elsereturnfalse
  836.             } 
  837.         } 

PS:有什么建议,欢迎评论,一起交流!