POP-UP


·       Tool tip pop-up

·       Calendar pop-up

·       Java script, conformation & prompt pop-up

·       Page on load authentication pop-up

·       File upload pop-up

·       File download pop-up

·       New tab / new window / browser pop-up


TOOL TIP POP-UP

Tool tips are two types

 

1 Generated using title attribute

2 Has a pop up in the html(division pop-up)

 

If the tool tips coming from the title attribute, we can find the element and use getAttribute() to get tool tip text.

If the tool tips coming from div pop up we can find div using driver.findElement() and use getText() to get text from the division

 

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import  org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import  org.openqa.selenium.firefox.FirefoxDriver;

 

public class ToolTipPopup {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.gsmarena.com/samsung-phones-9.php");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


String tool=driver.findElement(By.xpath("//img[contains(@src,'https://fdn2.gsmarena')]")).getAttribute("title");

System.out.println(tool);

}

}


CALENDAR  POP-UP

There are generally two kinds of calendar fields.

1 Which allows you to type the date.

2 Which allows you to select the date from the calendar pop up.

 

If the calendar allows to type the date use driver.findElement() and sendKeys().

 

If it is a pop up then click on the calendar icon to make the calendar icon visible. Use an Xpath to find the date and click on it.

Note: Calendar pop up will generally be in a table structure.

           Web driver cannot identify the hidden elements.

 

package mypackage;

 

import  java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class CalendarPopup {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.redbus.in/");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.findElement(By.xpath("//div[@class='fl search-box date-box gtm-onwardCalendar']")).click();

driver.findElement(By.xpath("//td[@class='next']")).click();

driver.findElement(By.xpath("//td[text()='2']")).click();

}

}

 

JAVA SCRIPT ALERT, CONFORMATIONM & PROMPT POP-UP

Alert: only one button ie ok

Conformation: It has two buttons ie ok and cancel

Prompt: in has two button and text ie ok, cancel and text

 

Java script alert conformation cannot handle by driver.findElement() as iit does not have any html structure java script is generated for alert( ) of java script and have only one button ok and use to provide the user some information

 

Confirmation pop-up is generated from confirm() method of javascript and has two buttons, ok and cancel and based on the inputs execution happens.

To handle javascript alert or confirmation pop-up, we need to first switch to the pop-up using driver.switchTo().alert() which will return on object of alert. Using this object we can

click on ok using accept ()
click on cancel using dismiss()
get the text on the pop-up using get text
type the text on the pop up using sendkeys().

 

ALERT POPUP

 

package mypackage;

 

import  java.util.concurrent.TimeUnit;

 

import  org.openqa.selenium.By;

import  org.openqa.selenium.WebDriver;

import  org.openqa.selenium.firefox.FirefoxDriver;

 

public class AlertPopup {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.rediff.com");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.findElement(By.xpath("//a[text()='Sign in']")).click();

driver.findElement(By.name("proceed")).click();

                   

System.out.println(driver.switchTo().alert().getText());

driver.switchTo().alert().accept();

}

}

 

CONFORMATION POPUP

 

package mypackage;

 

import  java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import  org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class ConformationPopup {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://demo.guru99.com/test/delete_customer.php");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.findElement(By.name("cusid")).sendKeys("nani");

driver.findElement(By.name("submit")).click();

System.out.println(driver.switchTo().alert().getText());

driver.switchTo().alert().dismiss();

}

}

 

PROMPT POPUP

 

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import  org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class PromptPopup {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://demo.guru99.com/test/delete_customer.php");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.findElement(By.name("cusid")).sendKeys("nani");

driver.findElement(By.name("submit")).click();

System.out.println(driver.switchTo().alert().getText());

driver.switchTo().alert().accept();

System.out.println(driver.switchTo().alert().getText());

driver.switchTo().alert().accept();

}

}


AUTHENTICATION POP-UP

By default selenium cannot handle on load authentication pop-up to over come the problem we can pass the user name and password in the url with the following syntax

Syntaxhttp://username:password@ipaddress/dominename

 

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

 

