Karl Cassar
Karl Cassar

Reputation: 6473

emulating a browser programmatically in C# / .Net

We would like to automate certain tasks in a website, like having a user 'login', perform some functionality, read their account history etc.

We have tried emulating this with normal POST/GETs, however the problem is that for example for 'login', the website uses javascript code to execute an AJAX call, and also generate some random tokens.

Is it possible to literally emulate a web-browser? For example:

The above could be translated into say the below sample code:

var browser = new Browser();
var pageHomepage = browser.Load("www.test-domain.com");
pageHomepage.DOM.GetField("username").SetValue("testUser");
pageHomepage.DOM.GetField("password").SetValue("testPass");
pageHomepage.DOM.GetField("btnSubmit").Click();
var pageAccountHistory = browser.Load("www.test-domain.com/account-history/");
var html = pageAccountHistory.GetHtml();
var historyItems = parseHistoryItems(html); 

Upvotes: 4

Views: 11851

Answers (4)

Simon Achmüller
Simon Achmüller

Reputation: 1176

Or just try System.Windows.Forms.WebBrowser, for example:

this.webBrowser1.Navigate("http://games.powernet.com.ru/login");
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
     System.Windows.Forms.Application.DoEvents();
HtmlDocument doc = webBrowser1.Document;
HtmlElement elem1 = doc.GetElementById("login");
elem1.Focus();
elem1.InnerText = "login";
HtmlElement elem2 = doc.GetElementById("pass");
elem2.Focus();
elem2.InnerText = "pass";

Upvotes: 1

Romano Zumbé
Romano Zumbé

Reputation: 8079

I would suggest to instantiate a WebBrowser control in code and do all your work with this instance but never show it on any form. I've done this several times and it works pretty good. The only flaw is that it makes use of the Internet Explorer ;-)

Upvotes: 1

Piotr Stapp
Piotr Stapp

Reputation: 19830

You could use for example Selenium in C#. There is a good tutorial: Data Driven Testing Using Selenium (webdriver) in C#.

Upvotes: 2

Gangadhar Heralgi
Gangadhar Heralgi

Reputation: 219

Try JMeter, it is a nice too for automating web requests, also quite popularly used for performance testing of web sites

Upvotes: 0

Related Questions