Skip to content
Browse files

Implemented Check(), Uncheck(), and Select() for WebDriver. You can a…

…lso now Visit() an absolute url with WebDriver.
  • Loading branch information...
1 parent f8b8480 commit e41914e4eba51ffbb10ed3a7e2d6eca8b38ce2af @remi committed
Showing with 59 additions and 13 deletions.
  1. +4 −4 Mara.Drivers.WebDriver/Element.cs
  2. +55 −9 Mara.Drivers.WebDriver/WebDriver.cs
View
8 Mara.Drivers.WebDriver/Element.cs
@@ -36,17 +36,17 @@ public class Element : IElement {
const string NO_VALUE_ATTRIBUTE_MESSAGE = "Element does not have a value attribute";
- internal static List<IElement> List(ReadOnlyCollection<IWebElement> nativeElements, WebDriver webDriver) {
+ public static List<IElement> List(ReadOnlyCollection<IWebElement> nativeElements, WebDriver webDriver) {
var list = new List<IElement>();
foreach (var webElement in nativeElements)
list.Add(new Element(webElement, webDriver));
return list;
}
- IWebElement NativeElement { get; set; }
- WebDriver ParentDriver { get; set; }
+ public IWebElement NativeElement { get; set; }
+ public WebDriver ParentDriver { get; set; }
- internal Element(IWebElement nativeElement, WebDriver webDriver) {
+ public Element(IWebElement nativeElement, WebDriver webDriver) {
NativeElement = nativeElement;
ParentDriver = webDriver;
}
View
64 Mara.Drivers.WebDriver/WebDriver.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Linq;
using System.Diagnostics;
using System.Collections.Generic;
@@ -239,18 +240,21 @@ public partial class WebDriver : IDriver {
// TODO I don't like using Mara.AppHost ... ? using something static feels icky? maybe?
public void Visit(string path) {
- Mara.Log("Visit({0})", path);
- // The ChromeDriver hates life ...
+ var url = (path.StartsWith("/")) ? (Mara.AppHost + path) : path;
+
+ Mara.Log("Visit({0})", url);
+
+ // The ChromeDriver hates life sometimes ...
if (Browser == "chrome")
for (var i = 0; i < 10; i ++)
- if (TryToVisitInChrome(path))
+ if (TryToVisitInChrome(url))
break;
else {
- Mara.Log("Chrome didn't want to visit {0} ... trying again ... ", path);
+ Mara.Log("Chrome didn't want to visit {0} ... trying again ... ", url);
System.Threading.Thread.Sleep(100);
}
else
- webdriver.Navigate().GoToUrl(Mara.AppHost + path);
+ webdriver.Navigate().GoToUrl(url);
}
public void FillIn(string field, string value) {
@@ -278,15 +282,57 @@ public partial class WebDriver : IDriver {
}
public void Check(string checkbox) {
- throw new NotImplementedException("WebDriver Check() not implemented yet");
+ (CheckBox(checkbox) as WebDriver.Element).NativeElement.Select();
}
public void Uncheck(string checkbox) {
- throw new NotImplementedException("WebDriver Uncheck() not implemented yet");
+ var element = CheckBox(checkbox) as WebDriver.Element;
+ if (element.NativeElement.Selected)
+ element.NativeElement.Toggle();
+ }
+
+ IElement CheckBox(string checkbox) {
+ var id_XPath = "//input[@type='checkbox'][@id='" + checkbox + "']";
+ var name_XPath = "//input[@type='checkbox'][@name='" + checkbox + "']";
+
+ // try ID first, then name ...
+ var element = Find(id_XPath);
+ if (element == null)
+ element = Find(name_XPath);
+ if (element == null)
+ throw new ElementNotFoundException(id_XPath + " OR " + name_XPath);
+
+ return element;
}
public void Select(string dropdown, string option) {
- throw new NotImplementedException("WebDriver Select() not implemented yet");
+ // Find the <select>
+ var id_XPath = "//select[@id='" + dropdown + "']";
+ var name_XPath = "//select[@name='" + dropdown + "']";
+ var element = Find(id_XPath);
+ if (element == null)
+ element = Find(name_XPath);
+ if (element == null)
+ throw new ElementNotFoundException(id_XPath + " OR " + name_XPath);
+
+ // Find the <option>
+ //
+ // Mara needs to add IElement.Find() and IElement.All() to search for subqueries ... right now, we need to get the native WebDriver element
+ //
+ Element nativeElement = element as WebDriver.Element;
+ var options = WebDriver.Element.List(nativeElement.NativeElement.FindElements(By.TagName("option")), this);
+
+ // find an option with the matching text OR value for this select
+ var matchingOption = options.FirstOrDefault(o => {
+ var text = (o.Text == null) ? "" : o.Text.Trim();
+ var value = (o.Value == null) ? "" : o.Value.Trim();
+ return (text == option.Trim() || value == option.Trim());
+ });
+
+ if (matchingOption != null)
+ (matchingOption as WebDriver.Element).NativeElement.Select();
+ else
+ throw new ElementNotFoundException("<option> with Text or Value: " + option);
}
public string Body {
@@ -298,7 +344,7 @@ public partial class WebDriver : IDriver {
}
public string CurrentPath {
- get { return webdriver.Url.Replace(Mara.AppHost, ""); } // FIXME Don't use a Global Mara.Server
+ get { return new Uri(CurrentUrl).LocalPath; }
}
public bool JavaScriptSupported { get { return true; }}

0 comments on commit e41914e

Please sign in to comment.
Something went wrong with that request. Please try again.