How To Solve Popup Issues in Selenium

1
191
Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Popup in Selenium
Popup in Selenium

Handling Popup is not easy in Selenium.This post talks about handle popup in selenium,code to handle popup in selenium webdriver,command to handle popup in selenium,handle authentication popup in selenium webdriver,handle authentication popup in selenium webdriver chrome,handle browser popup in selenium,handle certificate popup in selenium,handle download popup in selenium,handle login popup in selenium,handle windows based popup 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.openFile","text/csv,application/vnd.ms-excel")
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;
@BeforeSuite
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);
}
@Test
public void testUntitled() throws Exception {
driver.get(baseUrl + "/");
//Give cordinates where it needs to go
driver.findElement(By.linkText("20.30.0")).click();
screen.click("images/SaveFileButton.png");
}
@AfterSuite
public void tearDown() throws Exception {
driver.quit();
}
}

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.

However AutoIT is the best available tool to handle windows dialog or popups. AutoIT can handle modal dialog boxes,save dialog boxes,Username and password dialog boxes.

AutoIt is a free scripting language designed to automate the window component. It is similar to extensive VB Script and allows us to convert the script to an executable(.exe)
More details can be found here. AutoIT provides command to handle popup in selenium or provides ways to handle windows based popup in selenium.

image credit:bharath-marrivada.blogspot.com

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here