[跟著官方文檔學Selenium][學習筆記][九][WebDriver的雙向協議]

      網友投稿 1089 2022-05-29

      Selenium正在與瀏覽器供應商合作創建WebDriver雙向協議,作為一種提供穩定的跨瀏覽器API的方法,該API使用雙向協議處理各種瀏覽器的通用自動化以及特定測試的需求。在此之前,尋求此功能的用戶必須忍受當前實現的全部問題和局限。

      嚴格限制請求響應命令的傳統WebDriver模型,將從user agent轉變為基于WebSockets的軟件控制,通過這樣完善流事件的能力,以便更好地匹配瀏覽器DOM地事件性質。

      因為將測試受限于特定瀏覽器的特定版本是個壞主意,Selenium項目建議盡可能使用WebDriver BiDi。然而,在規范完成之前,CDP提供了許多有用的東西。為了幫助保持測試的獨立性和可移植性,Selenium提供了一些有用的輔助類。目前,這些應用程序使用CDP,但我們將盡快提供WebDriver Bidi的實現。

      Chrome開發工具

      雖然Selenium 4提供了對Chrome DevTools Protocol(CDP)的直接訪問,但是仍非常鼓勵您使用WebDriver Bidi APIs代替

      許多瀏覽器都提供開發者工具-一組與瀏覽器集成的工具,開發人員可以用其調試Web應用程序并探索其頁面的性能。谷歌瀏覽器開發工具(使用一種稱為Chrome DevTools Protocol)簡稱CDP的協議。顧名思義,這不是為測試而設計的,而并沒有一個穩定的API,所以它的功能高度依賴于瀏覽器的版本。

      WebDriver Bidi是W3C WebDriver的下一代協議,旨在提供由所有瀏覽器實現穩定的API,但尚未完成。在此之前,Selenium提供了通過CDP實現的方式 (諸如Google Chrome或Microsoft Edge,以及Firefox),允許您以有趣的方式增強測試。下面給出了實際使用的例子。

      模擬地理位置

      一些應用程序在不同的位置具有不同的特性和功能。自動化此類應用程序很難,因為很難使用Selenium在瀏覽器中模擬地理位置。但是在Devtools的幫助下,我們可以輕易模擬他們。下面的代碼片段演示了這一點。

      import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.v97.emulation.Emulation; import org.openqa.selenium.edge.EdgeDriver; import java.util.Optional; public class demo1 { public static void main(String[] args) { EdgeDriver webDriver = new EdgeDriver(); DevTools devTools=webDriver.getDevTools(); devTools.createSession(); devTools.send(Emulation.setGeolocationOverride(Optional.of(53.5043), Optional.of(13.4501), Optional.of(1))); webDriver.get("https://my-location.org/"); } }

      通過遠程WebDriver模擬地理位置

      import org.openqa.selenium.WebDriver; import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.HasDevTools; import org.openqa.selenium.devtools.v97.emulation.Emulation; import org.openqa.selenium.edge.EdgeOptions; import org.openqa.selenium.remote.Augmenter; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.MalformedURLException; import java.net.URL; import java.util.Optional; public class demo2 { public static void main(String[] args) throws MalformedURLException { EdgeOptions edgeOptions = new EdgeOptions(); WebDriver webDriver = new RemoteWebDriver(new URL(""),edgeOptions); webDriver = new Augmenter().augment(webDriver); DevTools devTools = ((HasDevTools) webDriver).getDevTools(); devTools.createSession(); devTools.send(Emulation.setGeolocationOverride(Optional.of(52.5043), Optional.of(13.4501), Optional.of(1))); webDriver.get("https://my-location.org/"); } }

      覆蓋設備模式

      使用Selenium與CDP的集成,可以覆蓋當前設備模式并模擬新模式。Width,height,mobile和deviceScaleFactor是必需的參數。可選參數包括scale,screenWidth,screenHeight,positionX,positionY,dontSetVisible,screenOrientation,viewport和displayFeature。

      import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.v97.emulation.Emulation; import org.openqa.selenium.edge.EdgeDriver; import java.util.Optional; public class demo3 { public static void main(String[] args) { EdgeDriver edgeDriver = new EdgeDriver(); DevTools devTools=edgeDriver.getDevTools(); devTools.createSession(); // iPhone 11 Pro dimensions devTools.send(Emulation.setDeviceMetricsOverride(375, 812, 50, true, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty())); edgeDriver.get("https://www.selenium.dev"); edgeDriver.quit(); } }

      [跟著官方文檔學Selenium][學習筆記][九][WebDriver的雙向協議]

      Collect Performance Metrics

      在應用導航的期間收集性能數據

      import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.v97.performance.Performance; import org.openqa.selenium.devtools.v97.performance.model.Metric; import org.openqa.selenium.edge.EdgeDriver; import java.util.List; import java.util.Optional; public class performanceMetricsExample { public static void main(String[] args) { EdgeDriver webDriver = new EdgeDriver(); DevTools devTools=webDriver.getDevTools(); devTools.createSession(); devTools.send(Performance.enable(Optional.empty())); List metricList = devTools.send(Performance.getMetrics()); webDriver.get("https://www.baidu.com"); webDriver.quit(); for (Metric m : metricList) { System.out.println(m.getName()+" = "+m.getValue()); } } }

      雙向接口

      以下這些API列表隨著Selenium項目通過支持真實用例而增長。

      Register Basic Auth

      某些應用程序使用瀏覽器身份驗證來保護頁面。使用Selenium,可以在出現基本身份驗證憑據時自動輸入它們。

      import org.openqa.selenium.HasAuthentication; import org.openqa.selenium.UsernameAndPassword; import org.openqa.selenium.edge.EdgeDriver; import java.net.URI; import java.util.function.Predicate; public class registerBasicAuth { public static void main(String[] args) { EdgeDriver edgeDriver = new EdgeDriver(); Predicate uriPredicate = uri -> uri.getHost().contains("your-domain.com"); ((HasAuthentication) edgeDriver).register(uriPredicate, UsernameAndPassword.of("username", "password")); edgeDriver.get("https://your-domain.com/login"); } }

      Listen to console.log events

      監聽console.log事件并注冊回調以處理事件

      import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.devtools.v97.log.Log; import org.openqa.selenium.edge.EdgeDriver; public class listenToConsole { public static void main(String[] args) { EdgeDriver driver = new EdgeDriver(); DevTools devTools = driver.getDevTools(); devTools.createSession(); devTools.send(Log.enable()); devTools.addListener(Log.entryAdded(), logEntry -> { System.out.println("log: "+logEntry.getText()); System.out.println("level: "+logEntry.getLevel()); }); driver.get("http://the-internet.herokuapp.com/broken_images"); driver.quit(); } }

      Listen to JS Exceptions

      監聽JS Exceptions并注冊回調以處理Exception

      import org.openqa.selenium.By; import org.openqa.selenium.JavascriptException; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebElement; import org.openqa.selenium.devtools.DevTools; import org.openqa.selenium.edge.EdgeDriver; import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; public class listenToJS { public static void main(String[] args) { EdgeDriver driver = new EdgeDriver(); DevTools devTools = driver.getDevTools(); devTools.createSession(); List jsExceptionsList = new ArrayList<>(); Consumer addEntry = jsExceptionsList::add; devTools.getDomains().events().addJavascriptExceptionListener(addEntry); driver.get(""); WebElement link2click = driver.findElement(By.linkText("")); ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", link2click, "onclick", "throw new Error('Hello, world!')"); link2click.click(); for (JavascriptException jsException : jsExceptionsList) { System.out.println("JS exception message: " + jsException.getMessage()); System.out.println("JS exception system information: " + jsException.getSystemInformation()); jsException.printStackTrace(); } } }

      Network Interception

      如果要捕獲進入瀏覽器的網絡事件并希望對其進行操作,則可以使用以下示例進行操作。

      import com.google.common.net.MediaType; import junit.framework.TestCase; import org.hamcrest.CoreMatchers; import org.hamcrest.Matcher; import org.hamcrest.MatcherAssert; import org.junit.jupiter.api.Assertions; import org.openqa.selenium.devtools.NetworkInterceptor; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.remote.http.Route; import org.openqa.selenium.WebDriver; import org.openqa.selenium.devtools.HasDevTools; import org.openqa.selenium.devtools.NetworkInterceptor; import org.openqa.selenium.remote.http.Contents; import org.openqa.selenium.remote.http.Filter; import org.openqa.selenium.remote.http.HttpResponse; import org.openqa.selenium.remote.http.Route; import static org.openqa.selenium.remote.http.Contents.utf8String; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.is; public class networkInterception { public static void main(String[] args) { WebDriver driver = new EdgeDriver(); NetworkInterceptor interceptor = new NetworkInterceptor( driver, Route.matching(req -> true) .to(() -> req -> new HttpResponse() .setStatus(200) .addHeader("Content-Type", MediaType.HTML_UTF_8.toString()) .setContent(utf8String("Creamy, delicious cheese!")))); driver.get("https://example-sausages-site.com"); String source = driver.getPageSource(); assertThat(source, CoreMatchers.containsString("delicious cheese!")); } }

      Selenium

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:2020最新的微信官方域名檢測API接口(含調用文檔)
      下一篇:使用代碼創建CRM服務請求文檔的subject測試數據
      相關文章
      亚洲综合AV在线在线播放| 国产精品亚洲综合一区| 亚洲精品无码精品mV在线观看| 国产亚洲福利一区二区免费看| 性色av极品无码专区亚洲| 亚洲第一成年免费网站| 亚洲风情亚Aⅴ在线发布| 亚洲午夜精品久久久久久app| 精品丝袜国产自在线拍亚洲| 亚洲一区在线观看视频| 亚洲精品欧洲精品| 亚洲网址在线观看| 亚洲欧洲日本精品| 亚洲av专区无码观看精品天堂| 亚洲伊人久久大香线焦| 亚洲AV色吊丝无码| 中文日韩亚洲欧美制服| 亚洲另类无码一区二区三区| 亚洲人成电影网站免费| 亚洲AV无码专区在线电影成人| 亚洲Av永久无码精品一区二区| 国产精品亚洲综合天堂夜夜| 亚洲精品成人区在线观看| 亚洲国产成人久久综合一区77 | 亚洲网站在线播放| 亚洲国产综合自在线另类| 亚洲区视频在线观看| 亚洲精品永久在线观看| jjzz亚洲亚洲女人| 国产成人综合亚洲AV第一页| 亚洲国产精品久久久天堂| 亚洲av无码成h人动漫无遮挡 | 在线电影你懂的亚洲| 亚洲熟妇色自偷自拍另类| 久久精品国产亚洲av麻豆图片| 亚洲中文无码永久免费| 国产精品亚洲精品日韩动图| AV在线播放日韩亚洲欧| 亚洲αv在线精品糸列| 亚洲精品国产第1页| 欧洲 亚洲 国产图片综合|