-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathProgram.cs
More file actions
151 lines (134 loc) · 6.31 KB
/
Program.cs
File metadata and controls
151 lines (134 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// -----------------------------------------------------------------------
// <copyright file="Program.cs" company="SimpleBrowser">
// Copyright © 2010 - 2019, Nathan Ridley and the SimpleBrowser contributors.
// See https://github.com/SimpleBrowserDotNet/SimpleBrowser/blob/master/readme.md
// </copyright>
// -----------------------------------------------------------------------
namespace Sample
{
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using RazorEngine.Templating;
using SimpleBrowser;
internal class Program
{
private static async Task Main(string[] args)
{
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
browser.RequestLogged += OnBrowserRequestLogged;
browser.MessageLogged += new Action<Browser, string>(OnBrowserMessageLogged);
// we'll fake the user agent for websites that alter their content for unrecognised browsers
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
await browser.NavigateAsync("http://github.com/");
if (LastRequestFailed(browser))
{
// always check the last request in case the page failed to load
return;
}
// click the login link and click it
browser.Log("First we need to log in, so browse to the login page, fill in the login details and submit the form.");
HtmlResult loginLink = browser.Find("a", FindBy.Text, "Sign in");
if (!loginLink.Exists)
{
browser.Log("Can't find the login link! Perhaps the site is down for maintenance?");
}
else
{
await loginLink.ClickAsync();
if (LastRequestFailed(browser))
{
return;
}
// 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 = "youremail@domain.com";
browser.Find("password").Value = "yourpassword";
await browser.Find(ElementType.Button, "name", "commit").ClickAsync();
if (LastRequestFailed(browser))
{
return;
}
// see if the login succeeded - ContainsText() is very forgiving, so don't worry about whitespace, casing, html tags separating the text, etc.
if (browser.ContainsText("Incorrect username or password"))
{
browser.Log("Login failed!", LogMessageType.Error);
}
else
{
// After logging in, we should check that the page contains elements that we recognise
if (!browser.ContainsText("Your Repositories"))
{
browser.Log("There wasn't the usual login failure message, but the text we normally expect isn't present on the page");
}
else
{
browser.Log("Your News Feed:");
// we can use simple jquery selectors, though advanced selectors are yet to be implemented
foreach (HtmlResult item in browser.Select("div.news .title"))
{
browser.Log("* " + item.Value);
}
}
}
}
}
catch (Exception ex)
{
browser.Log(ex.Message, LogMessageType.Error);
browser.Log(ex.StackTrace, LogMessageType.StackTrace);
}
finally
{
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);
var process = new Process();
process.StartInfo.FileName = path;
process.StartInfo.UseShellExecute = true;
process.Start();
}
}
private static bool LastRequestFailed(Browser browser)
{
if (browser.LastWebException != null)
{
browser.Log("There was an error loading the page: " + browser.LastWebException.Message);
return true;
}
return false;
}
private static void OnBrowserMessageLogged(Browser browser, string log)
{
Console.WriteLine(log);
}
private static void OnBrowserRequestLogged(Browser req, HttpRequestLog log)
{
Console.WriteLine(" -> " + log.Method + " request to " + log.Url);
Console.WriteLine(" <- Response status code: " + log.ResponseCode);
}
private static string WriteFile(string filename, string text)
{
DirectoryInfo dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Logs"));
if (!dir.Exists)
{
dir.Create();
}
string path = Path.Combine(dir.FullName, filename);
File.WriteAllText(path, 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);
}
}
}