selenium常用的方法_第1頁
selenium常用的方法_第2頁
selenium常用的方法_第3頁
selenium常用的方法_第4頁
selenium常用的方法_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、package avaya.browser.method;import java.io.IOException;import java.util.*;import org.junit.Before;import org.openqa.*;import com.thoughtworks.selenium.Wait;import erfac.Driver;/attention: 以Br.開頭的方法,是因為Br是自己在本類還封裝了一層,需要仔細看代碼public class Br extends Baseclass private String locator;pu

2、blic static String log(String info)System.out.println(info);return info;public static void openbrowser(String url)/driver=new FirefoxDriver();/Navigation navitation=driver.navigate();/navitation.to(url);driver.get(url);Br.log(open browser and input testURL:+url);/opration the windows maxpublic stati

3、c void maxwindow()driver.manage().window().maximize();Br.log(max windows);/refresh the windowspublic static void refresh()driver.navigate().refresh();Br.log(refresh the windows); public static void login(String url,String user,String pwd) throws InterruptedException driver.get(url); Br.clear(By.id(B

4、r.logrm:username); Br.sendkeys(By.id(Br.logrm:username),user); Br.clear(By.id(Br.logrm:password); Br.sendkeys(By.id(Br.logrm:password),pwd); Br.getWebText(By.id(Br.logrm:submitText); driver.findElement(By.id(Br.logrm:submitText).click(); try Thread.sleep(5000); catch (InterruptedException e) / TODO

5、Auto-generated catch blocke.printStackTrace(); Op.refresh();Br.log(,input:+url);Br.log(登陸賬號為:+user+ +登陸密碼為:+pwd); /if form parameter is not null,input data and use this method;public static void closebrowser(String BrowserType)if(BrowserType!=null) if(BrowserType.equals(c) try Runtime.getRuntime().e

6、xec(taskkill /F /IM chrome.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); else if(BrowserType.equals(i) try Runtime.getRuntime().exec(taskkill /F /IM iexplorer.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); else if(BrowserType.equ

7、als(o) try Runtime.getRuntime().exec(taskkill /F /IM opera.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); else if(BrowserType.equals(s) try Runtime.getRuntime().exec(taskkill /F /IM safari.exe); catch (IOException e) / TODO Auto-generated catch block e.printStackTr

8、ace(); else try Runtime.getRuntime().exec(taskkill /F /IM firefox.exe); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace(); /if form parameter is null,use this method;public static void closebrowser()try Runtime.getRuntime().exec(taskkill /F /IM firefox.exe); catch (IOExcepti

9、on e) / TODO Auto-generated catch blocke.printStackTrace(); public static void switchToIframe(By by)driver.switchTo().frame(driver.findElement(by);/driver.switchTo().frame(driver.findElement(By.xpath(/html/body/div4/div4/div/div/iframe); /find element and set the valuepublic static void sendkeys(By

10、by,String str) throws InterruptedExceptionString text;/text=driver.findElement(by).getText();driver.findElement(by).clear();Thread.sleep(1500);driver.findElement(by).sendKeys(str);/Br.log(execute select element object+text);Br.log(Set: +str);public static void click(By by) throws InterruptedExceptio

11、nString text;if(driver.findElement(by).getText()!=null & ! .equals(driver.findElement(by).getText()text=driver.findElement(by).getText();Thread.sleep(3000);System.out.println(execute click operation);System.out.println(click+ +text);elseSystem.out.println(this Element is not found text);driver.findE

12、lement(by).click();/* doubleclick * by */public static void doubleClick(By by)new Actions(driver).doubleClick(driver.findElement(by).doubleClick();public static void clear(By by)driver.findElement(by).clear();Br.log(Empty element content,by: +by);/*select element by index * webdriver is nonsupport s

13、elect function,so suggest not */ public static void selectByIndex(By by,int index)/driver.findElement(by).click();String text;Select select=new Select(driver.findElement(by);try Thread.sleep(10); catch (InterruptedException e) / TODO Auto-generated catch blocke.printStackTrace();select.selectByIndex

14、(index);Br.log(Perform a drop-down box operations,operation element:+by);text=getWebText(by);Br.log(select+index+element+element info is:+text);/*select element by value * webdriver is nonsupport select function,so suggest not */public static void selectByText(By by,String text)driver.findElement(by

15、).click();Select select=new Select(driver.findElement(by);select.selectByVisibleText(text);/select element by value/public static void selectBy(By by,String value)/driver.findElement(by).click();/Select select=new Select(driver.findElement(by);/select.selectByValue(value);/public static void select(

16、By by0,By by1)/driver.findElement(by0).click();/driver.findElement(by1).click();/Select select=new Select(driver.findElement(by);/select.selectByValue(value);/*select element by linktext * webdriver is nonsupport select function,so suggest not */public static void selectByLinkText(By by,String LinkT

17、ext)/driver.findElement(by).click();Select select=new Select(driver.findElement(by);select.selectByValue(LinkText);Br.log(execute select element ,the object:+LinkText); / 獲取鏈接元素的url值 public static String getLinkUrl(By by) return driver.findElement(by).getAttribute(href); / 判斷某個元素是否存在 public static b

18、oolean isElementExist(By by) try Boolean bool = driver.findElement(by).isDisplayed(); Br.log(execute+by+exist or not+,+result is :+bool); return bool; catch(NoSuchElementException e) return false; / 獲得某元素的文本描述信息 public static String getWebText(By by) String text; text=driver.findElement(by).getText(

19、);try Br.log(get Element text +:+text);return text;/return driver.findElement(by).getText();catch (NoSuchElementException e)return not found the Element text!; /* * 在一個元素集合中通過遍歷文本內容定位到相應的元素 */ public static void clickElementContainingText(By by,String text) List elementList = driver.findElements(by)

20、; for(WebElement e:elementList) if(e.getText().contains(text) e.click(); break; /* 根據(jù)id定位元素并獲取元素的選中狀態(tài) * param id * return */ public static boolean getselectstat(By by) boolean flag=true; if(flag) Br.log(頁面元素狀態(tài):+by+已被選中); return driver.findElement(by).isSelected(); else Br.log(頁面元素狀態(tài):+by+未被選中); retur

21、n false; /* 等待時間 * param second * return */ public static boolean wait(int second) trydriver.manage().timeouts().implicitlyWait(second, TimeUnit.SECONDS); /Br.log(執(zhí)行了等待操作,等待了+second+秒); return true; catch(Exception e) return false; / public static void waitForpageToload(int i)/ boolean isLoaded=fals

22、e; / int count=0; / do / if(count+3) / break; / / try / selenium.waitForPageToLoad(i); / isLoaded=true; / catch(Exception ex) / continue; / / while(!isLoaded); / / / /public boolean switchToWindow(WebDriver driver,String windowTitle) / 通過彈出框的title進行切換,非xpath等方法,此方法比較方便 public static boolean switchto

23、bytitle(String windowTitle) boolean flag = false; try String currentHandle = driver.getWindowHandle(); Set handles = driver.getWindowHandles(); for (String s : handles) if (s.equals(currentHandle) /System.out.println(test here); continue; else driver.switchTo().window(s); if (driver.getTitle().contains(windowTitle) flag = true; System.out.println(切換窗口到:+ windowTitle); break; else continue; catch (NoSuchWindowException e) System.out.printf(Window: + windowTitle+ cound not found!, e.fillInStackTrace(); flag = false; return flag; /* 通過windows的id號進行切換 * i 窗口的索引從0開始 * by driver的查找元素

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論