lefthandedgoat / canopy

f# web automation and testing library, built on top of Selenium (friendly to c# also)
http://lefthandedgoat.github.io/canopy/
MIT License
505 stars 117 forks source link

Cannot add finder #483

Closed Pwntheon closed 4 years ago

Pwntheon commented 4 years ago

I tried adding a finder but i'm getting a type error.

I assumed it was something wrong with my code, but when just pasting the example from the docs i get the same error:

The type 'IWebDriver -> IWebElement list' does not match the type 'IWebElement list'

module Finders

open canopy.classic
open OpenQA.Selenium

let findByHref href f =
    try
        let cssSelector = sprintf "a[href*='%s']" href
        f(By.CssSelector(cssSelector)) |> List.ofSeq
    with | ex -> []

addFinder findByHref
Pwntheon commented 4 years ago

For information, the selector i want to add is a lookup by data-attributes like this: element "data-tab=properties" should select the element <div class="tab" data-tab="properties">...</div>

Here is my selector as i wrote it:

module Finders

open canopy.classic
open System.Text.RegularExpressions
open OpenQA.Selenium

exception FinderDoesNotApply of unit

addFinder ( fun dataAttribute f ->
    try
        if Regex.Match(dataAttribute, "^data-\w{1,}=\w{1,}").Success then
            let selectorParts = dataAttribute.Split [|'='|]
            let selector = "*[" + selectorParts.[0] + "=\"" + selectorParts.[1] + "\"]"
            f(By.CssSelector(selector)) |> List.ofSeq
        else
            raise (FinderDoesNotApply())
    with | ex -> []
)
Pwntheon commented 4 years ago

I figured it out. The documentation is outdated. Adding a third (unused) parameter to the function solves it:

module Finders

open canopy.classic
open System.Text.RegularExpressions
open OpenQA.Selenium

exception FinderDoesNotApply of unit

let dataFinder dataAttribute f _ = 
    try
        if Regex.Match(dataAttribute, "^data-\w{1,}=\w{1,}").Success then
            let selectorParts = dataAttribute.Split [|'='|]
            let selector = "*[" + selectorParts.[0] + "=\"" + selectorParts.[1] + "\"]"
            f(By.CssSelector(selector)) |> List.ofSeq
        else
            raise (FinderDoesNotApply())
    with | ex -> []
lefthandedgoat commented 4 years ago

Yes, I didnt realize the documentation was out of date. Here is an example:

https://github.com/lefthandedgoat/canopy/blob/273fe1c5e93111d5114e2ef81e1d02e9e3eaf49b/tests/basictests/Program.fs#L739-L747

Pwntheon commented 4 years ago

Thank you for the prompt reply even though i solved the issue.

Loving canpoy :)