Friday, December 30, 2016

Use of Functions in Java & WebDriver

Back in September, I attended the JavaOne Conference in San Francisco. Having a fairly decent understanding around Streams and Lambdas, the sessions of focus was on additional functionality within Java 8 and getting a jump on Java 9. Functions, however, dominated my thought process, and the idea of functional programming exploded within. Since that time, I have spent my waking moments, thinking, reading, watching, and doing functional programming.

In simple terms, "its made programming fun again", and with that, I am applying this new knowledge to my projects.

I realize Java is not a fully functional language, like Clojure, Haskell or even Scala; however, it has some functional aspects that get you moving in the functional language direction without having to make a programming change, while trying to understand the new paradigm.

The example below, the getFile function takes a WebDriver and returns a File, while using a predicate to test the instance of the driver. Base on the outcome, an additional function is called accepting the driver and returns a File.
Function<WebDriver,File> getFile = driver -> instanceOfRemoteDriver.test(driver) ? remoteDriverScreenshotFile.apply(driver) : webDriverScreenshotFile.apply(driver);

As this shift continues to grow and gather momentum, Java automation engineers will start to feel the pressure to make a change. In my observations, QA automation engineers tend to lag behind the latest trends within his/her respected language. With that said, I created a GitHub project to share ideas and concepts around functional programming using Java and WebDriver (and in the future Scala).

Examples can be found @ functional-programming-selenium-examples

No comments:

Post a Comment