[跟著官方文檔學Selenium][學習筆記][九][WebDriver的雙向協議]
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("
覆蓋設備模式
使用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(); } }
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
雙向接口
以下這些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
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
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小時內刪除侵權內容。