A Selenium proxy is an intermediary between client requests and server responses. Proxies are primarily used to ensure privacy and encapsulation between numerous interactive systems.
Overview
Setting up a proxy in Selenium allows testers to control network traffic, bypass restrictions, and enhance security during automation testing.
Setting Unauthenticated Proxy in Selenium
- Load Selenium WebDriver
- Define Proxy (IP:PORT)
- Set ChromeOptions()
- Add proxy to options
- Add the options to the Chrome()
Setting Authenticated Proxy in Selenium
- Create a Chrome extension
- Add Extension to Selenium using add_extension to Selenium
- Run the script to configure the proxy in Chrome using Selenium WebDriver
- Test on real devices using BrowserStack’s cloud Selenium grid for accuracy
This guide covers proxy basics, testing with proxy servers, setting up a proxy server, and handling authenticated proxies to ensure seamless testing across different environments.
What is Selenium Proxy?
A Selenium Proxy is an intermediary server that routes network traffic between Selenium WebDriver and the target website. It allows testers to control, monitor, and modify requests and responses while automating web interactions.
Proxies in Selenium are commonly used for:
- Bypassing geo-restrictions: Testing location-based content by simulating access from different regions.
- Capturing and analyzing network traffic: Debugging requests, responses, and performance issues.
- Enhancing security and privacy: Hiding the real IP address to avoid detection or blocking.
- Testing under different network conditions: Simulating slow or unstable connections to check website resilience.
Selenium provides built-in support for proxies through the Proxy class, which can be configured for browsers like Chrome, Firefox, and Edge. By integrating proxies into test scripts, QA teams can ensure more robust and realistic testing scenarios.
Authenticating Proxy in Selenium
Proxy servers are most helpful in executing localization tests. Let’s say a tester wants to open an E-commerce website and check that the proper language settings and currency appear for users from a specific country.
- An easy way to verify this is to access the website as a user would from a target location.
- Like most tests, it would be easier to automate this activity, especially when a website has to be checked from multiple locations.
Selenium is the most widely used tool for running browser automation tests. Essentially, developers can use Selenium to monitor browser and website behavior without opening and executing an entire browser instance. This article will detail how to set up a proxy server and use it to access the website via Selenium.
Setting Up a Proxy Server using Selenium
Selenium allows setting up both authenticated and unauthenticated proxy servers using browser-specific configurations, ensuring seamless automation testing.
Setting Up Unauthenticated Proxy
An unauthenticated proxy server in Selenium can be set up with the following steps:
- Import Selenium WebDriver from the package
- Define the proxy server (IP:PORT)
- Set ChromeOptions()
- Add the proxy server argument to the options
- Add the options to the Chrome() instance
from selenium import webdriver PROXY = "11.456.448.110:8080" chrome_options = WebDriver.ChromeOptions() chrome_options.add_argument('--proxy-server=%s' % PROXY) chrome = webdriver.Chrome(chrome_options=chrome_options) chrome.get("https://www.google.com")
Use this Chrome WebDriver instance to execute tests that incorporate the proxy server. For example, the following code snippet tests to ensure that a search field shows the user’s current city as the default location.
def testUserLocationZurich(self): self.chrome.get(self.url) search = self.chrome.find_element_by_id('user-city') self.assertIn('Zurich', search.text)
To test a website from multiple locations by making this code reusable across separate tests, define a method that takes the proxy IP address as an argument.
selenium.webdriver.common.proxy – Proxy contains information about the proxy type and necessary proxy settings.
Testers can run tests using an unauthenticated server. However, if they wish to use an authenticated server, they can follow the procedure below.
Setting Up an Authenticated Proxy in Selenium
Authenticated proxy servers can be tedious to use in automated tests as there’s no built-in way to pass along proxy server credentials in Selenium. As of now, there are two options to handle authenticated proxies. The right choice depends on the testers’ requirements. It depends on factors like the version of Selenium and the headless browser used in tests.
Let’s learn how to use it.
- The best way to integrate authenticated proxies with Selenium is by using PhantomJS as a headless browser instead of the Chrome WebDriver.
- However, adding a browser extension to authenticate Selenium is possible.
- While this approach is more complex, it can be used with the latest version of Selenium, which may be a requirement for some development teams.
The first step is creating a Chrome extension by including two files in an archive named proxy.zip:
Background.js
var config = { mode: "fixed_servers", rules: { singleProxy: { scheme: "http", host: "YOUR_PROXY_ADDRESS", port: parseInt(PROXY_PORT) }, bypassList: ["foobar.com"] } }; chrome.proxy.settings.set({value: config, scope: "regular"}, function() {}); function callbackFn(details) { return { authCredentials: { username: "PROXY_USERNAME", password: "PROXY_PASSWORD" } }; } chrome.webRequest.onAuthRequired.addListener( callbackFn, {urls: ["<all_urls>"]}, ['blocking'] );
manifest.js
{ "version": "1.0.0", "manifest_version": 3, "name": "Chrome Proxy", "permissions": [ "Proxy", "Tabs", "unlimitedStorage", "Storage", "<all_urls>", "webRequest", "webRequestBlocking" ], "background": { "scripts": ["background.js"] }, "Minimum_chrome_version":"76.0.0" }
The Chrome extension can be added to Selenium using the add_extension method:
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_extension("proxy.zip") driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=chrome_options) driver.get("http://google.com") driver.close()
This example uses a single proxy server in the extension. To add more proxy servers, the tester must make further modifications to the chrome.proxy API. If the tester uses a CI/CD server, they would have to be sure that the build machine has Chrome installed and the relevant browser extension added.
Run Selenium Tests on Real Device Cloud
Try running the code detailed above to set the proxy for Chrome using Selenium WebDriver. Remember that Selenium tests must be run on a real device cloud to get accurate results.
BrowserStack’s cloud Selenium grid of 3500+ real browsers and devices allows testers to automate visual UI tests in real user conditions. Sign up, select a device-browser-OS combination, and run free tests.
Conclusion
Configuring a proxy in Selenium helps testers simulate different network conditions, improve security, and bypass restrictions. Whether using standard or authenticated proxies, proper setup ensures seamless automation.