0% found this document useful (0 votes)
88 views

Handling Browser Based Alerts and Pop Ups Mouse Hover Actions in Selenium Web Driver

The document discusses various techniques for handling pop-ups, alerts, and windows in Selenium WebDriver. It describes how to accept, dismiss, and get text from alerts using the Alert interface. It also provides examples of performing mouse hover actions using Actions class and handling multiple browser windows by getting window handles and switching between windows.

Uploaded by

sudheer reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Handling Browser Based Alerts and Pop Ups Mouse Hover Actions in Selenium Web Driver

The document discusses various techniques for handling pop-ups, alerts, and windows in Selenium WebDriver. It describes how to accept, dismiss, and get text from alerts using the Alert interface. It also provides examples of performing mouse hover actions using Actions class and handling multiple browser windows by getting window handles and switching between windows.

Uploaded by

sudheer reddy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Handling Browser based alerts and pop ups Mouse Hover


Actions in Selenium Web driver
Handling pop up is one of the most challenging piece of work to automate while testing
web applications. Owing to the diversity in types of pop ups complexes the situation
even more.
What is Alert box/ Pop up box/ confirmation Box/ Prompt/ Authentication
Box?
It is nothing but a small box that appears on the display screen to give you some kind of
information or to warn you about a potentially damaging operation or it may even ask
you for the permissions for the operation.
Example: Let us consider a real life example for a better understanding; Let us assume
that we uploaded a photograph on any of these popular social networking sites. Later
on, i wish to delete the uploaded photograph. So in order to delete, i clicked on the
delete button. As soon as I click on the delete button, the system warns me against my
action, prompting Do you really want to delete the file? So now we have an option to
either accept this alert or reject it.
So ahead in the session, lets see how do we reject or accept the alerts
depending on their types. Starting with the web based pop ups.
Web Based Popups

Let us see how do we handle them using WebDriver.


Handling web based pop-up box
WebDriver offers the users with a very efficient way to handle these pop ups using Alert
interface.
Page 1 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

There are the four methods that we would be using along with the Alert interface.

1) void dismiss() The dismiss() method clicks on the Cancel button as soon as the
pop up window appears.
2) void accept() The accept() method clicks on the Ok button as soon as the pop up
window appears.
3) String getText() The getText() method returns the text displayed on the alert box.
4) void sendKeys(String stringToSend) The sendKeys() method enters the specified
string pattern into the alert box.

Copy the below code in note pad and save it as .Html file, click on the Try It button alert
will generate
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display a confirm box.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
confirm("Press a button!");
}
</script>
</body>
</html>

Page 2 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

Below is the sample code to handling alerts


WebDriver driver=new FirefoxDriver();
//Launch the application
driver.navigate().to("file:///C:/Users/s.r.ankireddygari/Desktop/webdriver(selenium)/
Alerts.HTML");
//Click on button alert will generate
driver.findElement(By.xpath("//button")).click();
//switch to alert
Alert alert=driver.switchTo().alert();
//click on ok
alert.accept();
driver.findElement(By.xpath("//button")).click();
alert=driver.switchTo().alert();
//click on cancel
alert.dismiss();
driver.findElement(By.xpath("//button")).click();
alert=driver.switchTo().alert();
//Get text present on alert
String text= alert.getText();
System.out.println("text:"+text);

Mouse Hover Actions in Selenium Web driver


In order to perform a 'mouse hover' action, we need to chain all of the actions that we
want to achieve in one go. So move to the element that which has sub elements and click
on the child item. It should the same way what we do normally to click on a sub menu
item.
With the actions object you should first move the menu title, and then move to the sub
menu item and click it.
Page 3 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

First we need to create new action builder instance by passing the webdriver instance,
then.
Below is the sample code to perform Mouse hover action
Example 1:
Actions actions = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("menulink"));
actions.moveToElement(mainMenu);

WebElement subMenu = driver.findElement(By.cssSelector("subLinklocator"));


actions.moveToElement(subMenu);
actions.click().build().perform();

Here 'build()' method is used to compile all the list of actions into a single step and
ready to be performed.
Example 2:
Actions action = new Actions(driver);
WebElement mainMenu = driver.findElement(By.linkText("MainMenu"));
action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath("submenuxpath"))).clic
k().build().perform();

