selenium Webdriver 封裝方法

http://blog.csdn.net/gzh0222/article/details/7568527


  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.         public void click(By by) {  
  34.             driver.findElement(by).click();  
  35.         }  
  36.           
  37.         public void doubleClick(By by){  
  38.             new Actions(driver).doubleClick(driver.findElement(by)).perform();  
  39.         }  
  40.           
  41.         public void contextMenu(By by) {  
  42.             new Actions(driver).contextClick(driver.findElement(by)).perform();  
  43.         }  
  44.           
  45.         public void 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.         public void 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.         public void 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.         public void fireEvent(By by,String eventName) {  
  71.             System.out.println("webdriver 不建議使用這樣的方法,所以沒有實現。");  
  72.         }  
  73.           
  74.         public void focus(By by) {  
  75.             System.out.println("webdriver 不建議使用這樣的方法,所以沒有實現。");  
  76.         }  
  77.           
  78.         public void keyPress(By by,Keys theKey) {  
  79.             new Actions(driver).keyDown(driver.findElement(by), theKey).release().perform();          
  80.         }  
  81.           
  82.         public void shiftKeyDown() {  
  83.             new Actions(driver).keyDown(Keys.SHIFT).perform();  
  84.         }  
  85.           
  86.         public void shiftKeyUp() {  
  87.             new Actions(driver).keyUp(Keys.SHIFT).perform();  
  88.         }  
  89.           
  90.         public void metaKeyDown() {  
  91.             new Actions(driver).keyDown(Keys.META).perform();  
  92.         }  
  93.   
  94.         public void metaKeyUp() {  
  95.             new Actions(driver).keyUp(Keys.META).perform();  
  96.         }  
  97.   
  98.         public void altKeyDown() {  
  99.             new Actions(driver).keyDown(Keys.ALT).perform();  
  100.         }  
  101.   
  102.         public void altKeyUp() {  
  103.             new Actions(driver).keyUp(Keys.ALT).perform();  
  104.         }  
  105.   
  106.         public void controlKeyDown() {  
  107.             new Actions(driver).keyDown(Keys.CONTROL).perform();  
  108.         }  
  109.   
  110.         public void controlKeyUp() {  
  111.             new Actions(driver).keyUp(Keys.CONTROL).perform();  
  112.         }  
  113.           
  114.         public void KeyDown(Keys theKey) {  
  115.             new Actions(driver).keyDown(theKey).perform();  
  116.         }  
  117.         public void KeyDown(By by,Keys theKey){  
  118.             new Actions(driver).keyDown(driver.findElement(by), theKey).perform();  
  119.         }  
  120.           
  121.         public void KeyUp(Keys theKey){  
  122.             new Actions(driver).keyUp(theKey).perform();  
  123.         }  
  124.           
  125.         public void KeyUp(By by,Keys theKey){  
  126.             new Actions(driver).keyUp(driver.findElement(by), theKey).perform();  
  127.         }  
  128.           
  129.         public void mouseOver(By by) {  
  130.             new Actions(driver).moveToElement(driver.findElement(by)).perform();  
  131.         }  
  132.           
  133.         public void mouseOut(By by) {  
  134.             System.out.println("沒有實現!");  
  135.             //new Actions(driver).moveToElement((driver.findElement(by)), -10, -10).perform();  
  136.         }  
  137.           
  138.         public void mouseDown(By by) {  
  139.             new Actions(driver).clickAndHold(driver.findElement(by)).perform();  
  140.         }  
  141.           
  142.         public void mouseDownRight(By by) {  
  143.             System.out.println("沒有實現!");  
  144.         }  
  145.           
  146.         public void mouseDownAt(By by,String coordString) {  
  147.             System.out.println("沒有實現!");  
  148.         }  
  149.   
  150.         public void mouseDownRightAt(By by,String coordString) {  
  151.             System.out.println("沒有實現!");  
  152.         }  
  153.   
  154.         public void mouseUp(By by) {  
  155.             System.out.println("沒有實現!");  
  156.         }  
  157.   
  158.         public void mouseUpRight(By by) {  
  159.             System.out.println("沒有實現!");  
  160.         }  
  161.   
  162.         public void mouseUpAt(By by,String coordString) {  
  163.             System.out.println("沒有實現!");  
  164.         }  
  165.   
  166.         public void mouseUpRightAt(By by,String coordString) {  
  167.             System.out.println("沒有實現!");  
  168.         }  
  169.   
  170.         public void mouseMove(By by) {  
  171.             new Actions(driver).moveToElement(driver.findElement(by)).perform();  
  172.         }  
  173.   
  174.         public void 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.         public void type(By by, String testdata) {  
  181.             driver.findElement(by).clear();   
  182.             driver.findElement(by).sendKeys(testdata);  
  183.         }  
  184.   
  185.         public void typeKeys(By by, Keys key) {  
  186.             driver.findElement(by).sendKeys(key);  
  187.         }  
  188.         public void 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.             return null;  
  195.               
  196.         }  
  197.         public void check(By by) {  
  198.             if(!isChecked(by))  
  199.                 click(by);        
  200.         }  
  201.   
  202.         public void uncheck(By by) {  
  203.             if(isChecked(by))   
  204.                 click(by);        
  205.         }  
  206.           
  207.         public void select(By by,String optionValue) {  
  208.             new Select(driver.findElement(by)).selectByValue(optionValue);  
  209.         }  
  210.           
  211.         public void select(By by,int index) {  
  212.             new Select(driver.findElement(by)).selectByIndex(index);  
  213.         }  
  214.   
  215.         public void addSelection(By by,String optionValue) {  
  216.             select(by,optionValue);  
  217.         }  
  218.         public void addSelection(By by,int index) {  
  219.             select(by,index);  
  220.         }  
  221.           
  222.         public void removeSelection(By by,String value) {  
  223.             new Select(driver.findElement(by)).deselectByValue(value);  
  224.         }  
  225.           
  226.         public void removeSelection(By by,int index) {  
  227.             new Select(driver.findElement(by)).deselectByIndex(index);  
  228.         }  
  229.   
  230.         public void removeAllSelections(By by) {  
  231.             new Select(driver.findElement(by)).deselectAll();  
  232.         }  
  233.           
  234.         public void submit(By by) {  
  235.             driver.findElement(by).submit();  
  236.         }  
  237.           
  238.         public void open(String url) {  
  239.             driver.get(url);  
  240.         }  
  241.           
  242.         public void openWindow(String url,String handler) {  
  243.             System.out.println("方法沒有實現!");  
  244.         }  
  245.   
  246.         public void 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.         public void selectPopUp(String handler) {  
  266.             driver.switchTo().window(handler);  
  267.         }  
  268.           
  269.         public void selectPopUp() {  
  270.             driver.switchTo().window(getSecondWindowHandler());  
  271.         }  
  272.           
  273.         public void deselectPopUp() {  
  274.             driver.switchTo().window(getCurrentHandler());  
  275.         }  
  276.           
  277.         public void selectFrame(int index) {  
  278.             driver.switchTo().frame(index);  
  279.         }  
  280.           
  281.         public void selectFrame(String str) {  
  282.             driver.switchTo().frame(str);  
  283.         }  
  284.           
  285.         public void selectFrame(By by) {  
  286.             driver.switchTo().frame(driver.findElement(by));  
  287.         }  
  288.         public void waitForPopUp(String windowID,String timeout) {  
  289.             System.out.println("沒有實現");  
  290.         }  
  291.         public void accept(){  
  292.             driver.switchTo().alert().accept();  
  293.         }  
  294.         public void dismiss(){  
  295.             driver.switchTo().alert().dismiss();  
  296.         }  
  297.         public void chooseCancelOnNextConfirmation() {  
  298.             driver.switchTo().alert().dismiss();  
  299.         }  
  300.           
  301.         public void chooseOkOnNextConfirmation() {  
  302.             driver.switchTo().alert().accept();  
  303.         }  
  304.   
  305.         public void answerOnNextPrompt(String answer) {  
  306.             driver.switchTo().alert().sendKeys(answer);  
  307.         }  
  308.           
  309.         public void goBack() {  
  310.             driver.navigate().back();  
  311.         }  
  312.   
  313.         public void refresh() {  
  314.             driver.navigate().refresh();  
  315.         }  
  316.           
  317.         public void forward() {  
  318.             driver.navigate().forward();  
  319.         }  
  320.           
  321.         public void to(String urlStr){  
  322.             driver.navigate().to(urlStr);  
  323.         }  
  324.           
  325.         public void close() {  
  326.             driver.close();  
  327.         }  
  328.           
  329.         public boolean 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.         public boolean isPromptPresent() {  
  340.             return isAlertPresent();  
  341.         }  
  342.   
  343.         public boolean 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.         public void 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.         public boolean 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.         public boolean 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.         public boolean isTextPresent(String pattern) {  
  517.             String Xpath= "//*[contains(text(),\'"+pattern+"\')]" ;  
  518.             try {   
  519.                 driver.findElement(By.xpath(Xpath));  
  520.                 return true;   
  521.             } catch (NoSuchElementException e) {   
  522.                 return false;   
  523.             }     
  524.         }  
  525.   
  526.         public boolean isElementPresent(By by) {  
  527.             return driver.findElements(by).size() > 0;  
  528.         }  
  529.   
  530.         public boolean isVisible(By by) {  
  531.             return driver.findElement(by).isDisplayed();  
  532.         }  
  533.   
  534.         public boolean 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.             return null;  
  552.         }  
  553.         public void dragdrop(By by,String movementsString) {  
  554.             dragAndDrop(by, movementsString);  
  555.         }  
  556.         public void 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.         public void setMouseSpeed(String pixels) {  
  563.             System.out.println("不支持");  
  564.         }  
  565.   
  566.         public Number getMouseSpeed() {  
  567.             System.out.println("不支持");  
  568.             return null;  
  569.         }  
  570.         public void dragAndDropToObject(By source,By target) {  
  571.             new Actions(driver).dragAndDrop(driver.findElement(source), driver.findElement(target)).perform();  
  572.         }  
  573.   
  574.         public void windowFocus() {  
  575.             driver.switchTo().defaultContent();  
  576.         }  
  577.   
  578.         public void 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.             return null;  
  588.         }  
  589.   
  590.         public String[] getAllWindowNames() {  
  591.             System.out.println("不能實現!");  
  592.             return null;  
  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.         public void setCursorPosition(String locator,String position) {  
  608.             System.out.println("沒能實現!");  
  609.         }  
  610.   
  611.         public Number getElementIndex(String locator) {  
  612.             System.out.println("沒能實現!");  
  613.             return null;  
  614.         }  
  615.   
  616.         public Object isOrdered(By by1,By by2) {  
  617.             System.out.println("沒能實現!");  
  618.             return null;  
  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.             return null;  
  640.         }  
  641.   
  642.         public String getExpression(String expression) {  
  643.             System.out.println("沒能實現!");  
  644.             return null;  
  645.         }  
  646.   
  647.         public Number getXpathCount(By xpath) {  
  648.             return driver.findElements(xpath).size();  
  649.         }  
  650.   
  651.         public void assignId(By by,String identifier) {  
  652.             System.out.println("不想實現!");  
  653.         }  
  654.   
  655.         /*public void 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.         public void 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==truebreak;  
  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.         public void setTimeout(String timeout) {  
  681.             driver.manage().timeouts().implicitlyWait(Integer.parseInt(timeout), TimeUnit.SECONDS);  
  682.         }  
  683.   
  684.         public void waitForPageToLoad(String timeout) {  
  685.             driver.manage().timeouts().pageLoadTimeout(Integer.parseInt(timeout), TimeUnit.SECONDS);  
  686.         }  
  687.   
  688.         public void 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.         public boolean 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.         public void createCookie(Cookie c) {  
  716.               
  717.             driver.manage().addCookie(c);  
  718.         }  
  719.   
  720.         public void deleteCookie(Cookie c) {  
  721.             driver.manage().deleteCookie(c);  
  722.         }  
  723.   
  724.         public void 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.         public void 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.  
  754.         public void addScript(String scriptContent,String scriptTagId) { 
  755.             commandProcessor.doCommand("addScript", new String[] {scriptContent,scriptTagId,}); 
  756.         } 
  757.  
  758.         public void removeScript(String scriptTagId) { 
  759.             commandProcessor.doCommand("removeScript", new String[] {scriptTagId,}); 
  760.         } 
  761.  
  762.         public void useXpathLibrary(String libraryName) { 
  763.             commandProcessor.doCommand("useXpathLibrary", new String[] {libraryName,}); 
  764.         } 
  765.  
  766.         public void setContext(String context) { 
  767.             commandProcessor.doCommand("setContext", new String[] {context,}); 
  768.         }*/  
  769.   
  770.         /*public void attachFile(String fieldLocator,String fileLocator) { 
  771.             commandProcessor.doCommand("attachFile", new String[] {fieldLocator,fileLocator,}); 
  772.         }*/  
  773.   
  774.         /*public void captureScreenshot(String filename) { 
  775.             commandProcessor.doCommand("captureScreenshot", new String[] {filename,}); 
  776.         }*/  
  777.   
  778.         public String captureScreenshotToString() {  
  779.              String screen = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);  
  780.              return screen;  
  781.         }  
  782.   
  783.        /* public String captureNetworkTraffic(String type) { 
  784.             return commandProcessor.getString("captureNetworkTraffic", new String[] {type}); 
  785.         } 
  786. */  
  787.         /*public void addCustomRequestHeader(String key, String value) { 
  788.             commandProcessor.getString("addCustomRequestHeader", new String[] {key, value}); 
  789.         }*/  
  790.   
  791.         /*public String captureEntirePageScreenshotToString(String kwargs) { 
  792.             return commandProcessor.getString("captureEntirePageScreenshotToString", new String[] {kwargs,}); 
  793.         }*/  
  794.   
  795.         public void shutDown() {  
  796.             driver.quit();  
  797.         }  
  798.   
  799.         /*public String retrieveLastRemoteControlLogs() { 
  800.             return commandProcessor.getString("retrieveLastRemoteControlLogs", new String[] {}); 
  801.         }*/  
  802.   
  803.         public void keyDownNative(Keys keycode) {  
  804.             new Actions(driver).keyDown(keycode).perform();  
  805.         }  
  806.   
  807.         public void keyUpNative(Keys keycode) {  
  808.             new Actions(driver).keyUp(keycode).perform();  
  809.         }  
  810.   
  811.         public void keyPressNative(String keycode) {  
  812.             new Actions(driver).click().perform();  
  813.         }  
  814.               
  815.   
  816.             public void waitForElementPresent(By by) {  
  817.                 for(int i=0; i<60; i++) {  
  818.                 if (isElementPresent(by)) {  
  819.                 break;  
  820.                 } else {  
  821.                 try {  
  822.                 driver.wait(1000);  
  823.                 } catch (InterruptedException e) {  
  824.                 e.printStackTrace();  
  825.                         }  
  826.                     }  
  827.                 }  
  828.                 }  
  829.               
  830.   
  831.             public void clickAndWaitForElementPresent(By by, By waitElement) {  
  832.                 click(by);  
  833.                 waitForElementPresent(waitElement);  
  834.                 }  
  835.               
  836.               
  837.             public Boolean VeryTitle(String exception,String actual){  
  838.                 if(exception.equals(actual)) return true;  
  839.                 else return false;  
  840.             }  
  841.         } 


發佈了54 篇原創文章 · 獲贊 22 · 訪問量 44萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章