WatiN - Set the position of a browser - c#

Im testing my website using WatiN , and for a specific test I'm opening few IE windows and work on them simultaneously.The problem is that the windows are opened in the same position and i can't see what is going on inside the windows. Any ideas?
I'm looking for something like:
var browser=new IE();
browser.SetPosition(top_left_X,top_left_Y);

Swapping between windows
To bring one of the browsers to the front use the browser BringToFront() method.
Like:
IE ie = new IE();
ie.GoTo("www.cnn.com");
IE secondIE = new IE();
secondIE.GoTo("www.google.com");
secondIE.BringToFront();
Resizing windows
To resize the windows and make them both viewable, say side by side, I believe you'll need to use something like SetWindowPos() or PInvoke's movewindow(). You can use ie.hWnd to get the window pointer to the browser. Unfortunately it has been a really long time since I've done anything like that and can't put together an example now, or even guarantee it is the right course of action.

Related

Selenium clicking alternative

I am trying to use Selenium to automate the use of a web service.
Whenever I try to click on elements within the web page using Selenium the result is strange, it seems as if the web page is able to detect whenever I click using Selenium.
Whenever I click using my cursor the web page functions normally even if I set a break point mid application and start clicking it works fine.
Is there any alternative in .NET or Python that I could use to automate clicks in a similar fashion or perhaps avoid detection of these clicks on the service?
action.MoveToElement(element, 158, 208).Click().Build().Perform();
Thread.Sleep(5000);
for (int i = 0; i < 5; i++)
{
action.MoveToElement(element, 258, 158).Click().Build().Perform();
Thread.Sleep(2000);
action.MoveToElement(element, 228, 158).Click().Build().Perform();
//var screenshot = new Screenshot();
//screenshot.CaptureScreen(Convert.ToString(i + 1) + ".png");
Thread.Sleep(2000);
}
The service is an image service, whenever I click done to confirm that image has been processed, it will keep the image visible rather than making it disappear but when I interrupt and click it myself, it will disappear. The reason this is bad is the visible image will overlay the new image, the selenium clicked image will stay visible and manual clicks will not.
I am using the latest version of ChromeDriver and Selenium however I have tried Mozilla & IE with and other versions of selenium and web drivers with no avail.
The idea of putting CAPTCHA on a site is exactly to tell machines and people apart, so
seems as if the web page is able to detect whenever I click using Selenium
is working as designed. For simple captcha you can call some free ocr.space online OCR service. This will work just fine as long is an image with a text. You can also try Google Cloud Vision OCR which works equally well, but it is a bit more complicated to set up.

How to open two separate (default) browser windows instead of new tabs

I'm trying to use C# to open two separate browser windows side by side. I've tried using Process.Start(url) but that causes Chrome to open new tabs instead of new windows. This seems to work on IE, however I'd like to have code that can work with different types of browsers, namely: IE, Chrome, Firefox, and Safari. How do I detect the default browser and then open two separate windows side-by-side? Additionally, I want to be able to position the two windows next to each other, is that possible?
If you want to open new window in chrome instead of new tab, this code worked for me
Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = <yoururl> + " --new-window";
process.Start();
This is more about the way that the browser is configured than how the process is called from C#. In both cases, the system simply calls the default program assigned to handle the URL. There may or may not be arguments to that command, but typically it will simply invoke chrome.exe <url> and from there, the chrome.exe process decides how to handle the parameter.
The only method I am aware of would be to examine the registry (under HKEY_CLASSES_ROOT\http\shell\open\command) and parse the string value. Once you know the specific browser, you may be able to control the presentation using command-line arguments. Of course, this is specific to Windows and may be a pain to manage.
If the browser does not support setting a geometry from the command line, you will need to use FindWindow and SetWindowPos (using P/Invoke) to manipulate the window locations.
I am not sure about your application, but would embedding a WebBrowser Control meet your needs? Then you would have total control of the presentation.

access the newwindow inside the newwindow event

