Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ namespace Sample
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using RazorEngine.Templating;
using SimpleBrowser;

internal class Program
{
private static void Main(string[] args)
private static async Task Main(string[] args)
{
Browser browser = new Browser();
using Browser browser = new Browser();
try
{
// log the browser request/response data to files so we can interrogate them in case of an issue with our scraping
Expand All @@ -27,7 +29,7 @@ private static void Main(string[] args)
browser.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";

// browse to GitHub
browser.Navigate("http://github.com/");
await browser.NavigateAsync("http://github.com/");
if (LastRequestFailed(browser))
{
// always check the last request in case the page failed to load
Expand All @@ -43,7 +45,7 @@ private static void Main(string[] args)
}
else
{
loginLink.Click();
await loginLink.ClickAsync();
if (LastRequestFailed(browser))
{
return;
Expand All @@ -52,7 +54,7 @@ private static void Main(string[] args)
// fill in the form and click the login button - the fields are easy to locate because they have ID attributes
browser.Find("login_field").Value = "[email protected]";
browser.Find("password").Value = "yourpassword";
browser.Find(ElementType.Button, "name", "commit").Click();
await browser.Find(ElementType.Button, "name", "commit").ClickAsync();
if (LastRequestFailed(browser))
{
return;
Expand Down Expand Up @@ -89,7 +91,9 @@ private static void Main(string[] args)
}
finally
{
string path = WriteFile("log-" + DateTime.UtcNow.Ticks + ".html", browser.RenderHtmlLogFile("SimpleBrowser Sample - Request Log"));
RenderService rsvc = new RenderService();

string path = WriteFile("log-" + DateTime.UtcNow.Ticks + ".html", browser.RenderHtmlLogFile( rsvc, "SimpleBrowser Sample - Request Log"));

Console.WriteLine("Log file published to:");
Console.WriteLine(path);
Expand Down Expand Up @@ -135,4 +139,13 @@ private static string WriteFile(string filename, string text)
return path;
}
}

public class RenderService : HtmlLogFormatter.IViewRenderService
{
public string RenderToString<TModel>(string template, string title, TModel model)
{

return RazorEngine.Engine.Razor.RunCompile(template, title, model.GetType(), model);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this solution. It looks like RazorLight has simply been replaced by RazorEngine. Was that the intention? I thought you wanted to be able to create a production library that didn't include a dependency on Razor.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention was to remove the dependency from the library, so I used a dependency injection pattern (using the IViewRenderService interface that only implements this method). In the sample program (which is not part of the nuget) I just show that RazorEngine could be used to render the log to string, but anything that implements the interface can be used. And if you like to use RazorLight, go ahead. The Interface doesn't care.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used this as a step to move forward. I removed the dependency on RazorLight from SimpleBrowser and put all of the rendering stuff into a separate library by implementing the interface and setting the renderer in SimpleBrowser. This way, any renderer can be implemented - razor, json, email, etc.

}
}
}
7 changes: 6 additions & 1 deletion Sample/Sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
<Copyright>Copyright © 2010 - 2019, Nathan Ridley and the SimpleBrowser contributors.</Copyright>
<Authors>Nathan Ridley and the SimpleBrowser contributors.</Authors>
<AssemblyTitle>Sample</AssemblyTitle>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<DebugType>portable</DebugType>
<LangVersion>8.0</LangVersion>
<AssemblyName>Sample</AssemblyName>
<OutputType>Exe</OutputType>
<StartupObject>Sample.Program</StartupObject>
Expand All @@ -21,6 +22,10 @@
<WarningsAsErrors />
<PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RazorEngine.NetCore" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SimpleBrowser\SimpleBrowser.csproj" />
Expand Down
28 changes: 16 additions & 12 deletions SimpleBrowser.UnitTests/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace SimpleBrowser.UnitTests
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Moq;
using SimpleBrowser.Network;

Expand Down Expand Up @@ -49,7 +50,7 @@ public IHttpWebRequest GetWebRequest(Uri url)
{
Mock<IHttpWebRequest> mock = new Mock<IHttpWebRequest>();
mock.SetupAllProperties();
mock.Setup(m => m.GetResponse())
mock.Setup(m => m.GetResponseAsync())
.Returns(() =>
{
Mock<IHttpWebResponse> mockResponse = new Mock<IHttpWebResponse>();
Expand All @@ -58,10 +59,11 @@ public IHttpWebRequest GetWebRequest(Uri url)

byte[] responseContent = Encoding.UTF8.GetBytes(this.ResponseContent);
mockResponse.Setup(r => r.GetResponseStream()).Returns(new MemoryStream(responseContent));
return mockResponse.Object;
return Task.FromResult(mockResponse.Object);
});
mock.SetupProperty(m => m.Headers, new WebHeaderCollection());
mock.Setup(m => m.GetRequestStream()).Returns(new MemoryStream(new byte[2000000]));
mock.Setup(m => m.GetRequestStreamAsync()).Returns(Task.FromResult((System.IO.Stream)new MemoryStream(new byte[60000])));

return mock.Object;
}

Expand All @@ -81,7 +83,7 @@ public IHttpWebRequest GetWebRequest(Uri url)
{
Mock<IHttpWebRequest> mock = new Mock<IHttpWebRequest>();
mock.SetupAllProperties();
mock.Setup(m => m.GetResponse())
mock.Setup(m => m.GetResponseAsync())
.Returns(() =>
{
Mock<IHttpWebResponse> mockResponse = new Mock<IHttpWebResponse>();
Expand Down Expand Up @@ -110,10 +112,11 @@ public IHttpWebRequest GetWebRequest(Uri url)
}
}
mockResponse.Setup(r => r.GetResponseStream()).Returns(new MemoryStream(responseContent));
return mockResponse.Object;
return Task.FromResult(mockResponse.Object);
});
mock.SetupProperty(m => m.Headers, new WebHeaderCollection());
mock.Setup(m => m.GetRequestStream()).Returns(new MemoryStream(new byte[20000]));
mock.Setup(m => m.GetRequestStreamAsync()).Returns(Task.FromResult((System.IO.Stream)new MemoryStream(new byte[60000])));

return mock.Object;
}

Expand All @@ -131,7 +134,7 @@ public IHttpWebRequest GetWebRequest(Uri url)
{
Mock<IHttpWebRequest> mock = new Mock<IHttpWebRequest>();
mock.SetupAllProperties();
mock.Setup(m => m.GetResponse())
mock.Setup(m => m.GetResponseAsync())
.Returns(() =>
{
Mock<IHttpWebResponse> mockResponse = new Mock<IHttpWebResponse>();
Expand All @@ -151,10 +154,10 @@ public IHttpWebRequest GetWebRequest(Uri url)
}
}
mockResponse.Setup(r => r.GetResponseStream()).Returns(new MemoryStream(responseContent));
return mockResponse.Object;
return Task.FromResult(mockResponse.Object);
});
mock.SetupProperty(m => m.Headers, new WebHeaderCollection());
mock.Setup(m => m.GetRequestStream()).Returns(new MemoryStream(new byte[20000]));
mock.Setup(m => m.GetRequestStreamAsync()).Returns(Task.FromResult((System.IO.Stream)new MemoryStream(new byte[60000]) ));
return mock.Object;
}
}
Expand All @@ -170,7 +173,7 @@ public IHttpWebRequest GetWebRequest(Uri url)
{
Mock<IHttpWebRequest> mock = new Mock<IHttpWebRequest>();
mock.SetupAllProperties();
mock.Setup(m => m.GetResponse())
mock.Setup(m => m.GetResponseAsync())
.Returns(() =>
{
Mock<IHttpWebResponse> mockResponse = new Mock<IHttpWebResponse>();
Expand All @@ -190,10 +193,11 @@ public IHttpWebRequest GetWebRequest(Uri url)
}
}
mockResponse.Setup(r => r.GetResponseStream()).Returns(new MemoryStream(responseContent));
return mockResponse.Object;
return Task.FromResult( mockResponse.Object );
});
mock.SetupProperty(m => m.Headers, new WebHeaderCollection());
mock.Setup(m => m.GetRequestStream()).Returns(new MemoryStream(new byte[20000]));
mock.Setup(m => m.GetRequestStreamAsync()).Returns(Task.FromResult((System.IO.Stream)new MemoryStream(new byte[60000])));

