Update: This solution works on WSL1 only
Why this post?
If you are one of those testers that had been using a Windows machine and have had to setup a Linux system for running your Selenium automation test, this post is for you. I had to setup a Linux system for running the automation tests for one of our clients. The framework the client had been using, used a UNIX command to find the test files in the various subdirectories under the project root. This prevented us from running the tests on a Windows environment.
I am writing this post to share the steps I followed to setup a Linux environment on my Windows machine to run the Selenium tests. Hopefully, this will help other testers who face a similar issue.
Windows Subsystem for Linux(WSL):
WSL is a cool feature on Windows 10 that allows you to run most of the GNU/Linux commands and applications on Windows.
Here are the steps on how to setup WSL on Windows – https://docs.microsoft.com/en-us/windows/wsl/install-win10
You can pick any distribution of Linux for WSL, I chose Ubuntu for my project. The steps I am about to suggest should work regardless of the Linux distribution you are planning to use.
Access Windows files from WSL:
Now that you have WSL setup, you can start accessing Windows files from WSL. The Windows file system is mounted on the mount point – /mnt
on WSL. To see the contents of a file foo.txt in C:
drive, you can use – cat /mnt/c/foo.txt
from WSL. The .exe
files on Windows can be run from WSL too. To open Chrome browser on Windows run – /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe
.
Link Windows’s Chrome and chromedriver to WSL:
To run a basic Selenium UI test on any environment you would need a browser and a driver to control the browser, in our case all we have to do is, make Windows’s Chrome browser and the chromedriver accessible from WSL, through symlinks.
Run the following commands on WSL to create symlinks:
ln -s '/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe' /usr/bin/google-chrome
, this would create a file as google-chrome in/usr/bin
and link it to the Chrome browser on Windows.ln -s /mnt/c/Users/username/node_modules/chromedriver/lib/chromedriver/chromedriver.exe /usr/bin/chromedriver
, this would create a file a chromedriver in/usr/bin
and link it to the chromedriver on Windows.
Note: The location of Chrome browser and chromedriver will be different on your system. Check and substitute the right paths when using above commands. Also in some cases you may need to run as a root user for this to work.
How it works:
When driver = webdriver.Chrome()
is run from a script or Python interpreter on WSL, it searches for chromedriver
binary in system path, it will locate the chromedriver
we added with the above command and then will try to open google-chrome
binary in the system path, again it will use google-chrome
we added. WSL will start accessing the chromedriver
and google-chrome
from Windows as if they are present on its own filesystem and start running the test steps on the browser.
There you go, you now have a Linux testing environment inside you Windows machine. Have fun testing!
My expertise lies in engineering high-quality software. I began my career as a manual tester at Cognizant Technology Solutions, where I worked on a healthcare project. However, due to personal reasons, I eventually left CTS and tried my hand at freelancing as a trainer. During this time, I mentored aspiring engineers on employability skills. As a hobby, I enjoyed exploring various applications and always sought out testing jobs that offered a good balance of exploratory, scripted, and automated testing.
In 2015, I joined Qxf2 and was introduced to Python, my first programming language. Over the years, I have also had the opportunity to learn other languages like JavaScript, Shell scripting (if it can be called a language at all), and more recently, Rust. Despite this exposure, Python remains my favorite language due to its simplicity and the extensive support it offers for libraries.
In my free time, I like to watch football (I support Arsenal Football Club), play football myself, and read books.
Thanks for this guide!
Hi Mr. Shivahari,
Good day. I was tried to do exactly the same step you mention in your article to set up a test environment with Ubuntu20.04(WSL2). I ran into an issue that the browser did pop up as I except and received an error message as below in a while:
selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service /path_to_chromedriver/chromedriver
I could not find any solution on the web……..
Would it be possible to suggest some solution/method to overcome my issue?
Thank you.
P.S. my environment:
1. Windows 10
2. Ubuntu20.04
3. Chromedriver : 88.0.4324.96
4. Google chrome : 88.0.4324.104
Hi Steve,
Please check the chromedriver path you have set is correct. You can validate the chromedriver version in WSL by running this command
chromedriver –version
Let us know if this was helpful.
Regards,
Rohini
Hi, it doesn’t work for me either. It keeps saying can not connect to service. everything has been checked and tested…
Hi Nick,
We are able to reproduce this issue, thank you for bringing it to our attention, I have updated the blog now, this solutions works on WSL1 only.
What we know so far – this does not work on WSL2 because of the latest network changes and we are working on a few possible solutions I will update the blog once we find the right solution.
You can still work with this solution on WSL1 in parallel with WSL2, Microsoft allows using WSL1 & WSL2 together
Thanks,
Shivahari.P
Hi Steve,
We are able to reproduce this issue, thank you for bringing it to our attention, I have updated the blog now, this solutions works on WSL1 only.
What we know so far – this does not work on WSL2 because of the latest network changes and we are working on a few possible solutions I will update the blog once we find the right solution.
You can still work with this solution on WSL1 in parallel with WSL2, Microsoft allows using WSL1 & WSL2 together
The fact that most of us moved to Linux did not help investigating this Windows issue on time.
Thanks,
Shivahari.P