Skip to content

Instantly share code, notes, and snippets.

@dburriss
Created October 15, 2018 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dburriss/cffa00d4b0c30c1ac4c93858c538f870 to your computer and use it in GitHub Desktop.
Save dburriss/cffa00d4b0c30c1ac4c93858c538f870 to your computer and use it in GitHub Desktop.
Examples of using F# Canopy with fsx script files
#r "packages/NETStandard.Library/build/netstandard2.0/ref/netstandard.dll"
#r "packages/Selenium.WebDriver/lib/netstandard2.0/WebDriver.dll"
#r "packages/canopy/lib/netstandard2.0/canopy.dll"
#load "Pages.fsx"
open canopy.classic
open canopy.configuration
open canopy.types
open Pages
chromeDir <- "C:\\tools\\selenium\\"
start chrome
pin FullScreen
let term = "bob"
GooglePage.goto()
GooglePage.searchFor term
let results = SearchResultPage.results()
// print result count
results
|> List.length
|> printfn "Result count for '%s' is %i" term
//click first result
results
|> List.head
|> SearchResult.click
#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\\"
start chrome
pin FullScreen
url "https://google.com/"
"[name=q]" << "Youtube: BGF Red and Blue"
press enter
#r "packages/NETStandard.Library/build/netstandard2.0/ref/netstandard.dll"
open canopy.csharp
#r "packages/Selenium.WebDriver/lib/netstandard2.0/WebDriver.dll"
#r "packages/canopy/lib/netstandard2.0/canopy.dll"
open System
open canopy.classic
open OpenQA.Selenium
///////////////////////////////////////
/// HELPERS
///////////////////////////////////////
module ReadOnly =
let ofSeq<'a> (ss: 'a seq) = (ResizeArray ss).AsReadOnly()
let ofList<'a> (ls: 'a list) = (ResizeArray ls).AsReadOnly()
let ofArray<'a> (arr: 'a array) = (ResizeArray arr).AsReadOnly()
let empty<'a> () = (ResizeArray []).AsReadOnly()
let init<'a> length initializer = (ResizeArray (List.init length initializer)).AsReadOnly()
let toSeq<'a> (collection: Collections.ObjectModel.ReadOnlyCollection<'a>) = seq { for i in collection do yield i }
let toList<'a> collection = collection |> toSeq |> List.ofSeq
let toArray<'a> collection = collection |> toSeq |> List.ofSeq
let elText (el:IWebElement) = el.Text
// Allows finding by name using `q` instead of `[name=q]`
let findByName (name:string) (f:By -> Collections.ObjectModel.ReadOnlyCollection<IWebElement>) (_ : IWebDriver) =
try
f(By.Name(name)) |> ReadOnly.toList
with | ex -> []
addFinder findByName
#r "packages/Selenium.WebDriver/lib/netstandard2.0/WebDriver.dll"
#r "packages/canopy/lib/netstandard2.0/canopy.dll"
#load "CanopyHelpers.fsx"
open canopy.classic
open OpenQA.Selenium
open CanopyHelpers
type SearchResult = {
Title:string
Description:string
El:IWebElement
}
module SearchResult =
let fromEl el =
let title = el |> elementWithin "h3" |> elText
let desc = el |> elementWithin ".st" |> elText
{
Title = title
Description = desc
El = el
}
let click sr = sr.El |> elementWithin "h3" |> fun el -> el.Click()
module GooglePage =
let uri = "https://google.com/"
let goto () = url uri
let searchFor term =
"q" << term
press enter
module SearchResultPage =
let results () =
(List.collect (fun el -> elementsWithin ".g" el) (elements ".srg"))// collect needed because .srg is sometimes split across multipe elements
|> List.map SearchResult.fromEl
source https://www.nuget.org/api/v2
nuget NETStandard.Library
nuget canopy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment