Skip to content

Commit

Permalink
[java]: add docs and example for setNetworkConditions (#2071)[deploy …
Browse files Browse the repository at this point in the history
…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>
  • Loading branch information
navin772 and harsha509 authored Nov 22, 2024
1 parent 6c18cac commit 5b50534
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}
}
29 changes: 28 additions & 1 deletion examples/java/src/test/java/dev/selenium/browsers/EdgeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" >}}
Expand Down

0 comments on commit 5b50534

Please sign in to comment.