Selenium Automation with Moxproxy for Functional and Front End Performance Testing — Through Analyzing Network Calls

Hansani Jayasekara
4 min readJul 23, 2020

Analyzing browser network calls is very helpful in testing a website. That gives important information when it comes to Functional and Front End Performance. In day today testing we can do that using a proxy like fiddler or simply by using browser developer tools. But that is a manual process. What if we can automate that browser network call analysis!!!! Given bellow is a brilliant solution to automate browser network calls analysis in few easy steps

What is Moxproxy

Moxproxy is java library written on the top of LittleProxy library to support automated testing by controlling network traffic between http client and server or application components.

Writing your first code with MoxProxy

  1. Create a maven project

2. Add the Maven Selenium dependency to the pom.xml file from here

The latest dependency as of now

<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version></dependency>

3. Add the Maven TestNG dependency to the pom.xml file from here

The latest dependency as of now

<!-- https://mvnrepository.com/artifact/org.testng/testng --><dependency><groupId>org.testng</groupId><artifactId>testng</artifactId><version>7.1.0</version><scope>test</scope></dependency>

4. Add the Maven MoxProxy Core dependency to the pom.xml file from here

The latest dependency as of now

<!-- https://mvnrepository.com/artifact/com.moxproxy/moxproxy.core --><dependency><groupId>com.moxproxy</groupId><artifactId>moxproxy.core</artifactId><version>1.1.2</version></dependency>

5. Add LocalProxyBuilder class to your project. This class is responsible for establishing the local proxy. Add the following method to your class.

public MoxProxy buildproxy() {
MoxProxy proxy = LocalMoxProxy.builder()
.withPort(8089)
.withRecorderWhiteList(Collections.singletonList(URL))
.build();
return proxy;
}

6. Add DriverBuilder class to your project. This class is responsible for handling the driver of the desired browser. For this example, I am using Mozilla Firefox browser. Add the following method to your class.

public WebDriver buildDriver() {

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", PROXY_PORT);
profile.setPreference("network.proxy.ssl", "localhost");
profile.setPreference("network.proxy.ssl_port", PROXY_PORT);
profile.setPreference("network.proxy.socks", "localhost");
profile.setPreference("network.proxy.socks_port", PROXY_PORT);
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
FirefoxOptions options = new FirefoxOptions();
options.setLogLevel(FirefoxDriverLogLevel.FATAL);
options.setProfile(profile);

options.addArguments("start-maximized");
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);
System.out.println("starting..");
System.setProperty("webdriver.gecko.driver","Your_ gecko_ driver_ location"); // Setting system properties of FirefoxDriver
System.setProperty("webdriver.firefox.bin", "Firefox_exe_location"); // Setting system properties of
driver = new FirefoxDriver(capabilities); //Creating an object of FirefoxDriver
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(400, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(300, TimeUnit.SECONDS);

return driver;

}
·
  • Pass the port of your desire to PROXY_PORT variable
  • Replace · ‘gecko_ driver_ location’ with your gecko driver location.Eg: “D:\\SW\\geckodriver.exe”
  • Replace ·‘Firefox_exe_location’ with your firefox.exe location

7. Add a test class to your project and start writing your first moxproxy test !!!

  • Add the following method to start the proxy and the driver
· @BeforeClass
public void startServer() {
LocalProxyBuilder localproxybuilder = new LocalProxyBuilder();
proxy = localproxybuilder.buildproxy();
proxy.startServer();

DriverBuilder driverBuilder = new DriverBuilder();
driver = driverBuilder.buildDriver();
}
  • Retrieving recorded traffic
driver.get(URL);List<MoxProxyProcessedTrafficEntry> requestTraffic = proxy.getAllRequestTraffic();
List<MoxProxyProcessedTrafficEntry> responseTraffic = proxy.getAllResponseTraffic();
  • Sample test method to check the title.

In following example https://www.wikipedia.org/ is used as system under test

public void testWiki() {

driver.get(URL);

try {
Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(expectation);
} catch (Throwable error) {
Assert.fail("Timeout waiting for Page Load Request to complete.");
}


List<MoxProxyProcessedTrafficEntry> requestTraffic = proxy.getAllRequestTraffic();
List<MoxProxyProcessedTrafficEntry> responseTraffic = proxy.getAllResponseTraffic();

System.out.println(requestTraffic.size());
String expectedTitle = "Wikipedia";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
  • When all the tests are done stop the server and quit the driver
@AfterClass
public void stopServermethod() {
proxy.stopServer();
driver.quit();
}

Some interesting things you can do with Moxproxy

Functional tests

  • Get all the requests for a specific page load
System.out.println("Here comes the list of request URLs ###################");

for (MoxProxyProcessedTrafficEntry moxProxyProcessedTrafficEntry : requestTraffic) {

System.out.println( moxProxyProcessedTrafficEntry.getUrl());
}
  • Get all the responses for a specific page load
System.out.println("Here comes the list of responce URLs ###################" );

for (MoxProxyProcessedTrafficEntry moxProxyProcessedTrafficEntry : responseTraffic) {

System.out.println( moxProxyProcessedTrafficEntry.getUrl());
}
  • Get the timestamps of the request URLs
System.out.println("Here comes the list of Timestamps of request URLs  ###################");
for (MoxProxyProcessedTrafficEntry moxProxyProcessedTrafficEntry : requestTraffic) {
System.out.println( moxProxyProcessedTrafficEntry.getTimestamp());
}
  • Assert that all the calls are successful
for (MoxProxyProcessedTrafficEntry moxProxyProcessedTrafficEntry : responseTraffic) {

Assert.assertNotEquals(500, moxProxyProcessedTrafficEntry.getStatusCode());
}

Performance information

We can validate and monitor the time taken for each and every call inside the UI automations. Traditionally this type of verification is done through API level. But there we have some disadvantages.

1. The reported response time cover the browser-based response time contribution.

2. Traditional load test use a browser thus they cannot determine the difference between the different browsers.

We can eliminate above limitations by using Moxproxy. Moxproxy gives you the time taken for each call with the involvement of the browser. It is closer to the actual user experience. Given bellow is an example of doing that.

{

for (MoxProxyProcessedTrafficEntry moxProxyProcessedTrafficEntryResponce : responseTraffic) {

if (moxProxyProcessedTrafficEntry.getUrl().equals(moxProxyProcessedTrafficEntryResponce.getUrl())) {
int start = moxProxyProcessedTrafficEntry.getTimestamp().toInstant().getNano();
int end = moxProxyProcessedTrafficEntryResponce.getTimestamp().toInstant().getNano();

System.out.println(moxProxyProcessedTrafficEntry.getUrl() + " " + " " + (end - start));
}
}
}

Given above are just examples. Now that you have a list of network calls, you can perform any analysis you want!!!!!!!!!

Reference

https://github.com/lukasz-aw/moxproxy

--

--