Handle Multiple Browser Tabs and Alerts with Selenium WebDriver
But we can make use of the existing Selenium commands in a smarter way to automate this scenario. Although this scenario is quite rare and you will not encounter it usually.
1. Handle Multiple Browser Tabs with Selenium
Use Selenium Actions Class to Handle Multiple Browser Tabs
1.1. Launch google.com with Selenium WebDriver
// Set Driver path System.setProperty("webdriver.chrome.driver", "C:\\AUTOMATION\\chromedriver.exe"); // Initialize driver WebDriver driver=new ChromeDriver(); //Maximize browser window driver.manage().window().maximize(); //Go to URL driver.get("http://www.google.com"); //Set timeout driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
1.2. Automate Opening of two browser tabs
// Open new tab by pressing ctrl + t driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); //Go to URL driver.get("http://www.gmail.com"); //Set new tab timeout driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Do some operations on gmail like login
1.3. Automate Switching between two tabs during execution of automation script
// Switch back to first tab (google.com) by pressing ctrl + tab driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); driver.switchTo().defaultContent(); Thread.sleep(2000); // Do some operation on google.com
Code to Handle two Browser Tabs with Selenium:
public class SwitchBetweenBrowserTab { public static void main(String[] a) throws InterruptedException {
// Set Driver path System.setProperty("webdriver.chrome.driver", "C:\\AUTOMATION\\geckodriver.exe");// Initialize driver WebDriver driver = new FirefoxDriver(); //Maximize browser window driver.manage().window().maximize(); //Go to URL driver.get("http://www.google.com"); //Set timeout driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Open new tab driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t"); //Go to URL driver.get("http://www.gmail.com"); //Set new tab timeout driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Do some operation driver.findElement(By.id("gmail-sign-in")).click(); driver.findElement(By.id("Email")).sendKeys("WebDriver"); driver.findElement(By.id("Passwd")).sendKeys("WebDriver"); driver.findElement(By.id("signIn")).click(); Thread.sleep(2000); // Switch first tab driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); driver.switchTo().defaultContent(); Thread.sleep(2000); // Write search String driver.findElement(By.id("gbqfq")).sendKeys("WebDriver"); // Click on Search button driver.findElement(By.id("gbqfb")).click(); Thread.sleep(2000); // Browser close driver.close(); } }
Program Explanation:
- Open Google.com
- Then open a new tab by making use of Key class. By combination of CTRL + t key a new browser tab will be opened.
- Open Gmail.com in that tab.
- Perform some actions on Gmail sign in page.
- Switch back to google.com tab by pressing Ctrl + \t
- Now enter some keywords in google search box
- Close the browsers.
2. Handle Alerts with Selenium WebDriver
- Web Alerts
- Window Alerts
2.1. What is a Web Alert?
A Web alert is generally a JavaScript based pop-up that appears on the screen. It commonly have a text message and two buttons which could be Yes or No, Accept or Dismiss, Ok or Cancel.2.2. How to Handle an Alert with Selenium?
We already know that there aren't much operations we can perform on an alert. There is an Alert class in Selenium WebDriver, all the alert handling commands are present inside that class. We have to create the object of Alert class and then we can call those commands to handle alerts. Below is the syntax how to use those commands to handle web alerts:
/* Alert handling */ /* Alert class object creation * And switches the control/focus of your execution to the alert */ Alert alert = driver.switchTo().alert(); // Below command is used to accept the alert // like click on Yes, Accept, Ok button alert.accept(); // Below command is used to dismiss/reject the alert // like click on No, Dismiss, Cancel button alert.dismiss();
- First of all we have created the object of Alert class and named it as 'alert'. And on the right side of '=' we are switching the focus of the WebDriver execution to the alert so that the next commands could be performed on the alert and not on the web page present below the alert.
- alert.accept() command is to accept any alert. Like if we want to click on the Yes, Accept or Confirm button on any alert then we'll use this command.
- alert.dismiss() command is used to dismiss an alert. Like if you want to click on No, Cancel, Dismiss button on any alert then we'll use this command.
Apart from clicking on the OK or Cancel buttons another action which needs to be performed on an alert while automating is to validate the message displayed on the alert. We have the alert.getText() command in Alert class which can be used to validate alert message.
Example Code to Validate Alert message/text:/* Validate Alert Message with Selenium WebDriver */ /* Alert class object creation * And switches the control/focus of your execution to the alert */ Alert alert = driver.switchTo().alert(); // Expected Text String expectedPopUpText = "Congratulations!"; // Expected text - getText() is the command to get text from a pop-up String actualPopUpText = alert.getText(); // Print pop up text value before validation System.out.println(actualPopUpText); // Validate text if expectedPopUpText == actualPopUpText){ System.out.println("Pop Up Text matched."); } else{ System.out.println("Pop Up Text does not matched."); }
- Declare a string and initialize it with expected message value.
- Create and initialize Alert class object named as 'alert'.
- Get the value of the text message from the alert using getText() command.
- Print the text value.
- Match the value with the expected value using if/else condition and print the success or failure message.
Comments
Post a Comment