JavaScript Popup Boxes - Yash-777/SeleniumDriverAutomation GitHub Wiki

JavaScript Popup Boxes for User interaction

Type of Popup Boxes are:

  • alert
  • confirm
  • prompt

NOTE « JavaScript Popup Boxes like alert,confirm & prompt windows which halt's the execution of JavaScript until the dialog box is closed. The main purpose is to make sure the information comes from user but not from the script.

  • Alert: The Window.alert() method displays an alert dialog with the optional specified content and an OK button.

Syntax:

var message = 'Hello world!';
window.alert(message);

Alert Box

  • Confirm: The Window.confirm() method displays a modal dialog with an optional message and two buttons, OK and Cancel.

Syntax:

var message = "Do you really want to leave?";
var isVlaueOK = window.confirm(message);

if ( isVlaueOK ) { 
  window.open("exit.html", "Thanks for Visiting!");
}

Confirmation Box

  • Prompt: The Window.prompt() displays a dialog with an optional message prompting the user to input some text.

Syntax:

var message = "What's your sign?", defaultVal = 'scorpio';
var userMessage = window.prompt(message, defaultVal);

if ( userMessage.toLowerCase() == "scorpio" ) {
  alert("Wow! I'm a Scorpio too!");
}

Prompt Box

Example code to verify the JavaScript-code gets blocked when an JavaScript Popup Boxes are active.

var i = 1, t;
function sleep(delay) {
	t = setTimeout("flow()",delay);
}
function flow(){
	switch(i){ 
		case 0: clearTimeout(t); break;
		default : console.log('Running : '+ (i++) );

		sleep(1000 * 2);
		if(i === 10) { i = -1; }
	}
}

flow();

Note « By using Selenium framework we can handle these Popup Boxes.

Browser dialog window for Authentication using Tomcat.

Selenium Headers for authenticating and tomcat example

driver.get("http://username:[email protected]/")