return mock.Object;
}
}
Expand Down
9 changes: 5 additions & 4 deletions SimpleBrowser.UnitTests/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
namespace SimpleBrowser.UnitTests
{
using NUnit.Framework;
using System.Threading.Tasks;

[TestFixture]
public class Issues
{
[Test]
public void SampleApp()
public async Task SampleApp()
{
Browser b = new Browser(Helper.GetMoviesRequestMocker());
HttpRequestLog lastRequest = null;
b.RequestLogged += (br, l) =>
{
lastRequest = l;
};
b.Navigate("http://localhost/movies/");
await b.NavigateAsync("http://localhost/movies/");
HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Create New");
link.Click();
await link.ClickAsync();
HtmlResult box = b.Select("input[name=Title]");
box.Value = "1234";
box = b.Select("input[name=ReleaseDate]");
Expand All @@ -35,7 +36,7 @@ public void SampleApp()
box = b.Select("input[name=Rating]");
box.Value = "***";
link = b.Select("input[type=submit]");
link.Click();
await link.ClickAsync();
Assert.That(b.LastWebException == null, "Webexception detected");
Assert.That(lastRequest.PostBody.Contains("&Price=51&"));
}
Expand Down
17 changes: 9 additions & 8 deletions SimpleBrowser.UnitTests/OfflineTests/FileUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ namespace SimpleBrowser.UnitTests.OfflineTests
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;

[TestFixture]
public class FileUri
{
[Test]
public void CanLoadHtmlFromFile()
public async Task CanLoadHtmlFromFile()
{
FileInfo f = null;
string uri = string.Empty;
Expand All @@ -37,12 +38,12 @@ public void CanLoadHtmlFromFile()
}

Browser b = new Browser();
b.Navigate(uri);
await b.NavigateAsync(uri);
Assert.AreEqual(b.Select("ul#menu>li").Count(), 3, "Not loaded");
}

[Test]
public void CanLoadHtmlFromFilesWithAbsolutePath()
public async Task CanLoadHtmlFromFilesWithAbsolutePath()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Directory.Exists("C:\\Windows\\Temp"))
Expand All @@ -52,16 +53,16 @@ public void CanLoadHtmlFromFilesWithAbsolutePath()
@"C:\Windows\Temp\movies1.htm", true);

