Back to Home

Selenium on Windows: Everything from the Beginning

webdriver · selenium · test automation · windows · golang · internet explorer

Selenium on Windows: Everything from the Beginning

Original author: Alexander Andryashin
  • Transfer

I present to you the translation of my article on Medium.com.


First released over 30 years ago, Microsoft Windows is today the undisputed leader among desktop operating systems. This simply cannot be ignored when developing web applications. In this article I would like to discuss some features of using Selenium under Windows and offer a simple and field-proven solution that greatly simplifies life.



How Windows differs from Linux


In my previous articles ( first , second , third ), I described open source approaches and tools to create a scalable Selenium cluster. We also talked about how to effectively run tests on the developer's machine using the same tools. All articles used Linux as the operating system. How is Windows different from Linux in terms of Selenium?


  1. The presence of browsers that do not exist on other platforms. Depending on the version, Windows comes preloaded with Internet Explorer (IE) or Microsoft Edge. Only one version of each browser can be installed at a time. For both browsers, there are ready-made executable files for web drivers (IEDriverServer and EdgeDriver, respectively) that use Windows API calls to launch and control the browser. From this perspective, Windows browsers are no different from Linux browsers.
  2. A graphical interface is built into the operating system. Most versions of Windows (except the latest versions of Windows Server) have a built-in graphical interface that can neither be disabled nor replaced with another graphics server. The interface starts automatically with the operating system and constantly consumes resources. In addition, the Windows GUI by default displays all openable windows (including browser windows) in the same desktop and only one of these windows can be in focus at a given point in time. Because of this, attempts to run multiple IE or Edge in parallel often lead to various problems with window focus: different CSS styles (for example, when hovering over links), non-triggering DOM events, and so on. This problem is very interfering.
  3. Almost complete lack of Docker support. Recent versions of Windows Server support most of the Docker features natively, but there is no such support on the desktop versions we are interested in. Therefore, the only way to run Docker on these versions is with the Linux virtual machine in which Docker is installed.

As you see, many modern approaches when working with Selenium: using an X server without a monitor and launching browsers in containers do not work on Windows. But is it possible to achieve performance similar to Linux and bypass the known limitations of Windows? Yes, and it's easier than you might think! In the following sections I will explain how to do this.


Creating order out of chaos


We will move towards the goal step by step. To get started, make the decision as simple as possible. As you know, the typical installation scheme for Selenium on Windows looks like this:



The scheme consists of a Selenium server launched using the Java Virtual Machine (JRE), then the IEDriverServer or EdgeDriver executable file, and, finally, the browser itself - IE or Edge. There is at least one weak link in this chain - the Selenium server and Java. That's because Selenium here acts as a simple proxy server that starts the driver process on a random port and then sends all requests to this port. Proxying network traffic is the simplest task in any programming language, because the main work is performed by the network subsystem of the operating system. That is why installing Java (50 MB or more) and downloading the Selenium server (20 MB or more) for simple proxies looks like a cumbersome solution. Moreover, Selenium server does not work well under load:


  1. It consumes too much memory and sometimes even flows.
  2. Proxying is performed “manually” - a new instance of the HTTP client is created for each request and the incoming request is copied to it. This approach is very inefficient and in some cases causes strange timeouts during proxying.
    We can greatly improve the situation by simply replacing the heavy Selenium server with the lightweight Selenoid .

How to replace Selenium server with Selenoid


