TestProject Forum
Powered by leading experts in the test automation community

Example of setting web proxy as one would with Selenium driver?

I wish to use the browsermob-proxy tool to insert custom request headers during the coded SDK (C#) tests.

tl;dr - I cannot figure out how to configure the TestProject SDK WebDriver to use a proxy. I need a code example.

I can see from examples online how this is possible to do with Selenium drivers directly. I also found it documented in this article and examples in the browsermob-proxy docs.

And it should be possible in TestProject too; I see it alluded to in this other TestProject Forum thread.

But in all the C# SDK code samples I don’t see anyone creating a driver instance with options; I only see something like this:

		public ExecutionResult Execute(IOSTestHelper helper)
		{
			// Get driver initialized by TestProject Agent
			// No need to specify browser type, it can be done later via UI
			var driver = helper.Driver;

In these cases, Driver is already a ready-made instance. How do I create a new one with proxy capability?

Hello @zblocker, thanks for reaching out!

to configure the driver to use BMP as a proxy so it can record the network activity, you need to create custom capabilities and set it in driver’s constructor,

here is an example in Python:

import json
from browsermobproxy import Server
from src.testproject.sdk.drivers import webdriver
from src.testproject.sdk.drivers.webdriver.chrome import ChromeOptions

def simple_test():
 
 # firing up a proxy server
dict = {'port': 8090}
server = Server("/path/to/bmp/bin/browsermob-proxy", options=dict)
server.start()

# creating custom ChromeOptions to set the driver run under the proxy server
proxy = server.create_proxy()
chrome_options = ChromeOptions()
chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy))  # Configure chrome options

# setting up driver with ChromeOptions we just created (you should set your developer token, which you can create in TestProject platform)
driver = webdriver.Chrome(token="aUV7b3u1F9B5hjDwq63xc8BQj6btBhNj-Lr2HzReiw1",chrome_options=chrome_options)
proxy.new_har("my_har_file")

# navigating to the URL and performing the reqiured steps
driver.get("http://info.cern.ch/")
title = driver.find_element_by_tag_name("h1")
print(title.text)

# printing generated HAR file to the screen
result = json.dumps(proxy.har, ensure_ascii=False)
print(result)
server.stop()

# quitting the driver
driver.quit()

Test reports will be automatically generated to TestProject platform under your account after driver.quit() has been performed, so you can inspect them under “Reports” tab:

Currently, setting custom capabilities supported in python and Java SDKs, and not supported in C# SDK. we are going to add this to C# SDK soon as well.

1 Like

Thank you very much. May I ask when the changes to the C# SDK might come? I’m comfortable with Python, but my team on the whole is more proficient with C#.

You are welcome,

May I ask you what is your ultimate goal with your coded tests?
Do you want to run it locally and enjoy automatically generated test reports in the TestProject platform?
Or you want to use upload coded tests option in TestProject platform and use it in the TestProject dashboard?

For the coded tests I was envisioning creating test suites that a developer could run locally to idiot-proof their changes, and that would also run during the integration test phase of our build pipeline.

Thanks for sharing, we actually come to conclusion that it’s a great feature to have within C# SDK as well, we will release a version of C# SDK that accepts custom capabilities while executing locally during second half of next week, 30/9 -1/10 stay tuned :wink:

Hello @zblocker . I’m glad to inform you that we just released support for custom capabilities in C# SDK!
Support is available starting with agent 0.64.36 and SDK 0.64.0. Hope you enjoy it :laughing: