Header Ads

How To Solve Popup Issues in Selenium

If there is a scenario where we have to download a file ,usually we get a pop up asking us to click on open save or exit, this post talks about the approach how to solve this issue using selenium.
Before we go deep let us understand how many types of pop up available in Browser world.
There are mainly 3 types of pop up available.

  • JavaScript Popup
  • Browser Popup
  • Native OS popup

Now Selenium can really handle Javascript and Browser popup. Selenium has full control over them. But for Native Window's or OS's popup,selenium could do .
Selenium provides an API to handle JavaScript PopUps out of the box.
Alert alert = driver.switchTo().alert();
Now there are four different methods available to handle the popup.

  • accept()
  • dismiss()
  • getText()
  • sendKeys()
Using these Javascript and browser popups can be handled properly.
But for Native popups we need to take different approaches.

One approach given here to exclude these testcases. It may be possible to convince the customer about the downside of this problem.But if atall we need to perform this operation we may take the following approaches.

The approaches available are:
  • Disable Popup
  • Using Profile
  • Preference
  • Using Sikuli
  • Using AutoIT
  • Using Robot

Disable Popup

In this approach we disable the popup from browser setting and command it what's to be done.It we need to download.We provide that here and give path where we need to download. During run after this step,We copy the downloaded file to our required path. During run popup will not open rather file will be downloaded to the path given. More detailed steps can be found here
Using Profile

Some use profiler concept to handle download popup. The code will look like below:
FirefoxProfile profile = new FirefoxProfile();
        String path = "D:\\Download\\";
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", path);
        profile.setPreference("browser.download.alertOnEXEOpen", false);
        profile.setPreference("browser.helperApps.neverAsksaveToDisk", "application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel");
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.download.manager.focusWhenStarting", false);
        profile.setPreference("browser.helperApps.alwaysAsk.force", false);
        profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.setPreference("browser.download.manager.closeWhenDone", false);
        profile.setPreference("browser.download.manager.showAlertOnComplete", false);
        profile.setPreference("browser.download.manager.useWindow", false);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
        profile.setPreference("pdfjs.disabled", true);

        WebDriver driver = new FirefoxDriver(profile);
It is described here.But many a times it failed.

Using Preference

The Code will be
profile.set_preference("browser.download.panel.shown", False)
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/vnd.ms-excel")
profile.set_preference("browser.download.folderList", 2);
profile.set_preference("browser.download.dir", "c:\\firefox_downloads\\")
browser = webdriver.WebDriver(firefox_profile=profile)

It is described here
Using Sikuli

public class WebdriverSikuli {

    private WebDriver driver;
    private String baseUrl;
    private Screen screen;

    public void setUp() throws Exception {
                         driver = new FirefoxDriver();
                         screen = new Screen();
//Give link of the URL from where the download starts
                         baseUrl = "download URL";
                         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    public void testUntitled() throws Exception {
                         driver.get(baseUrl + "/");
//Give cordinates where it needs to go

    public void tearDown() throws Exception {

Using Robot

This is applicable when the interaction is very limited. Using Robot class we can simulate KeyPress,Tabkeys,Mouse move etc.  But all events happen either on keyboad or based on screen coordinates. So can perform on a single screen resolution. Slight difference in screen resolution may fail test execution. It depends on the focus too. If Focus got shifted from one place to another before invoking this method. It is bound to fail.No String support for keyboard event.It can throw only one character at a time.So little time consuming. It can not capture the property value of the popup like title,String inside the popup. So no custom approach can be taken based on the failure message of the popup. It handles all pop up in a same way. A detailed approach can be found here

Using AutoIT
AutoIT is one of the tool that can automate the windows operation. it creates executable script which can be called from selenium.It is based on image comparison. During creation of the script we provide the popup picture and asks it to compare for the same image during runtime. If matches then it perform the set of operations we want it do. This method is highly dependent on Screen resolution,So it will work exactly same or greater resolution not lesser resolution.
More details can be found here.

image credit:bharath-marrivada.blogspot.com
Powered by Blogger.