Selenium Tests in Kotlin: WebDriverManager
Selenium WebDriver allows controlling different types of browsers (such as Chrome, Firefox, Opera, Edge, and so on) programmatically using different programming languages (Java, Kotlin, JavaScript, Python, C#, …). Browser is driven using a native mechanism, so we need a binary file (driver) in between the test using the WebDriver API and the actual browser
The following picture shows a fine-grained diagram of the different flavour of WebDriver binaries and browsers:
Concerning Kotlin, in order to locate these drivers, the absolute path of the binary controlling the browser should be exported in a given environment variable before creating a WebDriver instance, as follows:
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver")
System.setProperty("webdriver.edge.driver", "C:/path/to/MicrosoftWebDriver.exe")
System.setProperty("phantomjs.binary.path", "/path/to/phantomjs")
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver")
System.setProperty("webdriver.opera.driver", "/path/to/operadriver")
System.setProperty("webdriver.ie.driver", "C:/path/to/IEDriverServer.exe")
Driver management is painful
- Driver (chromedriver, geckodriver, etc.) must be downloaded manually for the proper platform running the test (i.e. Windows, Linux, Mac)
- A proper driver version must match the browser version.
- Browser is constantly updated (and drivers too).
- Tests are not portable (different operative systems, path to driver).
Here is a simple sollution..
WebDriverManager is a Java library that allows automating the management of the binary drivers (chromedriver, geckodriver, etc.) required by Selenium
The WebDriverManager API is quite simple, providing a singleton object for each of the above-mentioned browsers:
WebDriverManager.chromedriver().setup()
WebDriverManager.phantomjs().setup()
WebDriverManager.edgedriver().setup()
WebDriverManager.iedriver().setup()
WebDriverManager.firefoxdriver().setup()
WebDriverManager.operadriver().setup()
WebDriverManager — Design
WebDriverManager — Conclusions
- WebDriverManager is used in different projects in the Selenium ecosystem. For instance:
-> io.appium » java-client: https://github.com/appium/java-client
-> com.codeborne » selenide: https://github.com/selenide/selenide
- WebDriverManager concept has been ported to other languages:
-> webdriver-manager (Node.js): https://github.com/angular/webdriver-manager
-> webdriver_manager (Python): https://github.com/SergeyPirogov/webdriver_manager
-> WebDriverManager.Net (.Net): https://github.com/rosolko/WebDriverManager.Net
-> Webdrivers Gem (Ruby): https://github.com/jeffnyman/webdriver_manager
References
A sample code can be found here: https://github.com/dilshan5/ui-automation-kotlin