public class AuthenticationPopup {

 

public static void main(String[] argsthrows InterruptedException {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://adein:manager@ww.engprod-charter.net");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

}


FILE UPLOAD POP-UP

How do you do file upload in web Driver

We can use driver.FindElement() to find the element and use sendkeys() and pass the full path of the file as an argument to sendkeys().

 

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.remote.server.handler.SendKeys;

 

public class FileuploadPopup {

 

public static void main(String[] args) {

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("https://www.w3schools.com/howto/howto_html_file_upload_button.asp");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);


       driver.findElement(By.id("myFile")).sendKeys("C:\\Users\\ramak\\Downloads.selenium_logo");

}

}


FILE DOWNLOAD POP-UP

    To save a file to disk automatically without the pop-up, we can take help of Firefox preferences by using Firefox profile.

Case 1:

To save the file to the downloads folder, we can use the preference browser.helperApps.neverAsk.saveToDisk and provide the file MIME type as a value for the preference i.e., application/zip. If there are multiple files types to be download, we can provide (,) separated list of MIME types of all the files.

FirefoxProfile prof=new FirefoxProfile();
prof.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip");

How to get preference name

  • Go to firefox browser
  • search about:config
  • click on I’ll be careful, I promise
  • search preference and copy and past.

How to get preference value / mime value

  • Go to browser
  • Search site point.com or mime type
  • Copy and paste

 

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import  org.openqa.selenium.WebDriver;

import  org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxProfile;

 

public class FileDownloadPopup {

 

public static void main(String[] args) {

              

FirefoxProfile profile=new FirefoxProfile();

profile.setPreference("browser.helperApps.neverAsk.saveToDisk",  "application/zip");

 

     System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://demo.automationtesting.in/FileDownload.html");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

              

driver.findElement(By.id("pdfbox")).sendKeys("Ramakrishna");

driver.findElement(By.id("createPdf")).click();

driver.findElement(By.id("pdf-link-to-download")).click();

}

}

Case 2

If the file has to be downloaded to desktop along with browser.helperApps.neverAsk.saveToDisk preference

we need to provide another preference which is browser.download.folderList with an integer value 0.

By default the value for browser.download.folderList will be 1 which represents download folder.

 

How to get folder set preference

  • Go to firefox browser
  • search about:config
  • click on I’ll be careful, I promise
  • search preference and copy and paste.


 

package mypackage;

 

import  java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxProfile;

 

public class FileDownloadPopup {

 

public static void main(String[] args) {


FirefoxProfile profile=new FirefoxProfile();

profile.setPreference("browser.helperApps.neverAsk.saveToDisk",  "application/zip");

profile.setPreference("browser.download.folderList", 0);

 

    System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://demo.automationtesting.in/FileDownload.html");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

              

driver.findElement(By.id("pdfbox")).sendKeys("Ramakrishna");

driver.findElement(By.id("createPdf")).click();

driver.findElement(By.id("pdf-link-to-download")).click();

}

}

Case 3:

Save file to custom folder path.

To download a file to custom folder path we need to set the preference browser.download.folderList with integer value 2 and we also need to set the preference browser.download.dir with the value which is custom folder path.

Note: Don’t forget to set the preference in Firefox class which is done in common for each case.

Preference name can be taken from about:config of Mozilla firefox browser MIME type can be taken from google or https://www.sitepoint.com/mime-types-complete-list/

 

package mypackage;

 

import java.util.concurrent.TimeUnit;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxProfile;

 

public class FileDownloadPopup {

 

public static void main(String[] args) {

                   

FirefoxProfile profile=new FirefoxProfile();

profile.setPreference("browser.helperApps.neverAsk.saveToDisk",  "application/zip");

profile.setPreference("browser.download.dir ""C:\\Users\\ramak\\music");

 

System.setProperty("webdriver.gecko.driver","C:\\Browser\\geckodriver.exe");

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://demo.automationtesting.in/FileDownload.html");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

                   

driver.findElement(By.id("pdfbox")).sendKeys("Ramakrishna");

driver.findElement(By.id("createPdf")).click();

driver.findElement(By.id("pdf-link-to-download")).click();

}

}