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
Related
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>";
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.
I have an asp.net form, which allow users to submit a registration form which internally sends/store all these values on SharePoint list using the web-service and hence on submit the page process time is a little lengthier then the normal form.
Mean time before the page gets redirect to a thanks page, user's tend to click the submit button again, which is causing the multipul submission of the same record.
Please suggest a way to restrict this submission, on button click I am using a jquery function for data validation, I have tried using the btn.disable = true method there, but once it reach's the else block (after passing the validation and this is where the disable = true code is) it doesn't submit's the form after reading btn.disable = true statement.
Experts, please show me a path.
Thanks in advance.
See Nathan Long's plugin: https://stackoverflow.com/a/4473801/1414562
{modified to allow re-submit form if an input change}
// jQuery plugin to prevent double submission of forms
jQuery.fn.preventDoubleSubmission = function() {
var $form = $(this);
$form.on('submit',function(e){ //on here instead of bind
if ($form.data('submitted') === true) {
// Previously submitted - don't submit again
e.preventDefault();
} else {
// Mark it so that the next submit can be ignored
$form.data('submitted', true);
}
}).find('input').on('change',function(){
$form.data('submitted',false);
});
// Keep chainability
return this;
};
Use it like this:
$('form').preventDoubleSubmission();
As you've found, if you disable the button in the onclick handler it won't send the request to the server. The simple "hack" to get around this is to, rather than disabling the button right away, use setTimeout to schedule a function to run in, say, 5ms that will disable the button; this will allow the request to be sent to the server before the button is disabled, while not leaving enough time for a person to actually click it twice.
Two ways
Either in Jquery or through C#
Jquery
$("#btnSubmit").live("click",(function(e) {
this.disabled = true;
/Do Something /
this.disabled = false;
return false;}));
C#
protected void btnSubmitClick(object sender, EventArgs e)
{
btnSubmit.Enabled = false;
/Do Something/
btnSubmit.Enabled = true;
}
Hope this helps
Many Thanks
Anna
I have seen umpteen posts talking about working with pop window using window handle.
But how to work with a browser which is opened when clicked on a button -
<button class="power_buy_now_button" type="button">
</button>
I tried to get window handle but each time I encounter a changing string, some thing like - "8c5f028e-e7cc-4d0f-afe4-983bb119391e"
There is not even title associated with new browser. More over I am not sure how I would use title to bring control to new browser. And then at some point I would have to bring control back to first browser.
Your best bet is to do something like the following:
// This code assumes you start with only one browser window in your test.
// If you have more than one browser window, your code will be more complex.
string originalHandle = driver.GetWindowHandle();
driver.FindElement(By.ClassName("power_buy_now_button")).Click();
// May need to wait for window handles collection to have a .Count of 2,
// as clicks are asynchronous.
string popupHandle = string.Empty;
ReadOnlyCollection<string> windowHandles = driver.GetWindowHandles();
foreach (string handle in windowHandles)
{
if (handle != originalHandle)
{
popupHandle = handle;
break;
}
}
driver.SwitchTo().Window(popupHandle);
// Do stuff in the popup window, eventually closing it with driver.Close()
driver.SwitchTo().Window(originalHandle);
I ended up in picking second handle and click on that. This approach works and I hope I would be able to get control back to main window also.
I'm using WatiN testing tool and i'm writing c#.net scripts. I've a scenario where i need to change the theme of my web page, so to do this i need to click on a image button which opens a ajax popup with the image and "Apply Theme" button which is below the image now i need to click on the button so how to do this please suggest some solution.
So first click your button that throws up the popup, and .WaitUntilExists() for the button inside the popup.
IE.Button("ShowPopup").click()
IE.Button("PopupButtonID").WaitUntilExists()
IE.Button("PopupButtonID").click()
This may not work in the case the button on the popup exists but is hidden from view. In that case you could try the .WaitUntil() and specify an attribute to look for.
IE.Button("ButtonID").WaitUntil("display","")
The Ajax pop-up itself shouldn't pose a problem if you handle the timing of the control loading asynchronously. If you are using the ajax control toolkit, you can solve it like this
int timeout = 20;
for (i=0; i < timeout; i++)
{
bool blocked = Convert.ToBoolean(ie.Eval("Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();"));
if (blocked)
{
System.Threading.Thread.Sleep(200);
}
else
{
break;
}
}
With the control visible you then should be able to access it normally.
Watin 1.1.4 added support for WaitUntil on controls as well, but I haven't used it personally.
// Wait until some textfield is enabled
textfield.WaitUntil("disable", false.ToSting, 10);
I'm not using any ajax control toolkit and in the popup there is no text field as i've mentioned there is only a image and a button below it, which i need to click in order to apply that image as theme.
I wrote a post about how I do ajax synchronization, since I was having problems with WaitUntilExists: http://lebobitz.wordpress.com/2011/03/06/synchronizing-watin-and-ajax-with-jquery/