Selenium-popups - Notes By ShariqSP
Popup Examples
Javascript Popup
Hidden Division Popup
Child Browser Popup
File Upload Popup
Window Popup
Notification Popup
Authentication Popup
User name : shariqPassword : Shariq
JavaScript Alert(), Confirm(), and Prompt() Methods
In Selenium, you can handle JavaScript alert, confirmation, and prompt dialogs using the following methods:
- alert(): Displays an alert dialog with a message.
- confirmation(): Displays a confirmation dialog with OK and Cancel buttons.
- prompt(): Displays a dialog with a message prompting the user to input some text.
Usage in Selenium:
These methods are commonly used for handling JavaScript dialogs that might appear during automated testing. For example:
// Handling an alert dialog
Alert alert = driver.switchTo().alert();
String alertMessage = alert.getText();
alert.accept(); // Clicking OK button
// open netbanking of hdfcbank drag and drop
driver.get("https://netbanking.hdfcbank.com/netbanking/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//locate iframe
driver.switchTo().frame(0);
WebElement continu=driver.findElement(By.linkText("CONTINUE"));
Actions act = new Actions(driver);
act.click(continu).perform();
Thread.sleep(2000);
//capture alert message
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
//accept alert pop up [we have OK button]
alert.accept();
// Handling a confirmation dialog
Alert confirmation = driver.switchTo().alert();
confirmation.dismiss(); // Clicking Cancel button
//locate close button on first pop-up
WebElement button = driver.findElement(By.xpath("//button[text()='Close']"));
Actions act = new Actions(driver);
act.click(button);
WebElement login = driver.findElement(By.partialLinkText("Login"));
act.moveToElement(login).perform();
WebElement agentLogin = driver.findElement(By.linkText("Agent Portal"));
act.moveToElement(agentLogin).click().perform();
Alert alert = driver.switchTo().alert();
alert.accept();
// Handling a prompt dialog
Alert prompt = driver.switchTo().alert();
prompt.sendKeys("Input text");
prompt.accept(); // Clicking OK button
//using prompts
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_prompt");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("//button[text()='Try it']")).click();
Alert alert=driver.switchTo().alert();
alert.sendKeys("is it working??");
alert.accept();
//calender pop-up-search room in agoda on selected dates
public static void main(String[] args) throws InterruptedException {
// we are using dynamic Xpath to select todays date and next day
// so we'll use LocalDatetime, next we'll extract month day and year and we use
// the same in xpath
// since we are not hardcoding xpath and its based on code
// it'll be called as dynamic xPath
LocalDateTime time = LocalDateTime.now();
// get date and year
int year = time.getYear(), day = time.getDayOfMonth();
//get month
String month = time.getMonth().toString().toLowerCase();
// since month is in uppercase like FEBRUARY
// but in xpath its February so lets firmate it
month = month.substring(0, 1).toUpperCase() + month.substring(1, month.length());
System.out.println(day + " " + month + " " + year);
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://agoda.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
// lets handle the pop being dsiplayed and click on No Thanks
// we dont know when the pop up will come so we'll wait untill pop
// up is displayed first then we'll handle it
// to do this we are using infinite for loop and we'll break once element is found
for (;;) {
Thread.sleep(1);
if (driver.findElement(By.xpath("//button[text()='No thanks']")).isDisplayed())
break;
}
driver.findElement(By.xpath("//button[text()='No thanks']")).click();
// lets locate text box and enter Banglore as destination
Actions act = new Actions(driver);
WebElement search = driver.findElement(By.id("textInput"));
search.sendKeys("bangalore");
// now use Key.ARROW_DOWN key to select first location being displayed
act.sendKeys(search, "bangalore").perform();
act.moveToElement(search).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
// locate and click on startdate
Thread.sleep(1000);
WebElement sDate = driver.findElement(By.id("check-in-box"));
act.click(sDate).perform();
act.click(sDate).perform();
Thread.sleep(1000);
// now locate previous month button and click
WebElement pMonth = driver.findElement(By.xpath("//button[@aria-label='Previous Month']"));
act.click(pMonth).perform();
Thread.sleep(2000);
// now locate todays date and click
// WebElement nextDay = driver.findElement(By.xpath("//div[text()='"+month+""+year+"']/..//span[text()='24']"));
WebElement todays = driver.findElement(By.xpath("//div[text()='" + month + " " + year + "']/..//span[text()='" + day + "']"));
act.click(todays).perform();
Thread.sleep(2000);
// now locate nextDay date and click
// WebElement nextDay = driver.findElement(By.xpath("//div[text()='February 2024']/..//span[text()='25']"));
WebElement nextDay = driver.findElement(
By.xpath("//div[text()='" + month + " " + year + "']/..//span[text()='" + (day + 1) + "']"));
act.click(nextDay).perform();
// now locate and click rooms button with default values
WebElement rooms = driver.findElement(By.xpath("//div[@data-selenium='occupancyBox']"));
act.click(rooms).perform();
// now locate search button and click
WebElement Search = driver.findElement(By.xpath("//span[text()='SEARCH']"));
act.click(Search).perform();
Thread.sleep(10000);
driver.close();
}
// Task : search flight in makemytrip on april 10th
public static void main(String[] args) throws InterruptedException {
// we are using dynamic Xpath to select todays date and next day
// so we'll use LocalDatetime, next we'll extract month day and year and we use
// the same in xpath
// since we are not hardcoding xpath and its based on code
// it'll be called as dynamic xPath
LocalDateTime time = LocalDateTime.of(2024, 4, 24, 10, 00);
// get date and year
int year = time.getYear(), day = time.getDayOfMonth();
// get month
String month = time.getMonth().toString().toLowerCase();
// since month is in uppercase like FEBRUARY
// but in xpath its February so lets firmate it
month = month.substring(0, 1).toUpperCase() + month.substring(1, month.length());
System.out.println(day + " " + month + " " + year);
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
// now locate fromcity
Thread.sleep(2000);
driver.findElement(By.id("fromCity")).click();
// now locate new input box which will be appearing next and send bengaluru and enter
Thread.sleep(3000);
WebElement fromCityElement = driver.findElement(By.xpath("//input[@placeholder='From']"));
Thread.sleep(2000);
fromCityElement.sendKeys("bengaluru");
Thread.sleep(2000);
fromCityElement.sendKeys(Keys.ARROW_DOWN);
fromCityElement.sendKeys(Keys.ENTER);
// now locate Tocity
Thread.sleep(2000);
driver.findElement(By.id("toCity")).click();
// now locate new input box which will be appearing next and send delhi and enter
Thread.sleep(3000);
WebElement toCityElement = driver.findElement(By.xpath("//input[@placeholder='To']"));
Thread.sleep(2000);
toCityElement.sendKeys("Delhi");
Thread.sleep(2000);
toCityElement.sendKeys(Keys.ARROW_DOWN);
toCityElement.sendKeys(Keys.ENTER);
//now locate next month button n click
Thread.sleep(2000);
driver.findElement(By.xpath("//span[@aria-label='Next Month']")).click();
//now select date as april 10
driver.findElement(By.xpath("//div[text()='April 2024']/../..//p[text()='10']")).click();
//now locate search and click
driver.findElement(By.xpath("//a[text()='Search']")).click();
Thread.sleep(10000);
driver.close();
}
// child broswer pop up using selenium notes by shariq - popups web page
// OPEN POPUP page and locate childbroswer popup button and click then new childbroswer will open there
// google page will be loaded switch to it
// then search for something
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
//using username and password with url
driver.get("https://master.d2l7vlmilqwd6o.amplifyapp.com/selenium/popups.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
Thread.sleep(2000);
//locating child broswer pop button
driver.findElement(By.xpath("//button[text()='Open Child Browser']")).click();
Set broswers=driver.getWindowHandles();
for(String i : broswers)
{
if(driver.switchTo().window(i).getTitle().contains("Google"))
{
driver.switchTo().window(i);
Thread.sleep(2000);
driver.findElement(By.name("q")).sendKeys("shariq sp");
Thread.sleep(2000);
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
}
}
Thread.sleep(5000);
driver.quit();
}
//FILE UPLOAD POPUP using sendKeys : upload your resume to naukari.com registration page
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.naukri.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
//locate register button and click on it
Thread.sleep(2000);
driver.findElement(By.linkText("Register")).click();
//locate i am experienced button and click on it
Thread.sleep(2000);
driver.findElement(By.xpath("//h2[text()=\"I'm experienced\"]")).click();
//locate search box and pass file path
Thread.sleep(2000);
driver.findElement(By.id("resumeUpload")).sendKeys("C:\\Users\\Dell\\Documents\\resume.pdf");
Thread.sleep(5000);
driver.close();
}
//FILE UPLOAD POPUP using Robot class and AWT : upload your resume to naukari.com registration page
public static void main(String[] args) throws InterruptedException, AWTException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.naukri.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
//locate register button and click on it
Thread.sleep(2000);
driver.findElement(By.linkText("Register")).click();
//locate i am experienced button and click on it
Thread.sleep(2000);
driver.findElement(By.xpath("//h2[text()=\"I'm experienced\"]")).click();
//locate i am upload button and click on it
Thread.sleep(2000);
driver.findElement(By.xpath("//button[text()='Upload Resume']")).click();
Robot robot = new Robot();
Thread.sleep(3000);
for(int i=0;i<8;i++) {
Thread.sleep(1000);
robot.keyPress(java.awt.event.KeyEvent.VK_TAB);
}
for(int i=0;i<2;i++) {
Thread.sleep(1000);
robot.keyPress(java.awt.event.KeyEvent.VK_DOWN);
}
Thread.sleep(2000);
robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
Thread.sleep(4000);
for(int i=0;i<3;i++) {
Thread.sleep(1000);
robot.keyPress(java.awt.event.KeyEvent.VK_TAB);
}
Thread.sleep(2000);
robot.keyPress(java.awt.event.KeyEvent.VK_R);
Thread.sleep(2000);
robot.keyPress(java.awt.event.KeyEvent.VK_DOWN);
Thread.sleep(2000);
robot.keyPress(java.awt.event.KeyEvent.VK_ENTER);
Thread.sleep(1000);
robot.keyRelease(java.awt.event.KeyEvent.VK_TAB);
Thread.sleep(1000);
robot.keyRelease(java.awt.event.KeyEvent.VK_DOWN);
Thread.sleep(1000);
robot.keyRelease(java.awt.event.KeyEvent.VK_ENTER);
Thread.sleep(1000);
robot.keyRelease(java.awt.event.KeyEvent.VK_R);
Thread.sleep(5000);
driver.quit();
}
//FILE UPLOAD POPUP using autohotkey : upload your resume to naukari.com registration page
installing autohotkey application
click here to open and download
afetr install open editor write the below script and save
Fileupload.ahk
send("C:\Users\Dell\Downloads\resume.pdf")
sleep(2000)
send("{TAB}")
sleep(2000)
send("{TAB}")
sleep(2000)
send("{ENTER}")
public static void main(String[] args) throws InterruptedException, AWTException, IOException
{
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.naukri.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
//locate register button and click on it
Thread.sleep(2000);
driver.findElement(By.linkText("Register")).click();
//locate i am experienced button and click on it
Thread.sleep(2000);
driver.findElement(By.xpath("//h2[text()=\"I'm experienced\"]")).click();
//locate i am upload button and click on it
Thread.sleep(2000);
driver.findElement(By.xpath("//button[text()='Upload Resume']")).click();
Thread.sleep(2000);
Process process = new ProcessBuilder("C:\\Program Files\\AutoHotkey\\UX\\AutoHotkeyUX.exe", "C:\\Users\\Dell\\Documents\\AutoHotkey\\Fileupload.ahk").start();
Thread.sleep(10000);
driver.quit();
}
window popup
// open globalsqa page and navigate to frames and window
// there you have to locate and click on click her in open new window
// then switch to a new window which is opened
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.globalsqa.com/demo-site/frames-and-windows/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
// now locate click here
driver.findElement(By.linkText("Click Here")).click();
// now switch to new window opened
Set windowsIDs = driver.getWindowHandles();
for(String id : windowsIDs)
{
driver.switchTo().window(id);
//search for a perticular word or something in url ad=nd stop loop when we find it
if(driver.getCurrentUrl().contains("#"))
{
System.out.println("we have switched to new window created");
//to confirm samepage that is opened lets search for frames in seach box
driver.findElement(By.id("s")).sendKeys("frames");
driver.findElement(By.id("s")).sendKeys(Keys.ENTER);
break;
}
}
Thread.sleep(5000);
driver.close();
}
//handle notification pop in yathra.com
public static void main(String[] args) throws AWTException, InterruptedException {
//handling using incognito Mode
ChromeOptions opt = new ChromeOptions();
opt.addArguments("--incognito");
//openbrowser by disbling notifications
ChromeOptions opt = new ChromeOptions();
opt.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver(opt);
driver.get("https://www.yatra.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
//handle using robot class
Thread.sleep(3000);
Robot robot = new Robot();
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_TAB);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(1000);
robot.keyRelease(java.awt.event.KeyEvent.VK_TAB);
Thread.sleep(1000);
robot.keyRelease(java.awt.event.KeyEvent.VK_DOWN);
Thread.sleep(1000);
robot.keyRelease(java.awt.event.KeyEvent.VK_ENTER);
Thread.sleep(10000);
driver.quit();
}
//Authentication popup
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
//using username and password with url
driver.get("https://admin:admin@the-internet.herokuapp.com/basic_auth");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Actions act = new Actions(driver);
//using robot class to send username and password
}
Popup Interview Questions
- What are popups in the context of web applications?
- How do you handle popups in Selenium WebDriver?
- Explain the difference between modal popups and non-modal popups.
- What are the different types of popups you have encountered while testing web applications?
- How do you identify the presence of a popup in Selenium WebDriver?
- What methods are available in Selenium WebDriver to interact with popups?
- Explain the switchTo() method in Selenium WebDriver and its usage in handling popups.
- How do you handle alert popups in Selenium WebDriver?
- How do you handle confirmation popups in Selenium WebDriver?
- How do you handle prompt popups in Selenium WebDriver?
- What are the common challenges faced while handling popups in Selenium WebDriver?
- Explain the getWindowHandles() method in Selenium WebDriver and its usage in handling popups.
- How do you close a popup window in Selenium WebDriver?
- What is the purpose of the Actions class in handling popups?
- Explain the Alert interface in Selenium WebDriver and its methods for handling popups.
- How do you handle multiple popups in Selenium WebDriver?
- What is the difference between getWindowHandle() and getWindowHandles() methods?
- How do you handle browser-based authentication popups in Selenium WebDriver?
- What is the purpose of the Robot class in handling popups?
- How do you handle file upload popups in Selenium WebDriver?
Multiple Choice Questions (MCQs)
- Which method is used to switch to a popup window in Selenium WebDriver?
- switchToWindow()
- switchToPopup()
- switchToFrame()
- switchTo()
- Which interface is used to handle alert popups in Selenium WebDriver?
- Alert
- Popup
- Window
- Confirmation
- How do you accept an alert popup in Selenium WebDriver?
- acceptAlert()
- accept()
- confirm()
- dismiss()
- Which method is used to close a popup window in Selenium WebDriver?
- closeWindow()
- closePopup()
- quit()
- close()
- How do you dismiss a confirmation popup in Selenium WebDriver?
- dismiss()
- accept()
- dismissConfirmation()
- cancel()
- Which method is used to retrieve the text from an alert popup in Selenium WebDriver?
- getText()
- alertText()
- getAlertText()
- retrieveText()
- What is the purpose of the getWindowHandles() method in Selenium WebDriver?
- To retrieve the handles of all open windows and popups.
- To retrieve the handle of the current window or popup.
- To switch to a specific window or popup.
- To close the current window or popup.
- Which method is used to switch to a specific window or popup in Selenium WebDriver?
- switchToWindow()
- switchToPopup()
- switchToFrame()
- switchTo()
- How do you handle prompt popups in Selenium WebDriver?
- sendKeys()
- typeText()
- sendPrompt()
- enterText()
- Which method is used to retrieve the current window or popup handle in Selenium WebDriver?
- getWindowHandle()
- getCurrentHandle()
- getCurrentWindow()
- getCurrentPopup()