From 5b505342dee60d0a13fd03b6202b82311c0501b7 Mon Sep 17 00:00:00 2001 From: Navin Chandra <98466550+navin772@users.noreply.github.com> Date: Fri, 22 Nov 2024 23:35:13 +0530 Subject: [PATCH] [java]: add docs and example for setNetworkConditions (#2071)[deploy site] * add example for java setNetworkConditions in chrome and edge * java docs for setNetworkConditions * delete network conditions after assertions --------- Co-authored-by: Sri Harsha <12621691+harsha509@users.noreply.github.com> --- .../dev/selenium/browsers/ChromeTest.java | 28 +++++++++++++++++- .../java/dev/selenium/browsers/EdgeTest.java | 29 ++++++++++++++++++- .../webdriver/browsers/chrome.en.md | 2 +- .../webdriver/browsers/chrome.ja.md | 2 +- .../webdriver/browsers/chrome.pt-br.md | 2 +- .../webdriver/browsers/chrome.zh-cn.md | 2 +- .../webdriver/browsers/edge.en.md | 2 +- .../webdriver/browsers/edge.ja.md | 2 +- .../webdriver/browsers/edge.pt-br.md | 2 +- .../webdriver/browsers/edge.zh-cn.md | 2 +- 10 files changed, 63 insertions(+), 10 deletions(-) diff --git a/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java b/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java index 430ae5d1639..24e97185336 100644 --- a/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java +++ b/examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java @@ -19,12 +19,12 @@ import org.openqa.selenium.chrome.ChromeDriverService; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chromium.ChromiumDriverLogLevel; +import org.openqa.selenium.chromium.ChromiumNetworkConditions; import org.openqa.selenium.logging.LogEntries; import org.openqa.selenium.logging.LogType; import org.openqa.selenium.logging.LoggingPreferences; import org.openqa.selenium.remote.service.DriverFinder; - public class ChromeTest extends BaseTest { @AfterEach public void clearProperties() { @@ -196,4 +196,30 @@ public void setPermission() { Assertions.assertEquals("denied", permissionState); driver.quit(); } + + @Test + public void setNetworkConditions() { + driver = new ChromeDriver(); + + ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions(); + networkConditions.setOffline(false); + networkConditions.setLatency(java.time.Duration.ofMillis(20)); // 20 ms of latency + networkConditions.setDownloadThroughput(2000 * 1024 / 8); // 2000 kbps + networkConditions.setUploadThroughput(2000 * 1024 / 8); // 2000 kbps + + ((ChromeDriver) driver).setNetworkConditions(networkConditions); + + driver.get("https://www.selenium.dev"); + + // Assert the network conditions are set as expected + ChromiumNetworkConditions actualConditions = ((ChromeDriver) driver).getNetworkConditions(); + Assertions.assertAll( + () -> Assertions.assertEquals(networkConditions.getOffline(), actualConditions.getOffline()), + () -> Assertions.assertEquals(networkConditions.getLatency(), actualConditions.getLatency()), + () -> Assertions.assertEquals(networkConditions.getDownloadThroughput(), actualConditions.getDownloadThroughput()), + () -> Assertions.assertEquals(networkConditions.getUploadThroughput(), actualConditions.getUploadThroughput()) + ); + ((ChromeDriver) driver).deleteNetworkConditions(); + driver.quit(); + } } diff --git a/examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java b/examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java index 164d73b0aa0..11aa4f13f1e 100644 --- a/examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java +++ b/examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java @@ -15,7 +15,9 @@ import org.junit.jupiter.api.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chromium.ChromiumDriverLogLevel; +import org.openqa.selenium.chromium.ChromiumNetworkConditions; import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeDriverService; import org.openqa.selenium.edge.EdgeOptions; @@ -24,7 +26,6 @@ import org.openqa.selenium.logging.LoggingPreferences; import org.openqa.selenium.remote.service.DriverFinder; - public class EdgeTest extends BaseTest { @AfterEach public void clearProperties() { @@ -190,4 +191,30 @@ public void setPermissions() { Assertions.assertEquals("denied", permissionState); driver.quit(); } + + @Test + public void setNetworkConditions() { + driver = new EdgeDriver(); + + ChromiumNetworkConditions networkConditions = new ChromiumNetworkConditions(); + networkConditions.setOffline(false); + networkConditions.setLatency(java.time.Duration.ofMillis(20)); // 20 ms of latency + networkConditions.setDownloadThroughput(2000 * 1024 / 8); // 2000 kbps + networkConditions.setUploadThroughput(2000 * 1024 / 8); // 2000 kbps + + ((EdgeDriver) driver).setNetworkConditions(networkConditions); + + driver.get("https://www.selenium.dev"); + + // Assert the network conditions are set as expected + ChromiumNetworkConditions actualConditions = ((EdgeDriver) driver).getNetworkConditions(); + Assertions.assertAll( + () -> Assertions.assertEquals(networkConditions.getOffline(), actualConditions.getOffline()), + () -> Assertions.assertEquals(networkConditions.getLatency(), actualConditions.getLatency()), + () -> Assertions.assertEquals(networkConditions.getDownloadThroughput(), actualConditions.getDownloadThroughput()), + () -> Assertions.assertEquals(networkConditions.getUploadThroughput(), actualConditions.getUploadThroughput()) + ); + ((EdgeDriver) driver).deleteNetworkConditions(); + driver.quit(); + } } diff --git a/website_and_docs/content/documentation/webdriver/browsers/chrome.en.md b/website_and_docs/content/documentation/webdriver/browsers/chrome.en.md index 462899b356c..6e4f28c6a71 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/chrome.en.md +++ b/website_and_docs/content/documentation/webdriver/browsers/chrome.en.md @@ -396,7 +396,7 @@ You can simulate various network conditions. {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L204-L210" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L129-L135" >}} diff --git a/website_and_docs/content/documentation/webdriver/browsers/chrome.ja.md b/website_and_docs/content/documentation/webdriver/browsers/chrome.ja.md index 73053fe3207..241fbb3162b 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/chrome.ja.md +++ b/website_and_docs/content/documentation/webdriver/browsers/chrome.ja.md @@ -390,7 +390,7 @@ Chrome Castデバイスを操作することができ、タブの共有も含ま {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L204-L210" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L129-L135" >}} diff --git a/website_and_docs/content/documentation/webdriver/browsers/chrome.pt-br.md b/website_and_docs/content/documentation/webdriver/browsers/chrome.pt-br.md index 8a96b3ac18d..e273d7f813a 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/chrome.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/browsers/chrome.pt-br.md @@ -399,7 +399,7 @@ please refer to the {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L204-L210" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L129-L135" >}} diff --git a/website_and_docs/content/documentation/webdriver/browsers/chrome.zh-cn.md b/website_and_docs/content/documentation/webdriver/browsers/chrome.zh-cn.md index e86557c85b4..cf2dde60719 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/chrome.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/browsers/chrome.zh-cn.md @@ -399,7 +399,7 @@ Chromedriver 和 Chrome 浏览器版本应该匹配, 如果它们不匹配, 驱 {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/ChromeTest.java#L204-L210" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_chrome.py#L129-L135" >}} diff --git a/website_and_docs/content/documentation/webdriver/browsers/edge.en.md b/website_and_docs/content/documentation/webdriver/browsers/edge.en.md index 04768b23e45..2bcae96e363 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/edge.en.md +++ b/website_and_docs/content/documentation/webdriver/browsers/edge.en.md @@ -397,7 +397,7 @@ You can simulate various network conditions. {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java#L198-L204" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_edge.py#L129-L135" >}} diff --git a/website_and_docs/content/documentation/webdriver/browsers/edge.ja.md b/website_and_docs/content/documentation/webdriver/browsers/edge.ja.md index ff9237645e6..cf8098c5581 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/edge.ja.md +++ b/website_and_docs/content/documentation/webdriver/browsers/edge.ja.md @@ -383,7 +383,7 @@ Edge を使用して Chrome Cast デバイスを操作し、タブを共有す {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java#L198-L204" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_edge.py#L129-L135" >}} diff --git a/website_and_docs/content/documentation/webdriver/browsers/edge.pt-br.md b/website_and_docs/content/documentation/webdriver/browsers/edge.pt-br.md index ce8bbb9505b..23e7599d04d 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/edge.pt-br.md +++ b/website_and_docs/content/documentation/webdriver/browsers/edge.pt-br.md @@ -399,7 +399,7 @@ You can simulate various network conditions. {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java#L198-L204" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_edge.py#L129-L135" >}} diff --git a/website_and_docs/content/documentation/webdriver/browsers/edge.zh-cn.md b/website_and_docs/content/documentation/webdriver/browsers/edge.zh-cn.md index a6d3aaa67de..cce5a9e4e36 100644 --- a/website_and_docs/content/documentation/webdriver/browsers/edge.zh-cn.md +++ b/website_and_docs/content/documentation/webdriver/browsers/edge.zh-cn.md @@ -399,7 +399,7 @@ You can simulate various network conditions. {{< tabpane text=true >}} {{< tab header="Java" >}} -{{< badge-code >}} +{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java#L198-L204" >}} {{< /tab >}} {{< tab header="Python" >}} {{< gh-codeblock path="/examples/python/tests/browsers/test_edge.py#L129-L135" >}}