How to hide the browser interface while executing a php script? - c#

I have a php script which i would need the following 3 web browsers to execute it respectively without revealing the respective browser interface to the user.
The 3 web browsers are,
Internet Explorer, Google Chrome, Mozilla Firefox.
I found this C# code snippet but it is documented that it only works for Microsoft Products like Internet Explorer.
ProcessStartInfo PInfo;
Process Pro;
PInfo.CreateNoWindow = true;
PInfo.UseShellExecute = false;
PInfo.WindowStyle = ProcessWindowStyle.Hidden;
Pro = Process.Start(Execute the Php script);
Can anyone advice me how can I hide the browser interface for the other (Google Chrome, Mozilla Firefox) while executing the php script ? Please do give links or code snippets. (Preferably in C#).

PHP is server-side code. A browser is client-side only. This would be impossible without some intermediary code ran on the client's system. This would no doubt be classified as malware.
You can however, try to open up a popup and attempt to hide that popup --but this is unreliable since the client ultimately has the say on how their client behaves. Most browsers already block popups blockers in place for this reason.
Another suggestion might be to be to simply insert an within the webpage with style="visible: hidden" attribute set. This will execute the PHP you seek without the direct knowledge of your client. However, if your primary objective is to hide chrome/ff/ie, I don't think it can be done using white-hat methods.

Related

Complete Internet Explorer Authentication Dialog with Selenium

I am using Selenium to simulate a user to automate some legacy software. The software works only with IE6 (I'm using IE11 in compatibility mode) and is a bit crap.
There is a point in the software where the Windows Security dialog appears. This requires credentials before the user/simulator can proceed.
I'm using IAlert.SetAuthenticationCredentials to try and populate the dialog but this doesn't seem to work. To move on from this, I can enter the details manually, but then Selenium seems to thing the main browser window has been closed:
Currently focused window has been closed.
The WindowHandles collection at this point is empty, but the browser window is still open, and has rendered the correct page.
What's going on here?
UPDATE
The answers provided are suggestions on how to handle the dialog. I'm wondering why Selenium thinks the browser window is closed when in fact it is still there.
It is not possible to interract with native windows via selenium. The way to deal with your issue is for example to use analogue of Robot in Java. Since you are using C# there is a simulator here https://www.codeproject.com/Articles/28064/Global-Mouse-and-Keyboard-Library.
Example code would be like following:
// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);
// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);
There are also Mouse simulators, so with this framework it will be possible to enter the required values in window and accept it.
Try to switch to that alert by,
var alert = driver.SwitchTo().Alert();
alert.SetAuthenticationCredentials("Username", "Pwd");
alert.Accept();
I have tested it and it works for IE11, selenium v3.1.0
Ref: https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_IAlert_SetAuthenticationCredentials.htm
Suggesstion 1-Go to internet explorer settings->security settings-> user authentication-> select automatic login with current username and password.
Suggesstion 2- if your application has access to it's API, then login via API, get the authentication token and set the auth.token in browser cookie.

C# Running code on open webbrowser, downloading specific files

So this is a bit of a tricky problem I'm having and I'm starting to think that it's not easily possible.
There's a website that has a level of security that I am trying to automatically download files from(it's a banking site). The website will not save user info when you log in, and after logging in you have to verify a certificate in your browser to receive your reports. Once you've verified the browser with the certificate and are logged in, however, you are free to open up the reports(pdf files) by directly typing in the link to them.
For other sites, I would do something like:
using (WebClient client = new WebClient())
{
client.DownloadFile(PDFFileReportWebsite,#LocalLocationToStoreFile);
}
But for this site, I have no idea what to do(the above doesn't work). Is there a way to have code like above run from within the browser itself after I've logged in and verified my certificate?
One solution I thought of that would be overkill(nevermind just gross/hacky), and I can't imagine the best option, would be to make a webbrowser app in C# and run the code on that browser after logging in.
EDIT:
ProcessStartInfo pi = new ProcessStartInfo(WebAddressOfPdfFileINeed);
Process.Start(pi);
correctly opens up the webbrowser(chrome since its my default) to the file I need(If I'm already logged in and verified my cert). Is there a way to force chrome to save this file or print it as text or a pdf?

http authorization with webdriver and popup window

I'm trying to use Selenium WebDriver to automatically login in to a site with a user-name and password. I've done my research and I don't believe this feature is supported by WebDriver, so I need to find another way. The site I'm trying to automate logging into is located here.
When prompted to login a popup window comes up that doesn't seem to be part of the browser. I'm using Firefox and Chrome. It seems Windows API may be required? I already tried passing the credentials in the URL but that didn't work. Also tried sendkeys, but received a Windows exception that the application was not accepting Windows messages. I also tried switching the current handle using driver.windowhandles but the popup doesn't seem to be a new handle.
Does anybody have any ideas? I'm kinda stuck. The preliminary code to get to the popup window is:
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.portal.adp.com");
string currentWindow = driver.CurrentWindowHandle;
IWebElement userLogin = driver.FindElement(By.Id("employee"));
userLogin.Click();
The popup you are seeing is prompted by web server and is a authentication prompt. Selenium doesn't support this operation.
One of the way to handle this limitation is to pass user and password in the url like like below:
http://user:password#example.com
More info available here : http://aleetesting.blogspot.in/2011/10/selenium-webdriver-tips.html
I wanted my answer out there because I think I've solved it. This answer does not require passing the credentials through the URL (for those of you that are unable to like me). It also does not require any custom Firefox Profiles or extensions to be installed or included with the solution or installed onto the browser eliminating cross-machine compatibility issues.
The issue with me was that the authentication could not be completed via passing the credentials through the URL because the login was behind a proxy.
So, I turned to windows automation toolkits and found AutoIT. Using AutoIT and Selenium, you can login automatically by sending the username and password to the windows dialog that appears. Here's how (note the steps below are for c#:
1 - Download AutoIT from http://www.autoitscript.com/site/autoit/downloads/
2 - Add the autoit .dll to your project references.
Right click on references, select Add Reference. Next click the browse button and browse to the dll location (most default installations it will be c:\Program Files (x86)\AutoIt3\AutoItX\AutoItX3.dll), and add to project.
3 - use AutoIT and Selenium like this (assuming your web driver is already initialized):
//Initialize AutoIT
var AutoIT = new AutoItX3();
//Set Selenium page load timeout to 2 seconds so it doesn't wait forever
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(2));
//Ingore the error
try
{
Driver.Url = url;
}
catch
{
return;
}
//Wait for the authentication window to appear, then send username and password
AutoIT.WinWait("Authentication Required");
AutoIT.WinActivate("Authentication Required");
AutoIT.Send("username");
AutoIT.Send("{TAB}");
AutoIt.Send("password");
AutoIT.Send("{ENTER}");
//Return Selenium page timeout to infinity again
Driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(-1));
Anyway, that's it, and it works like a charm :)
Also note that there are some special characters that need to be escaped in AutoIT using the sequence "{x}". For example, if your password is "!tRocks", you'd need to pass it into AutoIT as "{!}tRocks".
Happy automating.
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("network.http.phishy-userpass-length", 255);
profile.SetPreference("network.automatic-ntlm-auth.trusted-uris", hostname);
Driver = new FirefoxDriver(profile);
hostname is your URL (example.com) then try to
Driver.Navigate().GoToUrl(http://user:password#example.com);
I just got done working on a prototype project that is supposed to handle exactly this kind of situation.
It utilizes BrowserMob, a popular open source proxy, to perform the authentication.
SeleniumBasicAuthWrapper Hope it helps! It is still a work in progress, but hopefully we'll get any kinks or defects ironed out in the near future.

Accessing the Ubuntu terminal from Mono

Background
I'm writing an web application so I can control an Ubuntu Server from a web site.
One idea I had was to run the 'screen' application from mono and redirect my input and output from there.
Running 'screen' from mono:
ProcessStartInfo info = new ProcessStartInfo("screen", "-m");
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
var p = new Process();
p.StartInfo = info;
p.Start();
var output = p.StandardOutput;
var input = p.StandardInput;
but running 'screen' with the RedirectStandardInput gives out the error:
Must be connected to a terminal
I've tried many different arguments and none seems to work with 'Redirecting Standard Input'
Other ideas for controlling a server will be greatly appreciated
I think this is the typical question in which you're asking how to implement your solution to a problem, instead of asking how to solve your problem. I don't think you should do hacky things like making a web app that tunnels the user actions to the server via a terminal.
I think you can bypass all that and, without writing a single line of code, take advantage of what the platform (Gtk+ in this case) already provides you:
You could run gnome-terminal in the server with the Broadway GDK backend. This way the gnome-terminal app will not run in the server, but open a web server on the port you specify. Later, you can use any WebSockets-enabled browser to control it.
This is the easiest and less hacky solution compared to the other ones offered so far. If you still are excited about using Mono for web development you still can, and you could embed this access in an iFrame or something.
(PS: If you don't want to depend on GTK being installed in the server; you could just use WebSockets in your client part of the webpage to be able to send events from the server to the client, and the library SSHNET to send the user's input directly through the wire.)
screen will need a terminal of some sort. It's also gigantically overkill.
You may wish to investigate the pty program from the Advanced Programming in the Unix Environment book (pty/ in the sources) to provide a pseudo-terminal that you can drive programmatically. (You'd probably run the pty program as-provided and write your driver in Mono if you're so inclined.) (The pty program will make far more sense if studied in conjunction with the book.)
The benefit to using the pty program, or functionality similar to it, is that you'd properly handle programs such as passwd that open("/dev/tty") to prompt the user for a password. If you simply redirect standard IO streams via pipe() and dup2() system calls, you won't have a controlling terminal for the programs that need one. (This is still a lot of useful programs but not enough to be a remote administration tool.)
There may be a Mono interface to the pty(7) system; if so, it may be more natural to use it than to use the C API, but the C API is what does the actual work, so it may be easier to just write directly in the native language.
A different approach to solve the same problem is shellinabox. Also interesting is this page from the anyterm website that compares different products that implement this kind of functionality.
Using shellinabox is very simple:
# ./shellinaboxd -s /:LOGIN
(this is the example given on their website) will start a webserver (on in your case the Ubuntu server). When you point your browser to http://yourserver:4200 you'll see a login screen, just like you would see when opening a session with ssh/putty/telnet/... but in your browser.
You could provide the required remote access functionality to the server's shell by just including an iframe that points to that service in your application's webpage.

Selenium 2 (webdriver): Taking a Screenshot returns a black image

I am using Selenium 2 (Webdriver), in an ASP.NET website to build a service, where users can enter their URL and gets screenshots of the page, made with different browsers.
My page is hostet on an Windows Server 2008 R2.
Taking Screenshots with FirefoxDriver works perfect.
But when I am using InternetExplorerDriver, I just get an empty black file.
The App is running as Administrator - so there should't be permission issues.
My Code:
// Opening the Browser
var ieCapabilities = DesiredCapabilities.InternetExplorer();
ieCapabilities.SetCapability(InternetExplorerDriver.IntroduceInstabilityByIgnoringProtectedModeSettings, true);
var browserIe = new InternetExplorerDriver(ieCapabilities);
browserIe.Navigate().GoToUrl("http://www.google.com");
// Screenshot
var dir = Server.MapPath("/screenshots/");
browserIe.GetScreenshot().SaveAsFile(dir + "Filename.png", ImageFormat.Png);
browserIe.Close();
Any ideas why my file is black?
THANKS!
There's probably nothing wrong with your code. Although, I'm using Java, so I can't tell for sure.
I had the same issue with IE while FF and Chrome worked fine.
This post suggests that starting the Selenium Server via a remote desktop connection could lead to problems.
Some other posts suggest that the screen saver might have something to do with it.
I just tried leaving the remote desktop connection open and it solved the black screenshot issue. Also logging in via VNC seems to work, leading me to the theory that Windows locks the screen after terminating the remote desktop connection while leaving it unlocked if using VNC.
This post suggests that disabling screenshots when the screen is locked is a Windows Security feature.
InternetExplorerDriver mydriver = new InternetExplorerDriver();
mydriver.Navigate().GoToUrl("http://www.google.com/");
Screenshot myScrennShot = ((ITakesScreenshot)iedriver).GetScreenshot();
myScrennShot.SaveAsFile(#"C:\Path\123.png", ImageFormat.Png);
//or
byte[] data = myScrennShot.AsByteArray;
It works for me, probably it does work for you too :-) If it doesn't work I suggest you to separate this code to different service (WindowsService) because in you case this issue maybe connected with application pool restrictions. Anyways, please let me know how is it going.

Categories