public void Holding_Ctrl_Shft_Opens_New_Window() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; b.Navigate("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); link.Click(); Assert.That(b.Windows.Count() == 1); link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); b.KeyState = KeyStateOption.Ctrl; link.Click(); Assert.That(b.Windows.Count() == 2); link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); b.KeyState = KeyStateOption.Shift; link.Click(); Assert.That(b.Windows.Count() == 3); link = b.Find(ElementType.Anchor, FindBy.Text, "Home"); b.KeyState = KeyStateOption.Alt; link.Click(); Assert.That(b.Windows.Count() == 3); // alt does not open new browser }
public void SampleApp() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; b.Navigate("http://localhost/movies/"); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Create New"); link.Click(); 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]"); link.Click(); Assert.That(b.LastWebException == null, "Webexception detected"); Assert.That(lastRequest.PostBody.Contains("&Price=51&")); }
public void 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 = b.Navigate(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://afn.org/~afn07998/simplebrowser/testmeta.htm"); string targetHref = link.GetAttribute("href"); Assert.AreEqual(targetHref, "http://afn.org/~afn07998/simplebrowser/testmeta.htm"); link.Click(); Assert.IsNotNull(b.CurrentState); Assert.IsNotNull(b.Referer); Assert.AreEqual(b.Referer.ToString(), startingUrl); }
public void After_Navigating_Away_HtmlResult_Should_Throw_Exception() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; b.Navigate("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Create New"); link.Click(); Assert.AreEqual(new Uri("http://localhost/movies/Movies/Create"), b.Url); Assert.Throws(typeof(InvalidOperationException), () => link.Click(), "Clicking the link should now throw an exception"); }
public void Navigating_To_A_Url_With_Querystring_Parameters_Retains_Parameters() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); b.Navigate("http://localhost/movies/"); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "Rio Bravo"); link.Click(); Assert.AreEqual(new Uri("http://www.example.com/movie.html?id=4"), b.Url); }
public void Clicking_Target_Blank() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; b.Navigate("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); link.Click(); Assert.That(b.Url == new Uri("http://localhost/movies/")); Assert.That(b.Windows.Count() == 2); link.Click(); 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 void Forms_Validate_Input_Elements() { Browser b = new Browser(); b.SetContent(Helper.GetFromResources("SimpleBrowser.UnitTests.SampleDocs.HTML5Elements.htm")); // Test text input properties var testinput = b.Find("textinput"); testinput.Value = "text input updated"; Assert.IsTrue(testinput.Value == "text input updated"); Assert.IsTrue(testinput.TotalElementsFound == 1); Assert.IsFalse(testinput.Checked); // Test text area properties testinput = b.Find("textareainput"); testinput.Value = "text area input updated"; Assert.IsTrue(testinput.Value == "text area input updated"); Assert.IsTrue(testinput.TotalElementsFound == 1); Assert.IsFalse(testinput.Checked); // Test checkbox input properties testinput = b.Find("checkboxinput"); string ads = testinput.Value; testinput.Value = "text area input updated"; Assert.IsTrue(testinput.Value == "text area input updated"); Assert.IsTrue(testinput.TotalElementsFound == 1); Assert.IsFalse(testinput.Checked); // Submit the form HtmlResult submit = b.Find("es"); var clickResult = submit.Click(); Assert.IsTrue(clickResult == ClickResult.SucceededNavigationComplete); // Check to make sure the form submitted. var names = b.Select("td.desc"); var values = b.Select("td.val"); Assert.IsTrue(names.Count() == values.Count()); // Check to make sure the proper values submitted Assert.IsTrue(values.Where(e => e.Value == "text input updated").FirstOrDefault() != null); Assert.IsTrue(values.Where(e => e.Value == "text area input updated").FirstOrDefault() != null); }
public void Accessing_New_Windows_Using_Event() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); Browser newlyOpened = null; b.NewWindowOpened += (b1, b2) => { newlyOpened = b2; }; b.Navigate("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; link.Click(); 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 void When_Testing_Referer_RelNoReferrer() { string startingUrl = "http://yenc-post.org/simplebrowser/testrel.htm"; Browser b = new Browser(); Assert.AreEqual(b.RefererMode, Browser.RefererModes.NoneWhenDowngrade); bool success = b.Navigate(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find("test1"); Assert.IsNotNull(link); link.Click(); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); }
public void When_Testing_Referer_NoneWhenDowngrade_Typical() { string startingUrl = "http://afn.org/~afn07998/simplebrowser/test1.htm"; Browser b = new Browser(); Assert.AreEqual(b.RefererMode, Browser.RefererModes.NoneWhenDowngrade); bool success = b.Navigate(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find("test1"); Assert.IsNotNull(link); link.Click(); Assert.IsNotNull(b.CurrentState); Assert.AreEqual(b.Referer.ToString(), startingUrl); }
public void ClosingBrowsers() { Browser b = new Browser(Helper.GetMoviesRequestMocker()); HttpRequestLog lastRequest = null; b.RequestLogged += (br, l) => { lastRequest = l; }; b.Navigate("http://localhost/movies/"); Assert.That(b.Url == new Uri("http://localhost/movies/")); HtmlResult link = b.Find(ElementType.Anchor, FindBy.Text, "About us"); link.Click(); 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 void When_Testing_Referer_NoneWhenDowngrade_Secure_Transition() { string startingUrl = "https://www.codeproject.com"; Browser b = new Browser(); Assert.AreEqual(b.RefererMode, Browser.RefererModes.NoneWhenDowngrade); bool success = b.Navigate(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find("ctl00_AdvertiseLink"); Assert.IsNotNull(link); link.Click(); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); }
public void 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 = b.Navigate(startingUrl); Assert.IsTrue(success); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); HtmlResult link = b.Find(ElementType.Anchor, "href", "/"); Assert.IsNotNull(link); link.Click(); Assert.IsNotNull(b.CurrentState); Assert.AreEqual(b.Referer.ToString(), "http://www.iana.org/"); }
public void 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 = b.Navigate(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); link.Click(); Assert.IsNotNull(b.CurrentState); Assert.IsNull(b.Referer); }
public void Forms_Disabled_and_ReadOnly() { Browser b = new Browser(); b.SetContent(Helper.GetFromResources("SimpleBrowser.UnitTests.SampleDocs.HTML5Elements.htm")); var textarea = b.Find("readonlytextarea"); textarea.Value = "some value"; Assert.IsTrue(textarea.Value == "readme textarea"); Assert.IsTrue(textarea.ReadOnly); Assert.IsFalse(textarea.Disabled); textarea = b.Find("disabledtextarea"); textarea.Value = "some value"; Assert.IsTrue(textarea.Value == "disableme textarea"); Assert.IsFalse(textarea.ReadOnly); Assert.IsTrue(textarea.Disabled); var textinput = b.Find("readonlytext"); textinput.Value = "some value"; Assert.IsTrue(textinput.Value == "readme"); Assert.IsTrue(textinput.ReadOnly); Assert.IsFalse(textinput.Disabled); textinput = b.Find("disabledtext"); textinput.Value = "some value"; Assert.IsTrue(textinput.Value == "disableme"); Assert.IsFalse(textinput.ReadOnly); Assert.IsTrue(textinput.Disabled); var checkbox = b.Find("disabledcheck"); Assert.IsTrue(checkbox.Disabled); var radio = b.Find("disabledradio"); Assert.IsTrue(radio.Disabled); HtmlResult disabledSubmit = b.Find("ds"); Assert.IsTrue(disabledSubmit.Disabled); ClickResult clickResult = disabledSubmit.Click(); Assert.IsTrue(clickResult == ClickResult.SucceededNoOp); HtmlResult submit = b.Find("es"); Assert.IsFalse(submit.Disabled); clickResult = submit.Click(); Assert.IsTrue(clickResult == ClickResult.SucceededNavigationComplete); // Check to make sure the form submitted. var names = b.Select("td.desc"); var values = b.Select("td.val"); Assert.IsTrue(names.Count() == values.Count()); Assert.IsTrue(values.Where(e => e.Value == "readme textarea").FirstOrDefault() != null); // Now, validate that the disabled fields were not. Assert.IsTrue(values.Where(e => e.Value == "disableme textarea").FirstOrDefault() == null); Assert.IsTrue(values.Where(e => e.Value == "disableme").FirstOrDefault() == null); Assert.IsTrue(values.Where(e => e.Value == "disabledcheck").FirstOrDefault() == null); Assert.IsTrue(values.Where(e => e.Value == "disabledradio").FirstOrDefault() == null); Assert.IsTrue(values.Where(e => e.Value == "disabledselect").FirstOrDefault() == null); }
private static void Main(string[] args) { 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 browser.Navigate("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 { loginLink.Click(); 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"; browser.Find(ElementType.Button, "name", "commit").Click(); 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 { string path = WriteFile("log-" + DateTime.UtcNow.Ticks + ".html", browser.RenderHtmlLogFile("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(); } }
public void Click() { _htmlResult.Click(); }