SendMessage user32dll on an internet explorer page - c#

I have an Internet Explorer page opend on my desktop. The name of the webpage is TEST. With FindWindow() from user32.dll i can get a handler over the window. In this page I have a button called Go and I 2 textboxes called Name and Surname. How can I write in thewebpage my name and surname and than click Go programatically? THX

The normal approach to updating foreign windows (WM_SETTEXT et al) won't work because the form components within IE are not stock windows, rather they are rendered by IE itself.
To manipulate them you need to call via the DOM (or use something like WaitN).
using mshtml; //.net ref microsoft.mshtml
using SHDocVw; //com ref `microsoft internet controls` + change ref to no embed interop
int HWND = 0x001C0C10; //your IE
foreach(InternetExplorer ie in new ShellWindowsClass()) {
//find the instance
if (ie.HWND == HWND) {
//get doc
HTMLDocument doc = (HTMLDocument)ie.Document;
doc.getElementsByName("name").item(0).value = "bob";
doc.getElementsByName("surname").item(0).value = "smith";
doc.getElementsByName("go").item(0).click();
}
}

I am not sure what your asking is actually possible with Using Win32 (user32.dll), having said that
If your trying to interact with the web server then you could simply use httprequest and httpresponse class to GET and POST variables as required.
HttpWebRequest testRequest = (HttpWebRequest)WebRequest.Create("http://.....");
HttpWebResponse testResponse = (HttpWebResponse)testRequest.GetResponse();
string responseStatus = testResponse.StatusCode.ToString();
testResponse.Close();
http://msdn.microsoft.com/en-us/library/system.web.httprequest.aspx
http://msdn.microsoft.com/en-us/library/system.web.httpresponse.aspx
If your tring to test or reply a standard set of event then you may want to have a look at Fiddler
http://www.fiddler2.com/fiddler2/

Related

TestStack/White API

I'm trying to automate an application using TestStack/White API (Which is based on Microsoft's UI Automation library).
The problem is the following:
At a certain point of automation, I have to deal with an "Dialog" window, which looks to be a separate process, if i look at "Windows Task Manager". But no matter how i try to access the "Dialog Window" (Class, ID, Text, ControlType, etc.) I'm not able to access it.
You can find the UISpy image and code below...
Using UISpy - Dialog Information
using (var DISCLAIMER_App = Application.Attach(#"PathToExecutable"))
using (var DISCLAIMER_Window = DISCLAIMER_App.GetWindow(SearchCriteria.ByClassName("#32770"), InitializeOption.NoCache))
{
var IAccept_button = DISCLAIMER_Window.Get<Button>(SearchCriteria.ByText("I accept"));
IAccept_button.Click();
}
# I've tried also Application.Launch, Application.AttachOrLaunch.
# I also looked to be sure that the Dialog window is a separated process and doesn't belong to any parent window.
Any suggestions?
Found the Solution, had to use "ProcessStartInfo()" and pass the return data to "Application.AttachOrLaunch()":
var psi = new ProcessStartInfo(#"PathToExecutable");
using (var DISCLAIMER_App = Application.AttachOrLaunch(psi))
Source: http://techqa.info/programming/tag/white?after=24806697

Filling out a web form programatically in c# [duplicate]

I'm working on a Device that runs on windows CE and i need to automate a login process. I was able to achieve this in a Forms Application using the code below but it doesn't seem like I can use the same process on the smart device. Is there a way to do the same thing while working in CE?
string butts = webBrowser1.Url.AbsoluteUri;
HtmlDocument doc = webBrowser1.Document;
HtmlElement userValue = doc.GetElementById("username");
userValue.SetAttribute("value", "user");
HtmlElement passValue = doc.GetElementById("password");
passValue.SetAttribute("value", "pass");
HtmlElement subButton = doc.GetElementById("submit");
subButton.InvokeMember("click");
The HtmlDocument class, and really all of the System.Windows.Forms.HtmlXxx objects do not exist in the Compact Framework.
If you have a very small set of things you want to access, you might be able to roll your own implementation. You might be able to borrow some from the Mono code base as well. Otherwise, there really aren't any good answers.

How to pass data from windows application to web application?

I have a windows application and i want to open my web application from this windows application. My Windows application will generate a key and machine code after authorization and will save the key and machine code in to database among active users. Now i want to send this key to browser so that my web application can identify the user with his machine.
How can i do this?
i cannot use URL because the user will be able to copy the URL and use my web application from another machine. I must restrict it.
Is there any other way?
There are Two Ways to transfer winform data to web applications
If you want to transfer the data to IE then You can Use
1)MSHtml.dll
code
InternetExplorer TargetIE = null;
IHTMLDocument2 document = null;
//Check whether the IE is opened
foreach (InternetExplorer internetExplorer in new ShellWindows())
{
if (internetExplorer.Document is HTMLDocument)
{
TargetIE = internetExplorer;
break;
}
}
2) If you want to transfer data from winform to any web browser my personal advice to you please use selenium for this.
download the respective dll and driver for respective drivers from this site help
Code
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Chrome;
namespace WindowsFormsChrome
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// download the chrome driver
IWebDriver driver = new ChromeDriver(#"C:\Users\Downloads\chromedriver");
driver.Navigate().GoToUrl("http://www.yahoo.com");
IWebElement myField = driver.FindElement(By.Id("txtUserName"));
myField.SendKeys("UserName");
IWebElement myField = driver.FindElement(By.Id("txtPassword"));
myField.SendKeys("Password");
IWebElement myField = driver.FindElement(By.Id("btnLogin"));
myField.click()
}
}
}
this second part work for all browser yoou just replace chromeDriver class as per you want.
you can POST data using c#
http://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx
see also this post in stackoverflow
How to post data to a website
You can write an ashx handler and pass your data (or some reference to your data) from your windows application. Here is an example how this can be done :
how to call ASHX handler and getting the result back

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:
Visit 'www.[test-website].com'
Fill in these DOM items
DOM item 'username' fill in with 'testuser'
DOM item 'password' fill in with 'testpass'
Click' button DOM item 'btnSubmit'
Visit account history
Read HTML (So we can parse information about each distinct history item)
...
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);
You could use for example Selenium in C#. There is a good tutorial: Data Driven Testing Using Selenium (webdriver) in C#.
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 ;-)
Try JMeter, it is a nice too for automating web requests, also quite popularly used for performance testing of web sites
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";