Browser b = new Browser();
b.Navigate("file:///c:/Windows/Temp/movies1.htm");
await b.NavigateAsync("file:///c:/Windows/Temp/movies1.htm");
Assert.AreEqual(b.Select("ul#menu>li").Count(), 3);

b.Navigate("file:///c|/Windows/Temp/movies1.htm");
await b.NavigateAsync("file:///c|/Windows/Temp/movies1.htm");
Assert.AreEqual(b.Select("ul#menu>li").Count(), 3);

b.Navigate("file:///c|\\Windows\\Temp\\movies1.htm");
await b.NavigateAsync("file:///c|\\Windows\\Temp\\movies1.htm");
Assert.AreEqual(b.Select("ul#menu>li").Count(), 3);

b.Navigate("file://\\c|\\Windows\\Temp\\movies1.htm");
await b.NavigateAsync("file://\\c|\\Windows\\Temp\\movies1.htm");
Assert.AreEqual(b.Select("ul#menu>li").Count(), 3);

File.Delete(@"C:\Windows\Temp\movies1.htm");
Expand All @@ -74,7 +75,7 @@ public void CanLoadHtmlFromFilesWithAbsolutePath()
@"/tmp/movies1.htm", true);

Browser b = new Browser();
b.Navigate("file:///tmp/movies1.htm");
await b.NavigateAsync("file:///tmp/movies1.htm");
Assert.AreEqual(b.Select("ul#menu>li").Count(), 3);

File.Delete(@"/tmp/movies1.htm");
Expand Down
Loading