how to fill in html textbox from (c# ) windows application - c#

I would like to do the following from a windows application:
Open a browser and navigate to a URL.
When i am at the correct URL in my browser window, i want to fill in data in the html textboxes on that page from my windows application.
When that is done i want to press a button.
Note:
Google Chrome and Mozilla Firefox is currently used in my browser Window.
Internet Explorer(IE) is not used in browser Window.
1.The URL (https://www.exple.com/login.html ) had already opened on anyone web browsers such as Mozilla Firefox and Google Chrome in local System.
It consists of Two Textboxes such as Username and Password .
2.I Want to fill in data in Username & Password Textbox on Google Chrome/Mozilla Firefox Web Browser.
3.The Input data is taken from C# Windows Application to URL When pressed Submit Button in Windows Application.
Thanks in Advance .

You can do it, but it will really hard,but if you make autoscript in testcomplete and then get code from it, code will be ugly , but working

That's what WatiN is for. It was primarily created for web application testing, but your problem can be solved with WatiN as well.
Example from the front page:
[Test]
public void SearchForWatiNOnGoogle()
{
using (var browser = new IE("http://www.google.com"))
{
browser.TextField(Find.ByName("q")).TypeText("WatiN");
browser.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(browser.ContainsText("WatiN"));
}
}
This example uses IE, but Firefox is supported as well.

Related

Unable to download file (page url same as download url)

I'm trying to download a zip file (that is normally accessed/downloaded by pressing a button on a web page) using C#.
Normally the file is downloaded by selecting "Data Export" and then clicking the "SEARCH" button at this URL:
http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=en-GB&fromdate=2016-05-30&tomdate=2016-06-03
If trigger the download manually on the webpage and then copy the download url from the 'Downloads' view of chrome or firefox I get the exact same URL as above. When I paste that in a browser window I will not trigger the download, instead the above page will be loaded and I have to trigger the download manually in the same way as in the first place.
I've also tried using the network tab of the inspector to copy the request header of the request that is triggered when clicking the "SEARCH" button, but that URL is also the same as the one above.
Trying with C# I get the same result, the page itself is downloaded. My code looks as follows:
using (var client = new WebClient())
{
client.DownloadFile("http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=sv-SE&fromdate=2016-05-30&tomdate=2016-06-03", "zipfile.zip");
}
My guess is that my code is correct, but how do I get the correct URL to be able to download the file directly?
ASP.net inserts a bunch of crap into the the page to make things like this particularly hard. (Validation tokens, form tokens, etc).
Your best bet is to use a python library called Mechanize, or if you want to stick to C# you can use Selenium or C# WebBrowser. This will fully automate visiting the page (you can render the C# WebBrowser invisible), then just click the button to trigger the download programatically.

URL redirect + VSTO

I am creating an Add-in for Outlook 2010. Once the button is clicked, the email and subject will be saved to database (this is fine) and will also redirect to a URL. Could you please tell me how to do the URL redirection for VSTO? Not sure if that is possible. Thanks!
You can use Process.Start() to open the URL in the user's default web browser as follows:
Process.Start("explorer.exe", #"http://www.google.com");
or more simply (as you found out):
Process.Start(#"http://www.google.com");
To be clear, you're not redirecting, just simply browsing to the URL in a new window.
If it's a outlook form region you use, you could use the Navigate2 of the SHDocVw.WebBrowser
I am not sure if you wanted to open a new url in the default web browser.

How to resize Webbrowser content to fit 480 x 800

Im using some basic code to display a mobile website in my application using a web browser.
For some reason, if I use the standard browser it is sized correcly to 480 x 800 , but when I use the web browser in my application the page is way off more like 960 x 800.
Is there a way to force the size the page is displayed in the web browser?
The funny thing is that it was working a few months ago, but has suddenly gone haywire.
code is :
string site1 = "http://m.domain.com";
webBrowser1.Navigate(new Uri(site1, UriKind.Absolute));
webBrowser1.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(webBrowser1_Navigated);
I was thinking I could force user agent by using the below code, but I am getting errors " no overload for method, 'Navigate' takes 4 arguments.
webBrowser1.Navigate("http://localhost/run.php", null, null, "User-Agent: Windows Phone 7");
Page using app:
Page using standard IE9 browser outside of application, but on handset.
One place to start would be to sniff the request/response. There's clearly different HTML coming back from the server for the two requests. For the in-app browser, it clearly says it's having trouble identifying the device. If you can figure out what is different between the two requests, you might be able to force the in-app browser to make the request more like the out-of-app browser.
If you're using the simulator, Fiddler is a fantastic tool for that sort of thing. The first place I'd look is at the User-Agent header, which most sites use to figure out what type of browser is requesting the page.

Logging in to a website with C#

I'm sorry if this subject has already been answered, but I couldn't find what I needed (yet).
I'm working on a program that downloads files from university websites that use the same infrastructure. It's an open source project which I'm trying to support in my free time
(hosted in goodle code: http://code.google.com/p/highlearner/)
Until now we used GET and POST requests to login into the right page and download stuff. But the universities keep changing their websites and every little change requires teaking in Highlearner, which requires a new version, auto-updating all users, etc. Also, every university has its own login page, requiring me to tailor a login sequences..
So I'm looking for a more robust solution. Instead of manually redirecting and setting the HTTP parameters. Is there some kind of mini browser that supports with HTML + Javascript? No GUI is needed, I just need the engine.
This way, I will simply need to fill out the form parameters and let the browser do the work.
Thanks,
Nitay
You could try to automate the process with WatiN library . It allows you to click buttons, submit forms, etc.
using (var ie = new IE(loginUrl))
{
if (ie.TextField("username").Exists
&& ie.TextField("password").Exists)
{
ie.TextField("username").Value = "username";
ie.TextField("password").Value = "password";
ie.Button(Find.ByName("submit")).Click();
}
}

Facebook C# SDK Authorization Problem

I have an iFrame Facebook application. I am using the Facebook C# SDK, Facebook and Facebook.Web libraries.
When a user first comes to my application I create a FacebookApp object on page_load(). If the app.Session is null I create a CavasAuthorizer(app) then call Authorize().
Two problems:
First, the redirect URL that is generated by calling Authorize causes Facebook to error with a bad "next" parameter. Says the "next" parameter is not owned by the application. It looks like this:
next=http://localhost:4002/facebookredirect.axd/mygame_dev/mygame.aspx
I can edit the code in CanvasURLBuilder to make the next look like this:
next=http://localhost:4002/mygame.aspx
At this point the URL works if I cut and paste into a browser however it brings me to my second issue.
When the code runs the user is presented with a mostly empty page with a mid-sized Facebook image and a link "go to Facebook". When the user clicks on the link it then takes the user to the correct authorization page for my application.
I have a feeling these are two possibly related issues but potentially separate.
Any help here would be greatly appreciated.
-Andy
For the first problem
Make sure the site Url in the app configuration page is set to http://apps.facebook.com/[your_app]/
For the second problem.
When you are not authorized you are redirected to the login url but you can't do it from your iframe since it will redirect the iframe and you will get a Facebook inside facebook.
You should use window.top.location = ... to redirect the parent window.
EDIT
Facebook C# SDK Already does this for you when using the Mvc part of the SDK. Since you are using webforms you should use this code that is the equivalent.
protected void Page_Load(object sender, EventArgs e)
{
var fb = new FacebookApp();
var auth = new CanvasAuthorizer(fb);
if (!auth.IsAuthorized())
{
var url = auth.GetLoginUrl(new HttpRequestWrapper(Request));
var content = CanvasUrlBuilder.GetCanvasRedirectHtml(url);
Response.ContentType = "text/html";
Response.Write(content);
Response.End();
return;
}
//...Go on if authorized
}
To test locally make sure your Site URL and Canvas URL in facebook is something like http://localhost:8181/yourpage. Then make sure in VS you set the setting under project properties\Web so that ports are not dynamically generated and you can use 8181 (or whatever port you like, as long as it is the same as in FB).
I think your Callback url in your web config is pointing to localhost. You can't do that since the code actually runs inside of Facebooks IFrame. Localhost is localhost to the facebook web server. You will have to give a valid url in the Callback key in your web.config. If you don't have a domain you can map to your ip then check out any of the free dynamic dns clients out there. No-ip is one exammple (and one I use personally).

Categories