Recently I found myself doing a very repetitive task that entailed copying values one at a time off a page, navigating to the next page, then repeat. I would spend 2 hours automating 1 hour of work if said work is sufficiently boring, even if I may never need the automation again. I enjoy coding, I do not enjoy copy-pasting. So I wondered if it was even possible to run Canopy in an F# FSX script file. It turns out it is.
F# Scripting
In case you are new to F# let us briefly touch on what a FSX file is. F# code can be placed into .fs
files in a project and compiled to DLLs. This is how you would write a console application, Windows Service, or a Web Application. Another option that is great for experimenting is using .fsx
files (and nowadays C# as well with .csx). These are F# scripting file that allow you to run as a standalone script using FSI (FSharp Interactive).
fsi .\basic.fsx
This requires Fsi.exe
be on your PATH. For more information see the docs.
Worth mentioning is Ionide Project's great support for running script files, as well as working with PAKET which we will not go into in detail.
Setup
So the first thing you will need is a way to pull down the necessary Nuget packages. See my article on getting up and running with Paket fast if you need help setting up Paket.
Here is the TL;DR version:
.NET Core 2.1 SDK and later versions
You can install it in a specific directory.
dotnet tool install --tool-path ".paket" Paket --add-source https://api.nuget.org/v3/index.json
A basic script
First we use PAKET to pull down the Nuget package we need.
source https://www.nuget.org/api/v2
nuget canopy
And run .\.paket\paket.exe install
to download the packages.
#r "packages/Selenium.WebDriver/lib/netstandard2.0/WebDriver.dll"
#r "packages/canopy/lib/netstandard2.0/canopy.dll"
open canopy.classic
open canopy.configuration
open canopy.types
chromeDir <- "C:\\tools\\selenium\\"//or wherever you place your Selenium
start chrome
pin FullScreen
url "https://google.com/"
"[name=q]" << "Youtube: BGF Red and Blue"
press enter
One gotcha I did run across here is that the order of the
#r
references here does matter. The WebDriver.dll is required before canopy.dll.
For more advanced examples see the related Github repository.
Conclusion
And that is how easy it is to start using Canopy from a FSX file. This is a great way of automating some repetitive web task where an API is not available or exploring interacting with some DOM elements via Canopy in preparation for a UI test. Hope you found this useful. If you have any other use-cases, I would love to hear about them in the comments below.