MCQs on Handling Alerts and Popups | Selenium

Master the essential skills for handling alerts and popups in Selenium with these 30 MCQs. Explore handling JavaScript alerts, managing browser popups, and implementing file uploads and downloads efficiently.


MCQs on Handling Alerts and Popups in Selenium

JavaScript Alerts

  1. What method is used to switch to a JavaScript alert in Selenium WebDriver?
    a) driver.switchTo().alert()
    b) driver.switchTo().window()
    c) driver.manage().alert()
    d) driver.manage().window().alert()
  2. Which method is used to accept a JavaScript alert in Selenium?
    a) alert.click()
    b) alert.ok()
    c) alert.accept()
    d) alert.submit()
  3. How can you dismiss a JavaScript confirmation alert?
    a) alert.dismiss()
    b) alert.close()
    c) alert.cancel()
    d) alert.decline()
  4. What does the getText() method of an alert return?
    a) The page title
    b) The text displayed in the alert
    c) The alert type
    d) The text of the parent window
  5. How can you send text to a prompt alert box?
    a) alert.sendText(“text”)
    b) alert.type(“text”)
    c) alert.setText(“text”)
    d) alert.sendKeys(“text”)
  6. If an alert appears while executing a script, what happens when it is not handled?
    a) The script fails immediately
    b) The script continues without interruption
    c) The browser crashes
    d) The script pauses at the alert
  7. What exception is thrown if no alert is present and driver.switchTo().alert() is called?
    a) NoSuchWindowException
    b) NoAlertPresentException
    c) ElementNotVisibleException
    d) AlertNotFoundException
  8. Which method would you use to verify the presence of an alert in Selenium?
    a) driver.alert.isDisplayed()
    b) driver.switchTo().alert()
    c) driver.isAlertPresent()
    d) alert.exists()
  9. What Selenium WebDriver method allows handling alerts in a non-blocking way?
    a) Explicit waits with ExpectedConditions.alertIsPresent()
    b) driver.manage().timeouts().implicitlyWait()
    c) driver.switchTo().window()
    d) driver.executeScript()
  10. How can you retrieve the default text from a JavaScript alert input field?
    a) alert.getDefaultText()
    b) alert.getText()
    c) alert.sendKeys().defaultValue
    d) driver.alert.defaultText()

Browser Popups

  1. Which method is used to switch to a browser popup window in Selenium?
    a) driver.switchTo().window(windowHandle)
    b) driver.switchTo().alert()
    c) driver.manage().popup()
    d) driver.switchTo().popup()
  2. How can you obtain the unique identifier of a popup window?
    a) driver.getWindowHandle()
    b) driver.getWindowHandles()
    c) driver.switchTo().windowID()
    d) driver.popupHandle()
  3. What is the purpose of getWindowHandles()?
    a) Retrieve the title of all open windows
    b) Retrieve all window identifiers
    c) Switch to the parent window
    d) Close all popups
  4. What happens if you try to interact with a popup without switching to it?
    a) The script runs without issues
    b) A WebDriverException is thrown
    c) The popup automatically closes
    d) The browser crashes
  5. How do you close a popup window in Selenium?
    a) driver.close()
    b) driver.quit()
    c) driver.switchTo().alert().dismiss()
    d) driver.switchTo().popup().close()
  6. How can you return to the main window after interacting with a popup?
    a) driver.switchTo().window(parentWindowHandle)
    b) driver.switchTo().alert().dismiss()
    c) driver.manage().timeouts().implicitlyWait()
    d) driver.quit()
  7. What happens when driver.close() is called on a popup window?
    a) The popup and main window close
    b) Only the popup closes
    c) Only the main window closes
    d) All open windows close
  8. How can you verify the title of a popup window?
    a) driver.switchTo().window(windowHandle).getTitle()
    b) driver.getPopupTitle()
    c) driver.popup.getTitle()
    d) driver.manage().popup().title()
  9. How can you wait for a popup window to appear?
    a) Use explicit waits with ExpectedConditions.numberOfWindowsToBe()
    b) Use driver.manage().timeouts().implicitlyWait()
    c) Use Thread.sleep()
    d) Use driver.switchTo().popup()
  10. How do you maximize a popup window?
    a) driver.switchTo().window(windowHandle).manage().window().maximize()
    b) driver.manage().window().maximize()
    c) driver.popup().maximize()
    d) driver.switchTo().alert().maximize()

File Uploads and Downloads

  1. What Selenium method is used to upload a file?
    a) element.sendKeys(filePath)
    b) element.uploadFile(filePath)
    c) driver.upload(filePath)
    d) fileInput.sendFile(filePath)
  2. What type of element is typically used for file upload in HTML?
    a) input type=”text”
    b) input type=”file”
    c) input type=”upload”
    d) input type=”button”
  3. How can you handle file download prompts in browsers like Chrome or Firefox?
    a) Configure browser settings using desired capabilities
    b) Use driver.switchTo().alert()
    c) Use explicit waits
    d) Disable browser popups
  4. Which WebDriver option is used to specify a default download directory?
    a) browser.download.folderList
    b) options.addPreference(“download.default_directory”, path)
    c) driver.manage().timeouts().downloadDirectory()
    d) driver.getDownloadPath()
  5. How do you handle a file upload dialog in a non-browser context?
    a) Use tools like AutoIt or Robot class
    b) Use driver.sendKeys(filePath)
    c) Use driver.manage().alert()
    d) Use an explicit wait
  6. Which of the following can interrupt a file upload process?
    a) Incorrect file path
    b) Unsupported file type
    c) Browser security settings
    d) All of the above
  7. What must be verified after a file is downloaded using Selenium?
    a) File presence in the download directory
    b) Browser logs
    c) Popup alerts
    d) Window handles
  8. How can you check the progress of a file download in Selenium?
    a) Use external libraries like Apache Commons IO
    b) Use WebDriver’s built-in functions
    c) Monitor console output
    d) Check browser alerts
  9. What method would you use to click a “Choose File” button for file uploads?
    a) element.click()
    b) element.sendKeys(filePath)
    c) element.upload(filePath)
    d) element.press(filePath)
  10. What happens when you try to upload a non-existent file?
    a) An error is thrown
    b) The upload dialog closes silently
    c) The browser crashes
    d) Nothing happens

Answer Key

QnoAnswer (Option with Text)
1a) driver.switchTo().alert()
2c) alert.accept()
3a) alert.dismiss()
4b) The text displayed in the alert
5d) alert.sendKeys(“text”)
6d) The script pauses at the alert
7b) NoAlertPresentException
8b) driver.switchTo().alert()
9a) Explicit waits with ExpectedConditions.alertIsPresent()
10b) alert.getText()
11a) driver.switchTo().window(windowHandle)
12b) driver.getWindowHandles()
13b) Retrieve all window identifiers
14b) A WebDriverException is thrown
15a) driver.close()
16a) driver.switchTo().window(parentWindowHandle)
17b) Only the popup closes
18a) driver.switchTo().window(windowHandle).getTitle()
19a) Use explicit waits with ExpectedConditions.numberOfWindowsToBe()
20a) driver.switchTo().window(windowHandle).manage().window().maximize()
21a) element.sendKeys(filePath)
22b) input type=”file”
23a) Configure browser

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top