BHO Plugin (Microsoft JScript runtime error)

I have built a BHO (Browser Helper Object) in C# which detects phone numbers in web pages and places an image with a hyperlink next to it. The BHO basically inserts a javascript which uses a Regex String to find phone numbers and adds the image next to it.
This is the relevant code
public void OnDocumentComplete(object pDisp, ref object URL)
{
HTMLDocument document = (HTMLDocument)webBrowser.Document;
IHTMLElement head = (IHTMLElement)((IHTMLElementCollection)
document.all.tags("head")).item(null, 0);
IHTMLScriptElement scriptObject =
(IHTMLScriptElement)document.createElement("script");
scriptObject.src = "\nhttp://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js";
((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject);
IHTMLScriptElement scriptObject2 =
(IHTMLScriptElement)document.createElement("script");
scriptObject2.text = "\nwindow.onload = function()"+
"{"+
"$('body').html( $('body').html().replace(/(\\d\\d\\d\\d\\s\\d\\d\\d\\s\\d\\d\\d)/g,'$1 <img src=\"image.png\" border=\"0\">') );"
+"}"+
"\n\n";
((HTMLHeadElement)head).appendChild((IHTMLDOMNode)scriptObject2);
}
I have tested the BHO in IE on a very simple page with few phone numbers. It works as expected. But when i test the BHO with any other page on the web i get the following error
Microsoft JScript runtime error: Object doesn't support this property or method
or
Microsoft JScript runtime error: Permission denied
I am getting Microsoft JScript runtime error for a few other pages. Does this mean that i am not allowed to insert a javascript into the page ? What could be the reason ? I hope this is the right way to do it.
I think the errors are thrown because you add jQuery multiple times to the site you're loading. Have you ever tried to set a breakpoint inside the OnDocumentComplete method?
If you do so, you will see, that the OnDocumentComplete event is fired more than one time, per site request.
So you should check at first, if it's the first time the OnDocumentComplete event is fired for the actual site request. If so, add you javascript, if not, do nothing.
This should prevent double jQuery includes.

Categories

Resources