There are cases where you may just want to mouse hover on particular element and
check if the button state/color is changing after mouse hover.
Below is the example to perform mouse hover
WebElement searchBtn = driver.findElement(By.id("searchbtn"));

Actions action = new Actions(driver);


action.moveToElement(searchBtn).perform();

WebDriver driver=new FirefoxDriver();


//Launch the application
driver.navigate().to("https://www.irctc.co.in/");
//Implicit wait condition
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

Page 4 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

//Handling mouse hover actions


Actions actions=new Actions(driver);
over action)

//Move the mouse to the expected place(where we need to perform mouse

actions.moveToElement(driver.findElement(By.id("irctc_tourism"))).perform();
//Perform mouse over operation and click on the element
actions.moveToElement(driver.findElement(By.linkText("Air Packages")));
actions.click().build().perform();

Example of Handling Multiple Browser Windows in Selenium


WebDriver
Selenium WebDriver software testing tool has built in
"WebDriver.switchTo().window()" method available to switch from one window to
another window so it is very easy to handle multiple windows in webdriver. If you
remember, We can use "selectWindow" window command in selenium IDE software
Page 5 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

testing tool to select another window. If window do not have any title or both window
has same title then it was very difficult to select other window in selenium IDE software
testing tool. WebDriver software testing tool has made it very easy. You can handle
multiple windows even if all the windows do not have any title or contains same title.

If you wants to work with multiple tabs then viewTHIS POST and wants to work with
multiple IFrames then view THIS POST.
WebDriver.getWindowHandles()
In WebDriver software testing tool, We can use "WebDriver.getWindowHandles()" to
get the handles of all opened windows by webdriver and then we can use that window
handle to switch from from one window to another window. Example Syntax for getting
window handles is as bellow.
Set<String> AllWindowHandles = driver.getWindowHandles();

WebDriver.switchTo().window()
WebDriver.switchTo().window() method is useful to switch from one window to another
window of software web application. Example syntax is as bellow.

driver.switchTo().window(window2);

Bellow given webdriver example of switching window will explain you it deeply. Execute
it in your eclipse and try to understand how webdriver do it.

Copy bellow given @Test method part of handling multiple windows of webdriver and
replace it with the @Test method part of example given on THIS PAGE.(Note : @Test
method is marked with pink color in that linked page).

@Test

Page 6 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

public void test () throws InterruptedException


{
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.findElement(By.xpath("//b[contains(.,'Open New Page')]")).click();

// Get and store both window handles in array


Set<String> AllWindowHandles = driver.getWindowHandles();
String window1 = (String) AllWindowHandles.toArray()[0];
System.out.print("window1 handle code = "+AllWindowHandles.toArray()[0]);
String window2 = (String) AllWindowHandles.toArray()[1];
System.out.print("\nwindow2 handle code = "+AllWindowHandles.toArray()[1]);

//Switch to window2(child window) and performing actions on it.


driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
driver.findElement(By.xpath("//input[@value='Bike']")).click();
driver.findElement(By.xpath("//input[@value='Car']")).click();
driver.findElement(By.xpath("//input[@value='Boat']")).click();
driver.findElement(By.xpath("//input[@value='male']")).click();
Thread.sleep(5000);

//Switch to window1(parent window) and performing actions on it.


Page 7 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

driver.switchTo().window(window1);
driver.findElement(By.xpath("//option[@id='country6']")).click();
driver.findElement(By.xpath("//input[@value='female']")).click();
driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
driver.switchTo().alert().accept();
Thread.sleep(5000);

//Once Again switch to window2(child window) and performing actions on it.


driver.switchTo().window(window2);
driver.findElement(By.xpath("//input[@name='fname']")).clear();
driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("Name
Changed");
Thread.sleep(5000);
driver.close();

//Once Again switch to window1(parent window) and performing actions on it.


driver.switchTo().window(window1);
driver.findElement(By.xpath("//input[@value='male']")).click();
Thread.sleep(5000);

Page 8 of 9

SUDHEER REDDY ANKIREDDYGARI


SELENIUM WEBDRIVER

Page 9 of 9

You might also like