Selenoid is a lightweight Selenium server replacement written in Go . Selenoid comes as one small (about 7 MB) executable file and has no external dependencies. To get started, you just need to download and run this file. In my previous article, I briefly described how convenient Selenoid can be for launching browsers in Docker containers - its main purpose. The second supported mode is to run executable files instead of containers and proxy network traffic to them - just like the Selenium server does this with IEDriverServer and EdgeDriver. Replacing the Selenium server with Selenoid is very simple. For example, start Internet Explorer using Selenoid:


  1. Download the Selenoid executable from the release page . The executable file is usually called selenoid_windows_386.exefor 32-bit Windows and selenoid_windows_amd64.exefor Windows 64 bit. As far as I know, desktop versions of Windows do not have a built-in console program for downloading files. But, if you have Cygwin and curl installed , then you can download the file like this:
    $ curl -o selenoid.exe https://github.com/aerokube/selenoid/releases/download/1.2.1/selenoid_windows_386.exe
  2. Download and unpack the archive from the Selenium downloadIEDriverServer.exe page . For example, save in .IEDriverServer.exeC:\
  3. Configure Internet Explorer as described on the wiki .
  4. Create a simple configuration file for Selenoid - browsers.json:
    {
    "internet explorer": {
    "default": "11",
    "versions": {
      "11": {
        "image": ["C:\\IEDriverServer.exe"]
      }
    }
    }
    }
  5. We start Selenoid instead of the Selenium server (port 4444 should be free) using this file selenoid.bat:


    C:\selenoid.exe -conf C:\browsers.json -disable-docker -limit 4 > C:\selenoid.log 2>&1

    Here we assume that all files from the previous steps have been saved to C:\. Selenoid logs will be saved to C:\selenoid.log. Pay attention to the parameter -limit- it determines how many sessions can be started at the same time. When the specified number of sessions is started, new requests are queued in exactly the same way as in the Selenium server.


  6. Done! You can run tests at the same URL:
    http://localhost:4444/wd/hub
  7. To remain lightweight, Selenoid does not have a built-in graphical interface. The muzzle is made as a separate executable file: Selenoid UI . Just download the compiled file from the release page and run it, then open it http://localhost:8080/in a browser.

Run tests on multiple desktops


After replacing the Selenium server with Selenoid, you will see a significant reduction in memory and CPU consumption. This simple step may even allow you to run more browsers in parallel. However, a simple replacement does not cure problems opening multiple browser windows at the same time. Windows still appears on the same desktop and continue to lose focus. In order to get around this obstacle, you need to learn how to run browsers on separate desktops. The good news is that the internal Windows APIs even in desktop versions have support for virtual desktops - you can switch between desktops and launch windows in these desktops independently of each other. But there is better news - no need to dive into the insides of Windows to get this behavior for Selenium - the necessary functionality is already implemented in the projectheadless-selenium-for-win . After downloading the release, you will receive an archive with two executable files: desktop_utils.exeand headless_ie_selenium.exe.


The first one is a console utility for manually switching between desktops. The command looks something like this:


C:> desktop_utils.exe -s desktop1

To work with Selenium we need a second utility - headless_ie_selenium.exe. It is an add-in to IEDriverServer.exeprocessing requests for sessions and automatically starting IEDriverServer.exein a new desktop. headless_ie_selenium.exemust be in the same directory as IEDriverServer.exe. In order to use the utility with Selenoid you just need to replace let up the executable file in browsers.jsonand restart Selenoid:


{
  "internet explorer": {
    "default": "11",
    "versions": {
      "11": {
        "image": ["C:\\headless_ie_selenium.exe"]
      }
    }
  }
}

Now all problems with the focus of the windows should go away.


A bit of magic with Selenium capabilities


Simple replacement Selenium Selenoid on and IEDriverServer.exeon headless_ie_selenium.exe, we decided the most pressing problems of Selenium under Windows. Let's make a diamond out of a diamond by setting some useful capabilities in tests.


  1. By default, Internet Explorer uses the HTTP proxy system settings. This leads to the fact that the proxy settings set in one session "crawl" into other sessions. To fix this, set:


    ie.usePerProcessProxy = true

  2. Your web application may use cookies to store sensitive information. On Windows, these files are stored separately for each user and the default behavior is to reuse set cookies between parallel sessions. This can lead to floating tests. To avoid cookie reuse, you can start IE in anonymous mode:


    ie.browserCommandLineSwitches = "-private"

    Also do not forget to set:


    ie.ensureCleanSession = true

  3. In order to avoid strange errors with the focus of the window, also make sure that the capability indicated below is not set or is false:
    requireWindowFocus = false

Conclusion


In this article, I briefly described the main problems that you may encounter when running Selenium tests under Windows and suggested a simple solution. I continue to argue that tests in Selenium may not be painful. You just need to be able to cook it properly.

Read Next