Skip to content

Commit

Permalink
JimEvans: Beginning the long process of fixing the .NET WebDriverBack…
Browse files Browse the repository at this point in the history
…edSelenium (with integration tests!)

r10899
  • Loading branch information
jimevans committed Jan 7, 2011
1 parent b7fe34f commit b27dba9
Show file tree
Hide file tree
Showing 26 changed files with 987 additions and 23 deletions.
16 changes: 16 additions & 0 deletions WebDriver.sln
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "json-cpp", "third_party\jso
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mongoose", "third_party\mongoose\mongoose.vcproj", "{9AEBD612-232D-40CB-BE2C-F2B911FD6228}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebDriverBackedSeleniumTests", "selenium\test\csharp\WebdriverBackedSeleniumTests\WebDriverBackedSeleniumTests.csproj", "{68CF4628-4148-4627-ACA1-D4C225365D3F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|.NET = Debug|.NET
Expand Down Expand Up @@ -454,6 +456,20 @@ Global
{9AEBD612-232D-40CB-BE2C-F2B911FD6228}.Release|Win32.Build.0 = Release|Win32
{9AEBD612-232D-40CB-BE2C-F2B911FD6228}.Release|x64.ActiveCfg = Release|x64
{9AEBD612-232D-40CB-BE2C-F2B911FD6228}.Release|x64.Build.0 = Release|x64
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Debug|.NET.ActiveCfg = Debug|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Debug|Win32.ActiveCfg = Debug|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Debug|x64.ActiveCfg = Debug|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Release|.NET.ActiveCfg = Release|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Release|Any CPU.Build.0 = Release|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Release|Win32.ActiveCfg = Release|Any CPU
{68CF4628-4148-4627-ACA1-D4C225365D3F}.Release|x64.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<AssemblyName>webdriver-ie-test</AssemblyName>
<StartupObject>
</StartupObject>
<RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
<RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;

namespace Selenium.Internal
{
internal class AlertOverride
{
public void ReplaceAlertMethod(IWebDriver driver)
{
((IJavaScriptExecutor)driver).ExecuteScript(
"if (window.__webdriverAlerts) { return; } " +
"window.__webdriverAlerts = []; " +
"window.alert = function(msg) { window.__webdriverAlerts.push(msg); }; " +
"window.__webdriverConfirms = []; " +
"window.__webdriverNextConfirm = true; " +
"window.confirm = function(msg) { " +
" window.__webdriverConfirms.push(msg); " +
" var res = window.__webdriverNextConfirm; " +
" window.__webdriverNextConfirm = true; " +
" return res; " +
"};"
);
}

public string GetNextAlert(IWebDriver driver)
{
string result = (string)((IJavaScriptExecutor)driver).ExecuteScript(
"if (!window.__webdriverAlerts) { return null }; " +
"var t = window.__webdriverAlerts.shift();" +
"if (t) { t = t.replace(/\\n/g, ' '); } " +
"return t;"
);

if (result == null)
{
throw new SeleniumException("There were no alerts");
}

return result;
}

public bool IsAlertPresent(IWebDriver driver)
{
bool alertPresent = (bool)((IJavaScriptExecutor)driver).ExecuteScript(
"return window.__webdriverAlerts && window.__webdriverAlerts.length > 0;"
);

return alertPresent;
}

public string GetNextConfirmation(IWebDriver driver)
{
string result = (string)((IJavaScriptExecutor)driver).ExecuteScript(
"if (!window.__webdriverConfirms) { return null; } " +
"return window.__webdriverConfirms.shift();"
);

if (result == null)
{
throw new SeleniumException("There were no confirmations");
}

return result;
}

public bool IsConfirmationPresent(IWebDriver driver)
{
bool confirmPresent = (bool)((IJavaScriptExecutor)driver).ExecuteScript(
"return window.__webdriverConfirms && window.__webdriverConfirms.length > 0;"
);

return confirmPresent;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Selenium.Internal.SeleniumEmulation;
using OpenQA.Selenium;
using System.Threading;

namespace Selenium.Internal
{
internal class CommandTimer
{
private int timeout;
private object commandResult = null;
private SeleneseCommand command;
private IWebDriver driver;
private string[] args;

public CommandTimer(int timeout)
{
this.timeout = timeout;
}

public int Timeout
{
get { return timeout; }
set { timeout = value; }
}

public object Execute(SeleneseCommand command, IWebDriver driver, string[] args)
{
this.command = command;
this.driver = driver;
this.args = args;
Thread executionThread = new Thread(RunCommand);
executionThread.Start();
executionThread.Join(timeout);
if (executionThread.IsAlive)
{
throw new SeleniumException("Timed out running command");
}

return commandResult;
}

private void RunCommand()
{
commandResult = command.Apply(driver, args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ namespace Selenium.Internal.SeleniumEmulation
internal class Check : SeleneseCommand
{
private ElementFinder finder;

private SeleniumOptionSelector selector;

//public Check(ElementFinder elementFinder, SeleniumOptionSelector optionSelector)
public Check(ElementFinder elementFinder)
{
finder = elementFinder;
//selector = optionSelector;
}

protected override object HandleSeleneseCommand(IWebDriver driver, string locator, string value)
{
//if (locator.ToLowerInvariant().StartsWith("option "))
//{
// selector.Select(driver,
//}
IWebElement element = finder.FindElement(driver, locator);
element.Select();
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;

namespace Selenium.Internal.SeleniumEmulation.FilterFunctions
{
internal abstract class BaseFilterFunction : IFilterFunction
{

#region IFilterFunction Members

public List<IWebElement> FilterElements(IList<IWebElement> allElements, string filterValue)
{
List<IWebElement> toReturn = new List<IWebElement>();
foreach (IWebElement element in allElements)
{
if (ShouldAdd(element, filterValue))
{
toReturn.Add(element);
}
}

return toReturn;
}

#endregion

protected abstract bool ShouldAdd(IWebElement element, string filterValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;

namespace Selenium.Internal.SeleniumEmulation.FilterFunctions
{
internal class IndexFilterFunction : IFilterFunction
{
#region IFilterFunction Members

public List<IWebElement> FilterElements(IList<IWebElement> allElements, string filterValue)
{
int index = -1;
if (int.TryParse(filterValue, out index))
{
if (index < allElements.Count)
{
return new List<IWebElement>() { allElements[index] };
}
}

throw new SeleniumException("Element with index " + filterValue + " not found");
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;

namespace Selenium.Internal.SeleniumEmulation.FilterFunctions
{
internal class NameFilterFunction : BaseFilterFunction
{
protected override bool ShouldAdd(IWebElement element, string filterValue)
{
String name = element.GetAttribute("name");
return filterValue == name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;

namespace Selenium.Internal.SeleniumEmulation.FilterFunctions
{
internal class ValueFilterFunction : BaseFilterFunction
{
protected override bool ShouldAdd(IWebElement element, string filterValue)
{
string elementValue = element.Value;
return filterValue == elementValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ internal class IdentifierLookupStrategy : ILookupStrategy
{
public IWebElement Find(OpenQA.Selenium.IWebDriver driver, string use)
{
return driver.FindElement(By.Id(use));
try
{
return new IdLookupStrategy().Find(driver, use);
}
catch (NoSuchElementException)
{
return new NameLookupStrategy().Find(driver, use);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,75 @@
using System;
using OpenQA.Selenium;
using System.Collections.Generic;
using Selenium.Internal.SeleniumEmulation;
using Selenium.Internal.SeleniumEmulation.FilterFunctions;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;

namespace Selenium
{
internal class NameLookupStrategy : ILookupStrategy
{
private Dictionary<string, IFilterFunction> filterFunctions = new Dictionary<string, IFilterFunction>();
private Regex nameAndValueMatcherRegex = new Regex("^([A-Za-z]+)=(.+)");

public NameLookupStrategy()
{
filterFunctions.Add("value", new ValueFilterFunction());
filterFunctions.Add("name", new NameFilterFunction());
filterFunctions.Add("index", new IndexFilterFunction());
}

public IWebElement Find(OpenQA.Selenium.IWebDriver driver, string use)
{
return driver.FindElement(By.Name(use));
string[] parts = use.Split(new char[] { ' ' });

ReadOnlyCollection<IWebElement> allElements = driver.FindElements(By.Name(parts[0]));
List<IWebElement> filteredElements = new List<IWebElement>(allElements);

for (int i = 1; i < parts.Length; i++)
{
IFilterFunction filterBy = GetFilterFunction(parts[i]);

if (filterBy == null)
{
throw new SeleniumException(use + " not found. Cannot find filter for: " + parts[i]);
}

string filterValue = GetFilterValue(parts[i]);
filteredElements = filterBy.FilterElements(allElements, filterValue);
}

if (filteredElements != null && filteredElements.Count > 0)
{
return filteredElements[0];
}

throw new SeleniumException(use + " not found");
}

private string GetFilterValue(string originalFilterValue)
{
Match filterValueMatch = nameAndValueMatcherRegex.Match(originalFilterValue);
if (filterValueMatch.Success)
{
return filterValueMatch.Groups[2].ToString();
}
return originalFilterValue;
}

private IFilterFunction GetFilterFunction(string originalFilter)
{
string filterName = "value";

Match filterValueMatch = nameAndValueMatcherRegex.Match(originalFilter);
if (filterValueMatch.Success)
{
filterName = filterValueMatch.Groups[1].ToString();
}

return filterFunctions[filterName];
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ protected override object HandleSeleneseCommand(IWebDriver driver, string url, s

private string ConstructUrl(string appUrl, string path)
{
string urlToOpen = appUrl.Contains("://") ?
appUrl :
baseUrl + (!appUrl.StartsWith("/", StringComparison.Ordinal) ? "/" : string.Empty) + path;
string urlToOpen = path.Contains("://") ?
path :
baseUrl + (!path.StartsWith("/", StringComparison.Ordinal) ? "/" : string.Empty) + path;

return urlToOpen;
}
Expand Down
Loading

0 comments on commit b27dba9

Please sign in to comment.