全自動化測試基礎(chǔ)-WebDriver啟動瀏覽器的幾種方法總結(jié)_第1頁
全自動化測試基礎(chǔ)-WebDriver啟動瀏覽器的幾種方法總結(jié)_第2頁
全自動化測試基礎(chǔ)-WebDriver啟動瀏覽器的幾種方法總結(jié)_第3頁
全自動化測試基礎(chǔ)-WebDriver啟動瀏覽器的幾種方法總結(jié)_第4頁
全自動化測試基礎(chǔ)-WebDriver啟動瀏覽器的幾種方法總結(jié)_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

自動化測試基礎(chǔ)-WebDriver啟動瀏覽器的幾種方法總結(jié)在使用相應(yīng)的WebDriver之前,應(yīng)確保運行環(huán)境的OS和Browser都與WebDriver所需的運行環(huán)境相匹配。一、啟動FirefoxBrowser。PS:由于FirefoxDriver是直接打包在WebDriverJavaClientDriver中,所以下載后者后,就不需要再另外下載FirefoxDriver。(1)這種情況適用于Firefox安裝在了默認路徑下:WebDriverdriver=newFirefoxDriver();//直接new一個FirefoxDriverNavigationnavigation=driver.navigate();//進入今日頭條首頁navigation.to("/");(2)這種情況適用于Firefox未安裝在默認路徑下:System.out.println("startfirefoxbrowser...");System.setProperty("webdriver.firefox.bin","D:/ProgramFiles/MozillaFirefox/firefox.exe");//指定firefox的安裝路徑WebDriverdriver=newFirefoxDriver();Navigationnavigation=driver.navigate();navigation.to("/");(3)這種情況可以加載出Firefox的插件。首先,要知道我們?yōu)槭裁葱枰虞d插件?原因是webdriver在啟動瀏覽器時,啟動的一個干凈的沒有任務(wù)、插件及cookies信息的瀏覽器(即使你本機的firefox安裝了某些插件,webdriver啟動firefox也是沒有這些插件的),但是有可能被測系統(tǒng)本身需要插件或者需要調(diào)試等等,此時可以用如下方法在啟動firefox時加載插件,下面示例加載firebug插件:importjava.io.File;importjava.io.IOException;importorg.openqa.selenium.Alert;importorg.openqa.selenium.WebDriver;importorg.openqa.selenium.WebDriver.Navigation;importorg.openqa.selenium.firefox.FirefoxDriver;importorg.openqa.selenium.firefox.FirefoxProfile;publicclassTestDemo{publicstaticvoidmain(String[]args){System.out.println("startfirefoxbrowser...");System.setProperty("webdriver.firefox.bin","C:/ProgramFiles(x86)/MozillaFirefox/firefox.exe");Filefile=newFile("/files/firebug-2.0.7-fx.xpi");FirefoxProfileprofile=newFirefoxProfile();try{profile.addExtension(file);}catch(IOExceptione){e.printStackTrace();}profile.setPreference("extensions.firebug.currentVersion","2.0.7");//activefirebugextensionsprofile.setPreference("extensions.firebug.allPagesActivation","on");WebDriverdriver=newFirefoxDriver(profile);driver.get("/");System.out.println("startfirefoxbrowsersucceed...");}}上述代碼并未調(diào)通,報如下異常:startfirefoxbrowser...Exceptioninthread"main"org.openqa.selenium.WebDriverException:FailedtoconnecttobinaryFirefoxBinary(C:\ProgramFiles(x86)\MozillaFirefox\firefox.exe)onport7055;processoutputfollows:nullCausedby:

org.openqa.selenium.firefox.UnableToCreateProfileException:

java.io.FileNotFoundException:\files\firebug-2.0.7-fx.xpi(系統(tǒng)找不到指定的路徑。)Causedby:

java.io.FileNotFoundException:\files\firebug-2.0.7-fx.xpi(系統(tǒng)找不到指定的路徑。)(4)用第(3)種情況未調(diào)通。每次啟動如果都像上面那樣在代碼里面配置profile比較麻煩,可以使用下面的方法啟動本機器的firefox的配置,換句話說就是我們可以事先配置本機的firefox然后用webdriver啟動它,這樣本機上的firefox安裝了什么插件都可以直接使用了,不需要在配置profile:publicstaticvoidmain(String[]args){System.out.println("startfirefoxbrowser...");System.setProperty("webdriver.firefox.bin","C:/ProgramFiles(x86)/MozillaFirefox/firefox.exe");ProfilesInipi=newProfilesIni();FirefoxProfileprofile=pi.getProfile("default");WebDriverdriver=newFirefoxDriver(profile);driver.get("/");System.out.println("startfirefoxbrowsersucceed...");}二、啟動IEBrowser。PS:支持有三種不同的OS平臺,包括Windows、Linux、MacOS。(1)啟動本地IEBrowser。System.setProperty("webdriver.ie.driver","E:\\selenium\\IEDriverServer_x64_2.53.0\\IEDriverServer.exe");//IEDriverServer.exe所在本地路徑DesiredCapabilitiesieCapabilities=DesiredCernetExplorer();ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);WebDriverdriver=newInternetExplorerDriver(ieCapabilities);//進入今日頭條首頁driver.get("/");三、啟動ChromeBrowser。PS:只能在WindowsOS平臺上運行,但要區(qū)別32bit版本和64bit版本。(1)啟動本地ChromeBrowser。publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubSystem.setProperty("webdriver.chrome.driver","E:\\chromedriver_win32\\chromedriver.exe");//chromedriver.exe所在本地路徑WebDriverdriver=newChromeDriver();driver.get("/");driver.findElement(By.id("kw")).sendKeys(Keys.chord(Keys.SHIFT,"webdriver"));driver.findElement(By.id("su")).click();driver.close();}note:publicstaticvoidmain(String[]args){System.setProperty("webdriver.firefox.bin","C:/ProgramFiles(x86)/MozillaFirefox/firefox.exe");//設(shè)置DesiredCapabilities的屬性包含E_ENSURE_CLEAN_SESSION以確保在Browser實例啟動前清理會話的臟數(shù)據(jù)。Desir

溫馨提示

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

評論

0/150

提交評論