WatiN need to declare browser outside of STAThreaded thread - c#

I'm currently trying to use WatiN to do some automatic data collection. I used to use a WebBrowser controll, and the way I did it was I declared a stathread and ran it from there, then using a Browser.DocumentCompleted I started a void LoginPageLoaded on which I would set the user and pass, and login. Thing is, with WatiN, I'm trying to do
var th = new Thread(() =>
{
Browser browser = new IE(url);
browser.WaitForComplete();
HolyThunder hl = new HolyThunder();
l.LoginPageLoaded();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
But obviously, when I try to use the browser instance on the LoginPageLoaded void it says it downs't know what it is, because it was declared inside the th thread. I didn't do it when I ran LoginPageLoaded through browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LoginPageLoaded);, and I obviously can't declare the browser outside of that STAThread. What do you think I can do to fix this?
EDIT - I failed so bad... if I do everything I need to do inside that STAthread, everything will work. My question now is, in watin, is there anyway to open a "webbrowser" without actually having the visual component of the webbrowser?

You can try using NHtmlUnit, it's a headless browser. I am not sure if you can do it with WatiN.

You can run WebKit headless (see, for example http://phantomjs.org/), but I don't know if you can drive it from WatiN.

I guess you want to hide web browser window.
In that case try this:
Settings.Instance.MakeNewIeInstanceVisible = false;

To open a "webbrowser" without actually having the visual component of the webbrowser, use
var browser = new IE();
browser.Visible = false;

Related

WatiN doesn't find anything

I'm new to C# and I'm trying to do an application that automatize Internet Explorer.
When I click a button, the application does :
using ( var Browser = new IE())
{
Browser.GoTo("http://testweb.com");
Browser.TextField(Find.ByName("username")).TypeText("User");
Browser.TextField(Find.ByName("password")).TypeText("Pass");
}
But it doesn't write anything. It navigates to the web but...
Try this:
IE ie = null;
ie = new IE();
ie.GoTo("Link");
ie.WaitForComplete();
At least to get started.
For the other bit, you need to get an exact identification and then you can tell WaTiN to interact with it.
Textfield userTextBox = ie.Textfield(Find.ByName("name"));
userTextBox.TypeText("user");
This may seem banal but now you can add a peek definition in your code and see if "userTextBox" gets found by name. If it doesn't you need to find it through another method (ID or class).

Selenium doesn't recognize new opened window -Selenium C#

Seems like I'm facing some sync issues in my code.
during my process, I'm clicking a button which opens a new window.
I'm swicthing to the new window by the following code.
_webdriver.SwitchTo().Window(_webdriver.WindowHandles.Last();
Then, I'm inserting data into fields within the next window.
problem is that sometimes the objects in the "next window" are not being found.
I'm getting : "can't find element" error.
for me it seems like a sync problem , meaning , DOM issues.
so I have tried using :
_webdriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
and I even tried :
Thread.Sleep(3000);
Unfortunately , seems like most of the times the problem is that selenium didn't switch to the new window(could see it when debugging).
I'll be happy to have your assistance.
You could wait for two windows and then set the context to the new one:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
// wait for 2 windows
ReadOnlyCollection<String> handles = null;
wait.Until((d) => (handles = driver.WindowHandles).Count > 1);
// set the context on the new window
driver.SwitchTo().Window(handles[handles.IndexOf(driver.CurrentWindowHandle) ^ 1]);
i am not sure how this is done in C# but i think selenium is same you just have to use C# syntax for loop.
//Switch to newly opened window (JAVA)
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
this is how i do in Java. you will get driver.getWindowHandles() same method in C# as well, if i am not wrong as this is selenium method.
hope this is helpful to you.

watin attach to manually started window keeps failing

IList<string> values = new List<string>();
var instance = Find.By("hwnd", "110CC");
...
if(instance != null)
{
var ie = Browser.AttachTo<IE>(instance);
The browser instance is manually started by the tester in case this makes any difference.
This just doesn't work for me I keep getting an exception from watin saying that it can't find a window with that handle.
I got the handle with Spy++.
I tried searching by window title or window url also but it also didn't work.
Is there any way to do this?
Thank you
The below works as expected / no errors. WatiN 2.1, IE9, Win7
Before running the code, open an IE browser and point it at cnn.com
IE browser = Browser.AttachTo<IE>(Find.ByUrl("www.cnn.com"));
browser.TextField("hdr-search-box").TypeText("searchy");

Open InternetExplorer From C# with new context

var ie = new InternetExplorer { Visible = true };
ie.Navigate("http://google.com");
ie.DocumentComplete += IeDocumentComplete;
But when the document is opened, it's like a Ctrl+N in Internet Explorer.
I don't want to keep context and session. I would like a new instance of Internet explorer.
Can you help me please ?
Many Thanks
I think you need to capture the key board event and pass it to the InternetExpolorer control, if I am not wrong because the key interrupts are sent to your application (and not to your IE Control) it does not work.
http://msdn.microsoft.com/en-us/library/aa768335(VS.85).aspx
Hope that helps!!!
Based on your example code, I'm guessing your using the InternetExplorer class from ShDocVw.dll?
Instead of using that, try to open Internet Explorer by launching it as a new process...
System.Diagnostics.Process.Start("iexplore.exe http://www.google.com");
That may do it, but it may also be down to specific user settings in the browser itself.

Problem hiding Internet Explorer in WatiN, even when using Settings.Instance.MakeNewIeInstanceVisible = false

This question is more of a follow-up to this one:
Hiding Internet Explorer when WatiN is run
Like the person who asked that original question, I also want to stop IE from being shown when my WatiN tests are running, but even when using this setting in a seemingly correct manner (code snippet below), it still ends up showing an empty IE window initially (although it does not show the test behavior/web page interaction).
Is it possible to stop the window from showing at all, or is this as good as it gets?
My helper method to create a new IE instance:
public static IE CreateNewBrowserInstance(string url = DefaultAppUrl)
{
Settings.Instance.MakeNewIeInstanceVisible = false;
Settings.Instance.AutoMoveMousePointerToTopLeft = false;
Settings.Instance.AutoStartDialogWatcher = false;
return new IE(url, true);
}
You can Hide window after initializing new IE instance
browser.ShowWindow(NativeMethods.WindowShowStyle.Hide);

Categories