Automated Tester


This content shows Simple View

August 2015

Manipulating JavaScript Alerts with Selenium

If your application under test generates JavaScript alerts or dialogues how can we interact with these using Selenium rather than look elsewhere such as AutoIt?

Luckily we can manipulate these dialogues with Selenium to either accept (click Ok) or dismiss (click Cancel) the pop up allowing your test(s) to continue.

You can also get the text of an alert if you need to make assertions against it or send keystrokes to it.

IAlert alert = Context.Driver.SwitchTo().Alert();
var alertText = alert.Text;
// Dismiss the Alert
// alert.Dismiss();

Here we are switching to the active alert on screen, committing the text of it to a variable and then accepting the alert. We do not need to find elements on the dialogue in order to interact with them as the alert is not HTML based.

After this interaction occurs, focus will automatically resume on the main browser so we do not need to switch back to the browser.

Gallio Test Runner

Gallio (sometimes known as MB Unit or Gallio Icarus) is a test runner which to me has two distinct advantages over MsTest and NUnit.

Parallel test execution support and built in test reports, although NUnit does have parallel support with PNUnit.

How to set it up

  • Download and install Gallio from here.
  • In your Visual Studio Project navigate to Project > [project name] Properties from the drop down menu.
  • Click Debug
  • Set startup program to Gallio, by default it should be somewhere like: C:\Program Files\Gallio\bin\Gallio.Icarus.exe
  • In Start Options > Command Line Arguments paste in the path of your projects DLL housing the tests you want to execute
  • Save
  • Hit Start when you want to run or debug your tests


Gallio GUI

This looks very similar to NUnit but with some extra buttons and menus, the only ones you will really care about are start/ stop/ debug and test reports.


The reports aren’t anywhere near as nice as that you would find in Allure, but they are easily generated and easy to view.


Bootnote: In Gallio Icarus select menu Tools -> Options, select page “Preferences”, set “Test Runner Factory” to IsolatedAppDomain or Local to get the debugger to work.