2010-04-14 10:28:33 -07:00
|
|
|
|
using System;
|
2010-04-17 19:22:26 -07:00
|
|
|
|
using System.Collections.Generic;
|
2010-04-14 10:28:33 -07:00
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.Hosting;
|
2010-04-20 14:13:14 -07:00
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
using HtmlAgilityPack;
|
2010-04-14 10:28:33 -07:00
|
|
|
|
using Orchard.Specs.Hosting;
|
|
|
|
|
using Orchard.Specs.Util;
|
|
|
|
|
using TechTalk.SpecFlow;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
|
|
|
|
|
namespace Orchard.Specs.Bindings {
|
|
|
|
|
[Binding]
|
|
|
|
|
public class WebAppHosting {
|
|
|
|
|
private WebHost _webHost;
|
|
|
|
|
private RequestDetails _details;
|
2010-04-20 14:13:14 -07:00
|
|
|
|
private HtmlDocument _doc;
|
2010-04-17 19:22:26 -07:00
|
|
|
|
private MessageSink _messages;
|
2010-04-14 10:28:33 -07:00
|
|
|
|
|
|
|
|
|
[Given(@"I have a clean site")]
|
|
|
|
|
public void GivenIHaveACleanSite() {
|
|
|
|
|
_webHost = new WebHost();
|
|
|
|
|
_webHost.Initialize("Orchard.Web", "/");
|
2010-04-17 19:22:26 -07:00
|
|
|
|
|
|
|
|
|
var sink = new MessageSink();
|
|
|
|
|
_webHost.Execute(() => {
|
|
|
|
|
HostingTraceListener.SetHook(msg => sink.Receive(msg));
|
|
|
|
|
});
|
|
|
|
|
_messages = sink;
|
2010-04-14 10:28:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-04-17 19:22:26 -07:00
|
|
|
|
public class MessageSink : MarshalByRefObject {
|
|
|
|
|
readonly IList<string> _messages = new List<string>();
|
|
|
|
|
|
|
|
|
|
public void Receive(string message) {
|
|
|
|
|
_messages.Add(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-14 10:28:33 -07:00
|
|
|
|
[Given(@"I have module ""(.*)""")]
|
|
|
|
|
public void GivenIHaveModule(string moduleName) {
|
|
|
|
|
_webHost.CopyExtension("Modules", moduleName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Given(@"I have theme ""(.*)""")]
|
|
|
|
|
public void GivenIHaveTheme(string themeName) {
|
|
|
|
|
_webHost.CopyExtension("Themes", themeName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Given(@"I have core ""(.*)""")]
|
|
|
|
|
public void GivenIHaveCore(string moduleName) {
|
|
|
|
|
_webHost.CopyExtension("Core", moduleName);
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 14:27:43 -07:00
|
|
|
|
[Given(@"I have a clean site with")]
|
|
|
|
|
public void GivenIHaveACleanSiteWith(Table table) {
|
|
|
|
|
GivenIHaveACleanSite();
|
|
|
|
|
foreach (var row in table.Rows) {
|
|
|
|
|
switch (row["extension"]) {
|
|
|
|
|
case "core":
|
|
|
|
|
GivenIHaveCore(row["name"]);
|
|
|
|
|
break;
|
|
|
|
|
case "module":
|
|
|
|
|
GivenIHaveModule(row["name"]);
|
|
|
|
|
break;
|
|
|
|
|
case "theme":
|
|
|
|
|
GivenIHaveTheme(row["name"]);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Assert.Fail("Unknown extension type {0}", row["extension"]);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
[Given(@"I am on ""(.*)""")]
|
|
|
|
|
public void GivenIAmOn(string urlPath) {
|
|
|
|
|
WhenIGoTo(urlPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-14 10:28:33 -07:00
|
|
|
|
[When(@"I go to ""(.*)""")]
|
|
|
|
|
public void WhenIGoTo(string urlPath) {
|
|
|
|
|
_details = _webHost.SendRequest(urlPath);
|
2010-04-20 14:13:14 -07:00
|
|
|
|
_doc = new HtmlDocument();
|
|
|
|
|
_doc.Load(new StringReader(_details.ResponseText));
|
2010-04-14 10:28:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[When(@"I follow ""(.*)""")]
|
|
|
|
|
public void WhenIFollow(string linkText) {
|
2010-04-20 14:13:14 -07:00
|
|
|
|
var link = _doc.DocumentNode
|
|
|
|
|
.SelectNodes("//a")
|
|
|
|
|
.Single(elt => elt.InnerText == linkText);
|
|
|
|
|
|
|
|
|
|
var urlPath = link.Attributes["href"].Value;
|
2010-04-14 10:28:33 -07:00
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
WhenIGoTo(urlPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[When(@"I fill in")]
|
|
|
|
|
public void WhenIFillIn(Table table) {
|
|
|
|
|
var inputs = _doc.DocumentNode
|
|
|
|
|
.SelectNodes("//input");
|
|
|
|
|
|
|
|
|
|
foreach (var row in table.Rows) {
|
|
|
|
|
var r = row;
|
|
|
|
|
var input = inputs.Single(
|
|
|
|
|
x => x.Attributes.Contains("name") &&
|
|
|
|
|
x.Attributes["name"].Value == r["name"]);
|
|
|
|
|
input.Attributes.Add("value", row["value"]);
|
|
|
|
|
}
|
2010-04-14 10:28:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-04-20 14:13:14 -07:00
|
|
|
|
[When(@"I hit ""(.*)""")]
|
|
|
|
|
public void WhenIHit(string submitText) {
|
|
|
|
|
var submit = _doc.DocumentNode
|
|
|
|
|
.SelectNodes("//input[@type='submit']")
|
|
|
|
|
.Single(elt => elt.GetAttributeValue("value", null) == submitText);
|
|
|
|
|
|
|
|
|
|
var form = Form.LocateAround(submit);
|
|
|
|
|
var urlPath = form.Start.GetAttributeValue("action", _details.UrlPath);
|
|
|
|
|
var inputs = form.Children
|
|
|
|
|
.SelectMany(elt => elt.DescendantsAndSelf("input"))
|
|
|
|
|
.GroupBy(elt => elt.GetAttributeValue("name", elt.GetAttributeValue("id", "")), elt => elt.GetAttributeValue("value", ""))
|
|
|
|
|
.ToDictionary(elt => elt.Key, elt => (IEnumerable<string>)elt);
|
|
|
|
|
|
|
|
|
|
_details = _webHost.SendRequest(urlPath, inputs);
|
|
|
|
|
_doc = new HtmlDocument();
|
|
|
|
|
_doc.Load(new StringReader(_details.ResponseText));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-04-14 10:28:33 -07:00
|
|
|
|
[Then(@"the status should be (.*) (.*)")]
|
|
|
|
|
public void ThenTheStatusShouldBe(int statusCode, string statusDescription) {
|
|
|
|
|
Assert.That(_details.StatusCode, Is.EqualTo(statusCode));
|
|
|
|
|
Assert.That(_details.StatusDescription, Is.EqualTo(statusDescription));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Then(@"I should see ""(.*)""")]
|
|
|
|
|
public void ThenIShouldSee(string text) {
|
|
|
|
|
Assert.That(_details.ResponseText, Is.StringContaining(text));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Then(@"the title contains ""(.*)""")]
|
|
|
|
|
public void ThenTheTitleContainsText(string text) {
|
|
|
|
|
ScenarioContext.Current.Pending();
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-04-20 14:13:14 -07:00
|
|
|
|
|
|
|
|
|
public class Form {
|
|
|
|
|
public static Form LocateAround(HtmlNode cornerstone) {
|
|
|
|
|
foreach (var inspect in cornerstone.AncestorsAndSelf()) {
|
|
|
|
|
|
|
|
|
|
var form = inspect.PreviousSiblingsAndSelf().FirstOrDefault(
|
|
|
|
|
n => n.NodeType == HtmlNodeType.Element && n.Name == "form");
|
|
|
|
|
if (form == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var endForm = inspect.NextSiblingsAndSelf().FirstOrDefault(
|
|
|
|
|
n => n.NodeType == HtmlNodeType.Text && n.InnerText == "</form>");
|
|
|
|
|
if (endForm == null)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return new Form {
|
|
|
|
|
Start = form,
|
|
|
|
|
End = endForm,
|
|
|
|
|
Children = form.NextSibling.NextSiblingsAndSelf().TakeWhile(n => n != endForm).ToArray()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public HtmlNode Start { get; set; }
|
|
|
|
|
public HtmlNode End { get; set; }
|
|
|
|
|
public IEnumerable<HtmlNode> Children { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class HtmlExtensions {
|
|
|
|
|
public static IEnumerable<HtmlNode> PreviousSiblingsAndSelf(this HtmlNode node) {
|
|
|
|
|
var scan = node;
|
|
|
|
|
while (scan != null) {
|
|
|
|
|
yield return scan;
|
|
|
|
|
scan = scan.PreviousSibling;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public static IEnumerable<HtmlNode> NextSiblingsAndSelf(this HtmlNode node) {
|
|
|
|
|
var scan = node;
|
|
|
|
|
while (scan != null) {
|
|
|
|
|
yield return scan;
|
|
|
|
|
scan = scan.NextSibling;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-04-14 10:28:33 -07:00
|
|
|
|
}
|