模拟浏览器自动化测试工具Selenium之一eclipse集成开发部署篇
1、背景:在網頁自動化測試和網頁表單自動交互的場景中,對動態js頁面的加載,隱藏鏈接爬蟲和表單元素需要加載js來解析。
? ? ? ? ? ? ? ? ? htmlunit相比較于htmlparser以及httpclient只能解析靜態頁面來說,可以支持動態js頁面的解析,但對js支持不是很好,如angularjs,因此需采用模擬瀏覽器的工具。
? ? ? ? ? ? ? ? ? ?Selenium模擬瀏覽器:提供一組API和真實的瀏覽器內核交互。
2、部署:
? ?selenium下載:http://selenium-release.storage.googleapis.com/index.html
下載:selenium-java-2.53.1.zip(jar包引入eclipse的java工程)和IEDriverServer_x64_2.53.1.zip(對應本機操作系統是64位的IE瀏覽器啟動)。
兩點注意:1)發現最新版本3.0.1和jdk1.7不兼容(本機安裝是jdk1.7),所以更換回2.53.1版本。2)提示保護模式錯誤時,可通過IE瀏覽器設置(貌似效果不好),也可以在代碼上設置。
Internet Option in IE –> Security–>Check or Unchecked “Enable Protected Mode.”?
代碼中設置見案例代碼。IE瀏覽器保護模式錯誤提示如下:
Started InternetExplorerDriver server (64-bit) 2.53.1.0 Listening on port 36078 Only local connections are allowed Exception: org.openqa.selenium.remote.SessionNotFoundException: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 840 milliseconds Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46' System info: host: 'ICD-PC', ip: '132.97.194.17', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_79' Driver info: org.openqa.selenium.ie.InternetExplorerDriver3、案例:
package com.test;import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait;public class BrasShopYP {//selenium下載:http://selenium-release.storage.googleapis.com/index.htmlpublic static void main(String[] args) {try {System.getProperties().setProperty("webdriver.ie.driver","D:\\dev\\workspace\\ocweb\\libs\\IEDriverServer.exe");//System.setProperty("webdriver.ie.bin", "C:\\Program Files\\Internet Explorer\\iexplore.exe"); //WebDriver webDriver = new InternetExplorerDriver();DesiredCapabilities caps = DesiredCapabilities.internetExplorer();caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);WebDriver webDriver = new InternetExplorerDriver(caps);//訪問網址webDriver.get("http://www.baidu.com");// 獲取 網頁的 titleSystem.out.println("1 Page title is: " + webDriver.getTitle());// 通過 id 找到 input 的 DOMWebElement element = webDriver.findElement(By.id("kw"));// 輸入關鍵字element.sendKeys("zTree");// 提交 input 所在的 formelement.submit(); // 通過判斷 title 內容等待搜索頁面加載完畢,Timeout 設置50秒//(new WebDriverWait(webDriver, 50)).until(new ExpectedCondition<Boolean>() {// public Boolean apply(WebDriver d) {// return d.getTitle().toLowerCase().endsWith("ztree");// }//});// 顯示搜索結果頁面的 titleSystem.out.println("2 Page title is: " + webDriver.getTitle());// 關閉窗口,釋放資源。webDriver.close();}catch (Exception e) {System.err.println( "Exception: " + e ); }} }下步就要研究selenium如何提取頁面鏈接爬蟲并實現表單自動填寫交互。
總結
以上是生活随笔為你收集整理的模拟浏览器自动化测试工具Selenium之一eclipse集成开发部署篇的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 离线轻量级大数据平台Spark之读取CS
- 下一篇: 模拟浏览器自动化测试工具Selenium