How to handle popups of any Website
What are popups?
What are popups?
- Popups are launched automatically from the website and they act as a Interruption
- Popups windows are basically of 2 types
- HTML( Act like a new Frame)
- Non-HTML( Act like a Alert)
- In order to differentiate between them just inspect the popup window with Firebug
- Selenium has given us Alert Class to handle the Non-HTML popups
Non-HTML Pop-up window |
The following code will get the text written on the Pop-up
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class Alert {
public static void main(String[] args) throws IOException, InterruptedException {
WebDriver a= new FirefoxDriver();
a.manage().window().maximize();
a.get("http://www.ksrtc.in/");
a.findElement(By.xpath(".//*[@class='button1 redbtn']")).click();;
Thread.sleep(2000);
org.openqa.selenium.Alert s= a.switchTo().alert();
String aa=s.getText();
System.out.print(aa);
a.quit();
}
}
Notes
1-Alert is a class and we can create a reference variable for Alert class
2-SwitchTo() is a method offered by Selenium webdriver which act as a switching point to the new window which can be Alert and Frames
3- 3 different type of action can be taken on alert window for which 3 methods are available in Alert Class
- Accept():- To Click on Ok/Submit button on the Alert
- Decline(): Close the Alert window/Click on Cancel button on the Alert window
- getText():- Returns exact message of the Alert
Output
0 comments:
Post a Comment