I use a webbrowser control in my application to get data from a specific web page. This web page won't work with the older IE because it specifically checks for the IE version. So I made a registry change that allows my application to work as IE 9 and everything is ok most of the time...
The problem is when a newwindow has to be opened. It won't display anything. I guess that the newwindow is acknowledged as IE 7 and I don't know how to make it disguise itself as IE9.
I also tried the other way round. I thought that if I intercepted the newwindow url then I could just send it to IE9 or open it in another instance of a webbrowser control. But the newwindow event only allows to cancel the event. You can't get any useful information out of it.
I believe that interop services is what I need but I know nothing about them.
So I've got two questions:
(1) Can I make the newwindow identify as IE9? (and how...)
(2) How can I get the newwindow url using interop services (or anything, I wouldn't care)?
It is quite strange (from my perspective) that the WebBrowser control doesn't surface the much-more-useful NewWindow3 event.
This CodeProject article describes a remarkably simple way to make it available.
In the NewWindow event, assuming your first Wb control is named WB1 and the one you want to redirect to is WB2, do the following in your WB1 NW event.
Processed = True ' This cancels the current request.
WB2.Navigate URL ' This redirects it to the second WB2 control.
Otherwise, if you want to use the NW2 (NewWindow2) event instead of the NewWindow (NW) event, do this in the NW2 event of the WB1 control.
Set ppDisp = WB2.object ' Just swaps the objects around to redirect, don't need to issue a cancel.
Also, you can do this via BeforeNavigate2 (of WB1). But slightly different code.
Cancel = True ' Cancel Request.
WB2.Navigate2 URL ' Reissue it to WB2.
Now, as long as you control where it redirects to, you can get the new window URL easily, using WB2.LocationURL or Wb2.Document.URL if i am not mistaken.
Also, if you want to change the rendering engine to IE9 (even if IE9 is installed on your computer, WB control will use IE7 rendering engine for compatibility)... there are articles online and answers on SO (including some of my previous answers) which clarify how you can alter the registry to ensure the rendering engine used by the WB control is the same as that of the installed version (IE9), otherwise, it will always use IE7. And, if you have IE4, 5 or 6 installed on a machine, it will always use IE4 for the rendering engine. I think they update teh rendering version after ever 3-4 version changes. I'm assuming during version 10, WB control rendering version will be version 10 as well.
Let me know if you need more assistance with it and i've love to know how you got along and if this helped answer your question. All my examples are in VB6, but you can transform them easily.
Cheers.

Run firefox in background from C#

I run Firefox (default browser) from C# with the code:
System.Diagnostics.Process.Start(browser.Document.Url.ToString());
I want Firefox to run in the background, because every time is open a new tab, the Windows is focusing on the Firefox, and is annoying.
How can I control Firefox tabs, close them after a time ?
You can use a ProcessStartInfo to tell it to run hidden or minimized or whatever. Not sure how to programmatically manipulate FireFox but I'm sure there's an API.
var psi = new System.Diagnostics.ProcessStartInfo();
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.FileName = browser.Document.Url.ToString();
var proc = System.Diagnostics.Process.Start(psi);
//after a while...
proc.Kill();
Technically, you are not starting Firefox, you are executing a url.
I'm not sure exactly what Windows does, but in effect, that url is opened in the system's default browser, be it IE, FF or some other thing that might not even support tabs, so finding and killing Firefox is not really a solution if the url is opened in Opera.
Moreover, the Process.Start method returns null if no process is actually started by the call, so if Firefox is already running and just displays an additional tab, you will get a null as the result of the call.
So, I'm pretty sure this is impossible to do in a broad sence (any browser), and, unless Firefox has some sort of API for client-side management, not possible for that scenario either.
BTW, on my system (IE is the default browser), the WindowStyle property is not working as expected, as IE pops up to the front.
Rather than trimming the tabs, why not just kill the entire Firefox process and restart it periodically?
You won't be able to do this. First of all, I'm pretty sure running Firefox in the background won't stop it gaining focus when a new tab is opened. Second, it is difficult to control firefox programmatically. The only way to do what you want is to use a plugin like MozRepl. You could also try using selenium or your own JavaScript to control the browser behaviour. I needed to be able to open and close tabs in a shell script without using selenium or MozRepl, check out my question From a shell script open a new tab in a specific instance of Firefox

Is it possible to screenshot a minimized application

I know it's possible to capture a screen of an application that is behind another application, but I can't seem to find anything on capturing the screen of a minimized application.
Anyone happen to know if this is possible? I don't want to get into things like maximizing and minimizing the application really quick.
As #ziplin said with newer version of windows it may be possible (via DWM apis). From c# you can use the Windows API codepack to access the new apis
I don't believe so, simply because i've noticed programs that try to preview a window that is minimized seem to draw a blank on occasion if the window is minimized. Although, some new versions of windows (vista and 7, I believe) do this very thing, but I'm not sure how easy it is to replicate (at all)
: http://www.stardock.com/products/objectdock/
You can't without restoring the window. When a window is minimized, the application is not in a state to render a UI for your application to capture.
If anyone still wants to no a workable solution I tried this one and worked well for me. It does a trick for minimized windows (because windows does not refresh them, on the taskbar the iconic picture shows the last image of the window before it was minimized). And Ziplin above mentioned that Objectdock can capture minimized windows, actually it does the same as Windows, it uses the last image captured before minimizing.
So here is the link: http://www.codeproject.com/Articles/20651/Capturing-Minimized-Window-A-Kid-s-Trick
I've never tried it. This code claims looks reasonable, but has caveats about minimized windows. It is, if nothing else, a place to start.
As a last resort: why not capture window just before it gets minimized?
I know its too late, maybe it might help others.
I thought of a different approach, instead of minimizing, I moved my form outside my screen co-ordinates before capturing the screen and later respawned it.
Below is my code example:
//Move the form beyond the screen
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.Bounds.Width + 10, Screen.PrimaryScreen.Bounds.Height + 10);
// Screenshot code here...
//Respawn the form to origin location
this.Location = currentPosition;

Categories