Skip to content

Commit

Permalink
#245 Remove dependency on Selenium Support, add own wait function
Browse files Browse the repository at this point in the history
  • Loading branch information
lefthandedgoat committed Mar 13, 2016
1 parent 68dd6dc commit 10b3b13
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 50 deletions.
1 change: 0 additions & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ source https://www.nuget.org/api/v2/
nuget NuGet.CommandLine
nuget FSharp.Core >= 3.0.2 lowest_matching:true
nuget Newtonsoft.Json framework: >= net40
nuget Selenium.Support
nuget Selenium.WebDriver
nuget SizSelCsZzz framework: >= net40
nuget FAKE
Expand Down
2 changes: 1 addition & 1 deletion paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ NUGET
FSharp.Compiler.Service (>= 2.0.0.3)
Newtonsoft.Json (6.0.8) - framework: >= net40
NuGet.CommandLine (3.3.0)
Selenium.Support (2.52.0)
Selenium.Support (2.52.0) - framework: >= net40
Selenium.WebDriver (>= 2.52.0)
Selenium.WebDriver (2.52.0)
SizSelCsZzz (0.3.36) - framework: >= net40
Expand Down
45 changes: 28 additions & 17 deletions src/canopy/canopy.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module canopy.core

open OpenQA.Selenium.Firefox
open OpenQA.Selenium
open OpenQA.Selenium.Support.UI
open OpenQA.Selenium.Interactions
open SizSelCsZzz
open Microsoft.FSharp.Core.Printf
Expand Down Expand Up @@ -96,17 +95,29 @@ let puts text =
infoDiv.innerHTML = 'locating: " + escapedText + "';"
swallowedJs info

let private wait timeout f =
let wait = new WebDriverWait(browser, TimeSpan.FromSeconds(timeout))
wait.Until(fun _ -> (
try
(f ()) = true
with
| :? CanopyException as ce -> raise(ce)
| _ -> false
)
) |> ignore
()
let waitResults timeout (f : unit -> 'a) =
let sw = System.Diagnostics.Stopwatch.StartNew()
let rec innerwait timeout f =
let sleepAndWait () = sleep waitSleep; innerwait timeout f
if sw.Elapsed.TotalSeconds >= timeout then raise <| WebDriverTimeoutException("Timed out!")

try
let result = f()
match box result with
| :? bool as b ->
if b then result
else sleepAndWait ()
| _ as o ->
if o <> null then result
else sleepAndWait ()
with
| :? WebDriverTimeoutException as ex -> reraise()
| :? CanopyException as ce -> raise(ce)
| _ -> sleepAndWait ()

innerwait timeout f

let wait timeout (f : unit -> bool) = waitResults timeout f |> ignore

let private colorizeAndSleep cssSelector =
puts cssSelector
Expand Down Expand Up @@ -187,7 +198,7 @@ let suggestOtherSelectors cssSelector =
let describe text =
puts text

let waitFor2 message (f : unit -> bool) =
let waitFor2 message f =
try
wait compareTimeout f
with
Expand Down Expand Up @@ -231,16 +242,16 @@ let rec private findElements cssSelector (searchContext : ISearchContext) : IWeb

let private findByFunction cssSelector timeout waitFunc searchContext reliable =
if wipTest then colorizeAndSleep cssSelector
let wait = new WebDriverWait(browser, TimeSpan.FromSeconds(elementTimeout))

try
if reliable then
let elements = ref []
wait.Until(fun _ ->
wait elementTimeout (fun _ ->
elements := waitFunc cssSelector searchContext
not <| List.isEmpty !elements) |> ignore
not <| List.isEmpty !elements)
!elements
else
wait.Until(fun _ -> waitFunc cssSelector searchContext)
waitResults elementTimeout (fun _ -> waitFunc cssSelector searchContext)
with
| :? WebDriverTimeoutException ->
suggestOtherSelectors cssSelector
Expand Down
11 changes: 1 addition & 10 deletions src/canopy/canopy.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,6 @@
</When>
</Choose>
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v3.5'">
<ItemGroup>
<Reference Include="WebDriver.Support">
<HintPath>..\..\packages\Selenium.Support\lib\net35\WebDriver.Support.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.0' Or $(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3' Or $(TargetFrameworkVersion) == 'v4.6' Or $(TargetFrameworkVersion) == 'v4.6.1')">
<ItemGroup>
<Reference Include="WebDriver.Support">
Expand Down Expand Up @@ -179,4 +170,4 @@
</ItemGroup>
</When>
</Choose>
</Project>
</Project>
1 change: 1 addition & 0 deletions src/canopy/configuration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let mutable elementTimeout = 10.0
let mutable compareTimeout = 10.0
let mutable pageTimeout = 10.0
let mutable wipSleep = 1.0
let mutable waitSleep = 0.5
let mutable runFailedContextsFirst = false
let mutable reporter : IReporter = new ConsoleReporter() :> IReporter
let mutable disableSuggestOtherSelectors = false
Expand Down
13 changes: 2 additions & 11 deletions src/sampleprojectstructure/sampleprojectstructure.fsproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -114,15 +114,6 @@
</When>
</Choose>
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v3.5'">
<ItemGroup>
<Reference Include="WebDriver.Support">
<HintPath>..\..\packages\Selenium.Support\lib\net35\WebDriver.Support.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.0' Or $(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3' Or $(TargetFrameworkVersion) == 'v4.6' Or $(TargetFrameworkVersion) == 'v4.6.1')">
<ItemGroup>
<Reference Include="WebDriver.Support">
Expand Down Expand Up @@ -153,4 +144,4 @@
</ItemGroup>
</When>
</Choose>
</Project>
</Project>
11 changes: 1 addition & 10 deletions tests/basictests/basictests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,6 @@
</When>
</Choose>
<Choose>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And $(TargetFrameworkVersion) == 'v3.5'">
<ItemGroup>
<Reference Include="WebDriver.Support">
<HintPath>..\..\packages\Selenium.Support\lib\net35\WebDriver.Support.dll</HintPath>
<Private>True</Private>
<Paket>True</Paket>
</Reference>
</ItemGroup>
</When>
<When Condition="$(TargetFrameworkIdentifier) == '.NETFramework' And ($(TargetFrameworkVersion) == 'v4.0' Or $(TargetFrameworkVersion) == 'v4.5' Or $(TargetFrameworkVersion) == 'v4.5.1' Or $(TargetFrameworkVersion) == 'v4.5.2' Or $(TargetFrameworkVersion) == 'v4.5.3' Or $(TargetFrameworkVersion) == 'v4.6' Or $(TargetFrameworkVersion) == 'v4.6.1')">
<ItemGroup>
<Reference Include="WebDriver.Support">
Expand Down Expand Up @@ -170,4 +161,4 @@
</ItemGroup>
</When>
</Choose>
</Project>
</Project>

0 comments on commit 10b3b13

Please sign in to comment.