Transcripted Summary

In this chapter, we will slow down our internet connection speed to emulate a network condition. The purpose is to see how our AUT performs under adverse conditions.



Right now, there are no requests in the network panel. It's not available because we must open the panel before performing an action. That's how we secure the network traffic data.

Refresh the page, and we see all the network activity in the network log. Each row in the network log represents a resource.



This Network Throttling dropdown is how we emulate different connection speeds.

The presets are Fast 3G, Slow 3G, and Offline.



Select any one of these presets and a Warning icon shows up in the network tab.

Let's select Offline and we see the Warning icon.

Watch what happens when I refresh the page.



The page shows you are not connected.

Now, watch what happens when I slow down and select Slow 3G. It will load but load really slow. See the Slow icon.

Now watch what happens when I select Fast 3G, it will load much faster.

Refresh, and that's how we emulate network connection speeds.

# Test Setup

Now let's go to our test script and load the ChromeDriver; also DevTools.


  ChromeDriver driver;
  DevTools devTools; 

We're going to import ChromeDriver and DevTools.

Now it's time to set up our tests with @BeforeMethod public void setup() and then WebDriverManager.chromedriver().setup().

I'm going to type in driver = new ChromeDriver().

Now it's time to maximize the window by writing driver.manage().window().maximize()

At this point, we are not going to load the AUT, but I will write devTools = driver.getDevTools()..


  @BeforeMethod
  public void setUp(){
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    devTools = driver.getDevTools();
  }

# Test - Emulating a Slow Network Condition

Enabling the network to slow down is next. And we're going to enable the network to slow down after I write the @Test annotation, public void. How about we write, “enableSlowRexJonesII()”?

Now we are going to create a session, and we always start with devTools.createSession().

At this point, we're going to send a command to enable the network by writing devTools.send(); then pass in Network.enable.

I'm going to skip a line.

At this point, I will pass in Optional.empty() 3 times. So, I'm going to write a comma, then copy this statement and paste it 2 more times. Yes.



Now we have made it possible to deliver network tracking and events to the client.

Next is to emulate the network conditions.

Let's go to GitHub for the method and parameters, search for “emulate”. And we see at the bottom Network.emulateNetworkConditions. It activates emulation of network conditions.

I'm going to click the Network.emulateNetworkConditions to see the details.



The parameters are offline, latency, downloadThroughput, uploadThroughput, and connectionType. Connection type is the only optional parameter.

The method that we wrote for enabling the network has 3 optional parametersmaxTotalBufferSize, maxResourceBufferSize, and maxPostDataSize.



That's why I made them Optional.empty().

Do you see how the first 2 parameters are experiments? If I hover, the tooltip says this may be changed, moved, or removed.

Now let's go to the IDE to complete the test script.

We want the network to stay online. So, since we want the network to stay online, we're going to write devTools.send(Network.emulateNetworkConditions())

  • For the network to stay online, I'm going to write “false”. And that's how we keep the network online because offline equals false.
  • Next is the latency, and that will be set to 150.
  • Download throughput will be 2500.
  • Upload throughput will be 2000.
  • Next is the connection type. So, let's write Optional.of(ConnectionType.)). And we see different options. We have Bluetooth, 2G, 3G, 4G and Wi-Fi. Let's select “CELLULAR3G”.



And with 3G as the selection, the next step is to load the site — driver.get("") and pass in the URL of “https://rexjones2.com”.

Now let's print the title. How about we make a message that says "Enable Slow Network: " + driver.getTitle())


  @Test
  public void enableSlowRexJonesII(){
  devTools.createSession();
  devTools.send(Network.enable(
          Optional.empty(),
          Optional.empty(),
          Optional.empty()));
  devTools.send(Network.emulateNetworkConditions(
          false,
          150,
          2500,
          2000,
          Optional.of(ConnectionType.CELLULAR3G)));
  driver.get("https://rexjones2.com");
    System.out.println("Enable Slow Network: " + driver.getTitle());
  }

# Test 2 – Do Not Enable Network Emulation

Let's compare the time between enabling a slow network and loading a site without enabling the network.

So, the next test will be @Test public void, how about “doNotEnableRexJonesII()”

Now we're going to load the application and print the title.

And this time let's write "Do Not Enable Network: " + driver.getTitle())


  @Test
  public void doNotEnableRexJonesII(){
    driver.get("https://RexJones2.com");
    System.out.println("Do Not Enable Network: " + driver.getTitle());
  }

Now let's run from the class level to execute both tests at the same time.

Okay. So, we see the first test passed, which was the one for “Do Not Enable Network”. It was the second test, but the first one to pass.



Yes, so the Run window shows the title and the time it took to load. It loaded in 1 second 780 milliseconds. And the title is, “Do Not Enable Network: Rex Jones II”.

The first test is taking much longer to load, “enableSlowRexJonesII”.

So, let's see that we still see it loading. Now it may take some time to finish, so I will speed up the video.

Okay, let's go to the Run window.



Let's see why it failed. It failed for a timeout exception.

Let's use a different site. How about we use LinkedIn as the new site?

And I'm going to update only 1 test, the one that failed (“enableSlowRexJonesII”).


  driver.get("https://linkedin.com");

I'm going to run both tests again and see what happens.

Once again, the test that did not have the network enabled passed. And it loaded in 1 second and 345 milliseconds.

I hope this test passes for LinkedIn. We'll see what happens.

Okay. It's looking good. Bingo.

Let's see if it passed.



Yes, it passed. And it shows the title “LinkedIn: Log In or Sign Up”.

That was a good test because it shows how my site did not load under 3G, but LinkedIn loaded under 3G without a problem.

We see it took a little bit longer because it took 1 minute, 0 seconds and 762 milliseconds.

Okay. So, both tests show the page title.

That's it for emulating a network condition by slowing down the network.

# Thank you for watching Selenium 4 with Java.

Thanks again to Angie, Test Automation University, and Applitools.

My plan is to continue writing books, blogs, and creating videos. You can watch me on and find me on LinkedIn, YouTube, Facebook, and Twitter.

The Git Hub will show all the source code, everyone for each chapter.

Thanks again, take care and I wish you much success.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR