Selenium is popular framework that is being used with automation testing of webpages. But there is a problem that is being faced by the testing teams is it doesn’t support the handling of Popup or alert() boxes being called on onload() or onbeforeunload() event of browser. There’s workarounds exists like AutoIT etc. Special case seen for this is when we have Confirmation applied before navigating away from a page and selenium would fail to handle this and Automation will fail. One of my colleague was facing the same problem and he don’t want to use AutoIT. So I came with my own solution that can be used as fix and work well in handling the popups/alerts.
Environment: NUnit, Selenium, C#
The best way to deal with such alert box or popup is to use vbscript. It can identify the running window instance by its title and will send the “Enter” key to send ok.
The complete code for vbs will be:
Set oShell = CreateObject("WScript.Shell")
Set args = WScript.Arguments
arg1 = args.Item(0)
If oShell.AppActivate(arg1) ThenWscript.Sleep 500
oShell.SendKeys "{ENTER}"
End If
{
//Simulate autoclick on IE windows
Thread th = new Thread(new ThreadStart(() => {
Thread.Sleep(1000);
Process script = new Process();
script.StartInfo.FileName = Environment.CurrentDirectory + "\\auto.vbs";
script.StartInfo.Arguments = title;
script.Start();
script.WaitForExit();
script.Close();
}));
th.Start();
return th;
}
//navigating away from the current page by clicking through selenium
selenium.Click(Someotherpage);
//abort the thread if previous statement gets executed
th.Abort();