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
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()
Which method is used to accept a JavaScript alert in Selenium? a) alert.click() b) alert.ok() c) alert.accept() d) alert.submit()
How can you dismiss a JavaScript confirmation alert? a) alert.dismiss() b) alert.close() c) alert.cancel() d) alert.decline()
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
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”)
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
What exception is thrown if no alert is present and driver.switchTo().alert() is called? a) NoSuchWindowException b) NoAlertPresentException c) ElementNotVisibleException d) AlertNotFoundException
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()
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()
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
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()
How can you obtain the unique identifier of a popup window? a) driver.getWindowHandle() b) driver.getWindowHandles() c) driver.switchTo().windowID() d) driver.popupHandle()
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
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
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()
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()
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
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()
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()
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
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)
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”
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
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()
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
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
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
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
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)
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
Qno
Answer (Option with Text)
1
a) driver.switchTo().alert()
2
c) alert.accept()
3
a) alert.dismiss()
4
b) The text displayed in the alert
5
d) alert.sendKeys(“text”)
6
d) The script pauses at the alert
7
b) NoAlertPresentException
8
b) driver.switchTo().alert()
9
a) Explicit waits with ExpectedConditions.alertIsPresent()
10
b) alert.getText()
11
a) driver.switchTo().window(windowHandle)
12
b) driver.getWindowHandles()
13
b) Retrieve all window identifiers
14
b) A WebDriverException is thrown
15
a) driver.close()
16
a) driver.switchTo().window(parentWindowHandle)
17
b) Only the popup closes
18
a) driver.switchTo().window(windowHandle).getTitle()
19
a) Use explicit waits with ExpectedConditions.numberOfWindowsToBe()
20
a) driver.switchTo().window(windowHandle).manage().window().maximize()