public async Task Holding_Ctrl_Shft_Opens_New_Window() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; await b.NavigateAsync("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); await link.ClickAsync(); Assert.That(b.Windows.Count() == 1); link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); b.KeyState = KeyStateOption.Ctrl; await link.ClickAsync(); Assert.That(b.Windows.Count() == 2); link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); b.KeyState = KeyStateOption.Shift; await link.ClickAsync(); Assert.That(b.Windows.Count() == 3); link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); b.KeyState = KeyStateOption.Alt; await link.ClickAsync(); Assert.That(b.Windows.Count() == 3); // alt does not open new browser }
public async Task SampleApp() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; await b.NavigateAsync("http://localhost/movies/"); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Create New"); await link.ClickAsync(); HtmlResult box = b.Select("input[name=Title]"); box.Value = "1234"; box = b.Select("input[name=ReleaseDate]"); box.Value = "2011-01-01"; box = b.Select("input[name=Genre]"); box.Value = "dark"; box = b.Select("input[name=Price]"); box.Value = "51"; box = b.Select("input[name=Rating]"); box.Value = "***"; link = b.Select("input[type=submit]"); await link.ClickAsync(); Assert.That(b.LastWebException == null, "Webexception detected"); Assert.That(lastRequest.PostBody.Contains("&Price=51&")); }
public async Task When_Testing_Referer_Unsafe_Url_Secure_Transition() { string startingUrl = "https://www.codeproject.com/"; Browser b = new Browser(); b.RefererMode = Browser.RefererModes.UnsafeUrl; Assert.AreEqual(b.RefererMode, Browser.RefererModes.UnsafeUrl); bool success = await b.NavigateAsync(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find("ctl00_AdvertiseLink"); Assert.IsNotNull(link); link.XElement.SetAttributeValue("href", "http://yenc-post.org/simplebrowser/testmeta.htm"); string targetHref = link.GetAttribute("href"); Assert.AreEqual(targetHref, "http://yenc-post.org/simplebrowser/testmeta.htm"); await link.ClickAsync(); Assert.IsNotNull(b.CurrentState); Assert.IsNotNull(b.Referer); Assert.AreEqual(b.Referer.ToString(), startingUrl); }
public async Task After_Navigating_Away_HtmlResult_Should_Throw_Exception() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; await b.NavigateAsync("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Create New"); await link.ClickAsync(); Assert.AreEqual(new Uri("http://localhost/movies/Movies/Create"), b.Url); Assert.Throws(typeof(InvalidOperationException), () => link.ClickAsync().GetAwaiter().GetResult(), "Clicking the link should now throw an exception"); }
public async Task Navigating_To_A_Url_With_Querystring_Parameters_Retains_Parameters() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); await b.NavigateAsync("http://localhost/movies/"); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Rio Bravo"); await link.ClickAsync(); Assert.AreEqual(new Uri("http://www.example.com/movie.html?id=4"), b.Url); }
public async Task Clicking_Target_Blank() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; await b.NavigateAsync("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); await link.ClickAsync(); Assert.That(b.Url == new Uri("http://localhost/movies/")); Assert.That(b.Windows.Count() == 2); await link.ClickAsync(); Assert.That(b.Windows.Count() == 3); Browser newBrowserWindow = b.Windows.First(br => br.WindowHandle != b.WindowHandle); Assert.That(newBrowserWindow.Url == new Uri("http://localhost/movies/About")); }
public async Task Accessing_New_Windows_Using_Event() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); Browser newlyOpened = null; b.NewWindowOpened += (b1, b2) => { newlyOpened = b2; }; await b.NavigateAsync("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Details"); b.KeyState = KeyStateOption.Ctrl; await link.ClickAsync(); Assert.That(b.Windows.Count() == 2); Assert.NotNull(newlyOpened); Assert.That(b.Url.ToString() == "http://localhost/movies/"); Assert.That(newlyOpened.Url.ToString() == "http://localhost/movies/Movies/Details/1"); }
public async Task When_Testing_Referer_NoneWhenDowngrade_Typical() { string startingUrl = "http://yenc-post.org/simplebrowser/test1.htm"; Browser b = new Browser(); Assert.AreEqual(b.RefererMode, Browser.RefererModes.NoneWhenDowngrade); bool success = await b.NavigateAsync(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find("test1"); Assert.IsNotNull(link); await link.ClickAsync(); Assert.IsNotNull(b.CurrentState); Assert.AreEqual(b.Referer.ToString(), startingUrl); }
public async Task ClosingBrowsers() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; await b.NavigateAsync("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); await link.ClickAsync(); Assert.That(b.Url == new Uri("http://localhost/movies/")); Assert.That(b.Windows.Count() == 2); b.Close(); Assert.That(b.Windows.Count() == 1); b.Windows.First().Close(); Assert.That(b.Windows.Count() == 0); Assert.Throws(typeof(ObjectDisposedException), () => { Uri s = b.Url; }); }
public async Task When_Testing_Referer_NoneWhenDowngrade_Secure_Transition() { string startingUrl = "https://www.greatrace.com/"; Browser b = new Browser(); Assert.AreEqual(b.RefererMode, Browser.RefererModes.NoneWhenDowngrade); bool success = await b.NavigateAsync(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find(ElementType.Anchor, "href", "http://www.timewise.us/"); link.XElement.RemoveAttributeCI("target"); Assert.IsNotNull(link); await link.ClickAsync(); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); }
public async Task When_Testing_Referer_Origin_Typical() { string startingUrl = "http://www.iana.org/domains/reserved"; Browser b = new Browser(); b.RefererMode = Browser.RefererModes.Origin; Assert.AreEqual(b.RefererMode, Browser.RefererModes.Origin); bool success = await b.NavigateAsync(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find(ElementType.Anchor, "href", "/"); Assert.IsNotNull(link); await link.ClickAsync(); Assert.IsNotNull(b.CurrentState); Assert.AreEqual(b.Referer.ToString(), "http://www.iana.org/"); }
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 = "*****@*****.**"; 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(); } }