Selenium Chrome C# doesn't work if browser is minimized - c#

I integrated Selenium Chrome driver with my little .NET application. I'm doing automation on a google page. All is working fine and as expected as long as the browser is visible or in the background. If I minimize it it stops before doing any work. I have a lot of code with lots of DISPLAYED tests:
var collection = cdriver.FindElements(By.TagName(#"input"),10);
//var collection = cdriver.FindElementsByClassName("gwt-TextBox");
bool found = false;
IWebElement texter = null;
do
{
foreach (IWebElement element in collection)
{
if(element.GetAttribute("class").ToString() == "gwt-TextBox" )
{
if(element.Displayed==true) { found = true; texter = element; }
}
}
} while(!found);
texter.SendKeys(readerCSV.apkTitle);
Is there a way for me to minimize the chrome window so all the code that is working fine when the window is maximized or visible will also work fine with it minimized ? Enabled tests don't work as expected.
Another example:
var btnUpload = cdriver.FindElement(By.Id("gwt-uid-170"),10);
btnUpload.Click();
The above btnUpload.click() code gives me QpenQA.Selenium.ElementNotVisibleException
Is there a startup option for the chrome driver so I can start the window without the minimize button?

Chrome has to be pulled up because the driver is checking to make sure that the element is visible/enabled/etc to the user. Just stop minimizing it and let it run in the background and it will work.

Related

Prevent browser window from coming to foreground

I use an extension method to invoke interactions in a new browser tab:
public static void DoInNewTab(this ChromeDriver driver, Action<ChromeDriver> action)
{
var currentHandle = driver.CurrentWindowHandle;
// DOES NOT WORK:
//driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.ExecuteScript("window.open()"); // HERE
Thread.Sleep(2000);
driver.SwitchTo().Window(driver.WindowHandles.Last());
action.Invoke(driver);
driver.Close(); // HERE
driver.SwitchTo().Window(currentHandle);
}
Works like a charm, except that the browser window keeps getting activated and coming to the foreground and getting focus. It is a bit clumsy to debug, as breakpoints obviously activate the IDE window, but as far as I can see, the browser window gets activated on the lines marked with HERE.
How can I prevent the window from being activated and make sure it stays in the background?

Hiding browser popups - Watin

I'm using Watin library in a windows forms app. In order to hide the browser I use this instruction :
Settings.Instance.MakeNewIeInstanceVisible = false;
However, it doesn't hide the popups (when simulating a click on an element that opens a popup).
Is there a way to hide them?
You can do it programmatically by running some javascript code and make window.open function to do nothing!
Example
Here is a test page I made that has a very simple form and when the user clicks the Sum button, it sums up numA + numB and it displays the result inside a <span id="result"></span> element. After the result text update, it opens a popup window by calling window.open. In order to make this popup window disappear, we need to eliminate the window.open function:
window['open'] = function() { return false; }
To do that using Watin, we have to use the Eval function and inject the javascript code like this:
browser.Eval("window['open'] = function() { return false; }");
Then all popups are gone for that page load only and we have the wanted result.
Sample C# code
private void buttonPopupdEnabled_Click(object sender, EventArgs e)
{
WatiN.Core.Settings.Instance.MakeNewIeInstanceVisible = false;
IE ie = new IE();
ie.GoTo("http://zikro.gr/dbg/html/watin-disable-popups/");
ie.Eval("window['open'] = function() { return false; }");
ie.TextField(Find.ById("numA")).TypeText("15");
ie.TextField(Find.ById("numB")).TypeText("21");
ie.Button(Find.ById("sum")).Click();
string result = ie.Span(Find.ById("result")).Text;
ie.Close();
labelResult.Text = String.Format("The result is {0}", result);
}
Program running before javascript injection (Popup shows up)
Program running after javascript injection (Popup is gone)
I've checked released notes and found this:
By default WatiN tests make the created Internet Explorer instances
visible to the user. You can run your test invisible by changing the
following setting. Be aware that HTMLDialogs and any popup windows
will be shown even if you set this setting to false (this is default
behavior of Internet Explorer which currently can't be suppressed).
IE.Settings.MakeNewIeInstanceVisible = false; // default is true
Since WatIN haven't updated since 2011, I think you wouldn't expect any new feature support what you want.
I don't know if this could be a workaround but If those popups are not important to you why just don't block all popups?
How to turn off popup blocker through code in Watin?
HKEY_LOCAL_MACHINE\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_WEBOC_POPUPMANAGEMENT
Value = 0 for Off
Value = 1 for On

c# winform above chrome in kiosk mode

I'm having trouble getting a winform to appear above chrome in kiosk mode.
My goal is to have a transparent window display certain system information on top of all other windows. I have set this.TopMost = true and have even added the script:
private void keepWindowOnTop()
{
while (true)
{
if (!this.TopMost || !this.TopLevel)
{
this.TopMost = true;
this.TopLevel = true;
}
Thread.Sleep(1);
}
}
Still, when my winform launches chrome kiosk is on top.
Any suggestions would be greatly appreciated!
Edit: I need the transparent window to always be in the foreground even when other windows are selected (clicked). This feature works fine when running from Visual Studios, but after the application is installed on a separate system it breaks.
Could it be because I'm importing the .dll at run time? Does that have any effect on TopLevel?
cheers

Does the .NET WebBrowser control support console.log? [duplicate]

So I'm writing a Javascript coding UI using C# Windows Forms. This is my code for when the "Run" button is pressed, in case it helps:
//If the button in the upper-right corner is clicked
private void run_Click(object sender, EventArgs e)
{
//If the program isn't running
if (!running)
{
//Show the web browser
webBrowser1.Visible = true;
richTextBox1.Visible = false;
//Set the label to Running
status.Text = "Running";
//Set the html text to this below
webBrowser1.DocumentText = "<!DOCTYPE html><html><body><script>\n" + richTextBox1.Text + "\n</script></body></html>";
//Set the "run" button to "stop"
run.Text = "Stop";
//Set the status to running
running = true;
}
//otherwise
else
{
//Show the text box
webBrowser1.Visible = false;
richTextBox1.Visible = true;
//Set the label to Ready
status.Text = "Ready";
//Go to nothingness
webBrowser1.Navigate("about:blank");
//Set the "stop" button to "run"
run.Text = "Run";
//Set the status to not running
running = false;
}
}
I run the program, and for the most part, everything works fine. However, when I try to use the console.log() command, the following error appears:
'console' is undefined
I also try Console.Log (I actually don't know Javascript; just trying my best) but that returns the same error, that 'Console' is undefined.
Also, once I get console.log working, how do I open the console on the WebBrowser control? I've tried searching the internet, but nothing has come up on either of these questions.
You can get JavaScript console output from within Visual Studio.
By default the webBrowser1 control uses IE7 to render it's output. IE7 does not have a console.log() function. In order to get the console.log() function to work, you need to add the following meta tag:
<meta http-equiv="X-UA-Compatible" content="IE=11">
'IE=8' or greater should make the console.log() available to you.
When you debug a Windows Forms application it debugs using the .NET Managed Code debugger. In order to debug differently, instead of pressing 'Play' to debug, try selecting "Debug" > "Start without Debugging". Now once your application is running, go to "Debug" > "Attach to Process" and find your WindowsFormsApplication.exe, attach to it using the Script Code Debugger instead of the .NET Managed Code debugger.
Now, in Visual Studio:
You can open "Debug" > "Windows" > "JavaScript Console"
You can also open "Debug" > "Windows" > "DOM Explorer"
Yes, Console class is not available in your browser control, but you can create a logger class like this
[ComVisible(true)]
public class Logger
{
public void log(string s)
{
Console.WriteLine(s);
}
}
and use it in your browser control
webBrowser1.ObjectForScripting = new Logger();
webBrowser1.DocumentText = "<script>external.log('TEST');</script>";

How to open multiple IE tabs in C# when no IE window is opened?

I need to open multiple IE tabs in C# (windows application). Below is my code:
string[] pcList = txtInput.Text.Trim().Split(',');
foreach (string pc in pcList)
{
if (pc.Trim() != "")
{
System.Diagnostics.Process.Start("http://myCom/Lookup?type=ProductCode&name=" + pc.Trim());
}
}
If the default browser is firefox, there is no problem.
If the default browser is IE, and one IE window was opened, there is no problem either.
Multiple tabs will be opened according to the input in txtInput.
The problem I'm having is: if the default browser is IE and no IE window was opened, only one IE window and one tab will be opened. I do not know why is that and how to fix it. Can anyone help?
Thanks!
This is what I did to get around the same problem.
Process internetBrowserProcess = new Process();
ProcessStartInfo psiOjbect = new ProcessStartInfo("http://DefaultWebsiteOfmyCompany.com"); // You can also use "about:blank".
internetBrowserProcess.StartInfo = psiOjbect;
internetBrowserProcess.Start();
Thread.Sleep(2000); //Need to wait a little till the slow IE browser opens up.
foreach (string websiteUrl in Properties.Settings.Default.WebSiteURLs)
{
Process.Start(websiteUrl );
}
You can call a Process.Start("url") will open browser (if it is not running) otherwise Open a new Tab(if it supports it)
Similar SO question : Open new tab in IE

Categories

Resources