AutoIT tool and two ways to handle windows Authentication

What is AutoIt tool?
AutoIt V3 is a freeware tool which is used for automating anything in Windows environment. AutoIt script is written in a BASIC language. It can simulate any combination of keystrokes, mouse movement and window/control manipulation.

Selenium Usage
  • Handle window GUI and non HTML popups
  • Upload files
Download AutoIT and AutoIT editor
https://www.autoitscript.com/site/autoit/downloads/

Handle Windows Authentication using AutoIT tool.

import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HandleWindwowAuthentication {

 public static void main(String[] args) throws IOException, InterruptedException {
  
  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("http://the-internet.herokuapp.com/basic_auth");
  Thread.sleep(2000);
  Runtime.getRuntime().exec("C:\\Users\\Hitendra\\Desktop\\Authen.exe");
  Thread.sleep(2000);
        //driver.close();
 }
}

2nd way to Handle Windows Authentication:

import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandleWindwowAuthentication1 {

 public static void main(String[] args) throws IOException, InterruptedException {
  
  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth");
        //driver.close();

 }

}

Please refer below youtube video to get more details:


Alert Interface and Handle JavaScript Alerts in Selenium

What is an Alert?
  • Alert is a small message box which displays on-screen notification to give the user some kind of information or ask for permission to perform certain kind of operation. It may be also used for warning purpose.
  • There are many user actions that can result in an alert on screen. For e.g. user clicked on a button that displayed a message or may be when you entered a form, HTML page asked you for some extra information. So in this video we will learn Handling of Alerts, JavaScript Alerts and PopUp Boxes.
There are two types of alerts that we would be focusing on majorly:
  • Web-based alert pop-ups
  • Windows-based alert pop-ups
Handling windows based pop-ups is beyond Web Driver's capabilities, thus we would exercise some third-party utilities to handle window pop-ups.

Types of Web Based/Java Script Alerts
Java scrip provides mainly following three types of alerts:

1. Simple alert("This is a simple alert");
2. confirmation alert- confirm("Confirm pop up with OK and Cancel button");
3. prompt- prompt("Do you like ATI?", "Yes/No");

There are the four methods that we would be using along with the Alert interface.
  • void dismiss() – The dismiss() method clicks on the “Cancel” button as soon as the pop up window appears.
  • void accept() – The accept() method clicks on the “Ok” button as soon as the pop up window appears.
  • String getText() – The getText() method returns the text displayed on the alert box.
  • void sendKeys(String stringToSend) – The sendKeys() method enters the specified string pattern into the alert box.
Simple Alert Program:
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class SimpleAlertDemo {

 public static void main(String[] args) throws InterruptedException {
  
  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.automationtestinginsider.com/2019/08/textarea-textarea-element-defines-multi.html");
  Thread.sleep(2000);
  driver.findElement(By.id("simpleAlert")).click();
  Alert alert=driver.switchTo().alert();
  String alertText=alert.getText();
  System.out.println("Alert Text is: "+alertText);
  Thread.sleep(2000);
  alert.accept();
  driver.close();
 }
}

Output:
Alert Text is: Simple Alert

This is a Simple Alert

Confirmation Alert Demo:
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ConfirmationAlertDemo {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.automationtestinginsider.com/2019/08/textarea-textarea-element-defines-multi.html");
  // Thread.sleep(2000);

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

  if (isAlertPresent(driver)) {
   System.out.println("Alert is present");
  } else {
   System.out.println("Alert is not present");
  }
  driver.close();
 }

 public static boolean isAlertPresent(WebDriver ldriver) {

  try {
   Alert alert = ldriver.switchTo().alert();
   String alerteTxt = alert.getText();
   alert.accept();
   //alert.dismiss();
  } catch (NoAlertPresentException e) {
   // TODO Auto-generated catch block
  }
  return false;
 }
}

Output:
Alert is present

Prompt Alert Demo:
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PromptAlertDemo {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;
  System.setProperty("webdriver.gecko.driver",
    "C:\\Users\\Hitendra\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("https://www.automationtestinginsider.com/2019/08/textarea-textarea-element-defines-multi.html");
  Thread.sleep(3000);
  driver.findElement(By.id("promptAlert")).click();
  Alert alert = driver.switchTo().alert();
  Thread.sleep(2000);
  String alertText = alert.getText();
  System.out.println("Alert Text is: " + alertText);
  alert.sendKeys("Yes");
  Thread.sleep(2000);
  alert.accept();
  driver.close();
 }
}

Output:

Alert Text is: Do you like ATI?

Please refer below youtube video to get more details:



Mouse hover Auto Suggestion in Selenium

Mouse Hover Actions in Selenium Webdriver. 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.

Please refer below Program to understand how to perform mouse hover:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class MousehoverDemo {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://s1.demo.opensourcecms.com/wordpress/wp-login.php");
  Thread.sleep(1000);

  Actions act = new Actions(driver);

  driver.findElement(By.id("user_login")).sendKeys("opensourcecms");
  driver.findElement(By.id("user_pass")).sendKeys("opensourcecms");
  driver.findElement(By.id("wp-submit")).click();
  Thread.sleep(1000);

  WebElement logoutOption = driver.findElement(By.xpath("//a[text()='Howdy, ']"));

  act.moveToElement(logoutOption).perform();

  driver.findElement(By.xpath("//a[@class='ab-item'][text()='Log Out']")).click();

  Thread.sleep(2000);
  driver.close();

 }

}

Please refer below Program to understand how to handle Auto Suggestions in Selenium:
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class Autosuggestion {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;
  System.setProperty("webdriver.chrome.driver",
    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");
  driver = new ChromeDriver();
  driver.manage().window().maximize();
  driver.get("https://www.google.com/");
  Thread.sleep(2000);

  driver.findElement(By.name("q")).sendKeys("Selenium");
  Thread.sleep(3000);

  List<WebElement> list = driver.findElements(By.xpath("//ul/li[@class='sbct']"));

  for(int i = 0 ;i< list.size();i++)
  {
   System.out.println(list.get(i).getText());
   
   String searchText=list.get(i).getText();
   
   if(searchText.equals("selenium testing"))
   {
    list.get(i).click();
    break;
   }
  }

  driver.close();
 }

}

Please refer below youtube video to understand How to handle Mouse hover and Auto Suggestion.



Multi Select Actions in Selenium

Please refer below Program to understand how to handle Multi Select Actions in Selenium:

import java.util.List;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Action;

import org.openqa.selenium.interactions.Actions;

public class MultiSelectActions {

 public static void main(String[] args) throws InterruptedException {

 
  WebDriver driver;

  System.setProperty("webdriver.chrome.driver",

    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

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

  driver.get("https://www.seleniumeasy.com/test/jquery-dual-list-box-demo.html");

  Thread.sleep(1000); 

  Actions act = new Actions(driver);
 

  List<WebElement>list=driver.findElements(By.xpath

    ("//select[@class='form-control pickListSelect pickData']/option"));

  Action actions=act.keyDown(Keys.CONTROL)

  .click(list.get(0))

  .click(list.get(2))

  .click(list.get(4))

  .click(list.get(5))

  .keyUp(Keys.CONTROL)

  .build(); 

  actions.perform(); 

  Thread.sleep(2000); 

  driver.findElement(By.xpath("//div[@class='col-sm-2 pickListButtons']/button[1]"))

  .click(); 

  Thread.sleep(2000);

  driver.close();   

 }
}

Please refer below youtube video to understand How to handle Multi Select Actions:



How to handle tooltip in Selenium?

In many web pages, it’s very common that when hovered over some link, text, some text or sometimes image gets displayed. For example, in Gmail inbox, if hovered over right-hand side menu options, small “hover box”  with text gets displayed. This is called a tooltip of web element.

This text usually is a brief description of the functionality of the object or in some cases, it displays a detailed description of the object. In some cases, it may display only the full name of the object. So basically, the purpose of a tooltip is to provide some hint to the user about the object. In many instances, it is required to verify if this text description being displayed as expected.

Please refer below Program to understand how to handle tooltip in Selenium:

Program1 (without actions class when title attribute present for the element):
import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;


public class ToolTipDemo {

 public static void main(String[] args) throws InterruptedException {

 
  WebDriver driver;

  System.setProperty("webdriver.chrome.driver",

    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

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

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

  Thread.sleep(1000); 

  WebElement searchBox=driver.findElement(By.xpath("//input[@class='gsc-input']"));

 
  String tooltipText=searchBox.getAttribute("title");
 

  System.out.println("Tooltip text is: "+tooltipText); 

  driver.close();

 }

}

Program2(using Actions Class):

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;


public class ToolTipDemo1 {

 public static void main(String[] args) throws InterruptedException {

 
  WebDriver driver;

  System.setProperty("webdriver.chrome.driver",

    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

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

  driver.get("http://demoqa.com/tooltip-and-double-click/");

  Thread.sleep(1000);
  
  Actions act= new Actions(driver);
 
  WebElement mouseHover=driver.findElement(By.id("tooltipDemo"));
  
  act.moveToElement(mouseHover).perform();
  
  WebElement tooltipMsg=driver.findElement(By.xpath("//span[@class='tooltiptext']"));


  System.out.println("Tooltip Message is: "+tooltipMsg.getText());

 
  driver.close();

 }

}

Please refer below youtube video to understand How to handle tooltip.


How to handle drag and drop in Selenium?

The basic sequence involved in drag and drop is:
Move the pointer to the object.
Press, and hold down, the button on the mouse or other pointing device, to "grab" the object.
"Drag" the object to the desired location by moving the pointer to this one.
"Drop" the object by releasing the button.

Please refer below Program to understand how to perform drag and drop:
import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;


public class DragAndDropDemo {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;

  System.setProperty("webdriver.chrome.driver",

    "C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

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

  driver.get("https://demoqa.com/droppable/");

  Thread.sleep(1000);



  WebElement from = driver.findElement(By.id("draggable"));

  WebElement to = driver.findElement(By.id("droppable"));

  Actions act = new Actions(driver);

  // act.dragAndDrop(from, to).perform();

  // act.dragAndDropBy(from, 133, 22).perform();

  act.clickAndHold(from).moveToElement(to).release().build().perform();


  WebElement droppedMsg = driver.findElement(By.xpath("//div/p[text()='Dropped!']"));

  if (droppedMsg.isDisplayed()) {

   System.out.println("Success");

   System.out.println("Message is: "+droppedMsg.getText());

  } else {

   System.out.println("Failed");

  }

  Thread.sleep(1000);

  // driver.close();

 }

}

Please refer below youtube video to understand How to handle drag and drop.


Perform double click and right click

Double click action in Selenium web driver can be done using Actions class.

Right click operation is mostly used when performing right click on an element opens a new menu. Right click operation in Selenium web driver can be done using the pre-defined command Context Click

Actions action = new Actions(driver);
WebElement link = driver.findElement(By.ID ("Element ID"));
action.contextClick(link).perform();

Double click operation is used when the state of web element changes after double click operation. Double Click operation in Selenium web driver can be done using the pre defined command Double Click as mentioned below

Actions action = new Actions(driver);
WebElement link = driver.findElement(By.ID ("Element ID"));
action. doubleClick (link).perform();

Please refer below Program to understand how to perform double click and right click:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;


public class DoubleAndRightClick {

 public static void main(String[] args) throws InterruptedException {

 
  WebDriver driver;

  System.setProperty("webdriver.chrome.driver","C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

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

  driver.get("https://www.automationtestinginsider.com/2019/08/textarea-textarea-element-defines-multi.html");

  Thread.sleep(1000);  

  WebElement doubleClickBtn= driver.findElement(By.id("doubleClickBtn"));

  WebElement rightClickBtn= driver.findElement(By.id("rightClickBtn"));  

  Actions act= new Actions(driver);  

  act.doubleClick(doubleClickBtn).perform();

  System.out.println("Double clicked");

  Thread.sleep(2000);

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

  Thread.sleep(2000);

  act.contextClick(rightClickBtn).perform();

  Thread.sleep(2000);

  driver.close();

 }

}

Please refer below youtube video to understand How to Perform double click and right click:



Keyboard Operations using Selenium

Keyboard Events Using Selenium Actions Class API: We use below methods:
sendKeys(keysToSend) : sends a series of keystrokes onto the element.
keyDown(theKey) : Sends a key press without release it. Subsequent actions may assume it as pressed. (example: Keys. ALT, Keys. SHIFT, or Keys. CONTROL)
keyUp(theKey): Performs a key release.

Please refer below Program to understand how to perform keyboard operations:

public class KeyboardMouseActions {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver;

  System.setProperty("webdriver.chrome.driver","C:\\Users\\Hitendra\\Downloads\\chromedriver_win32\\chromedriver.exe");

  driver = new ChromeDriver();

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

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

  Thread.sleep(1000);

  WebElement googleSearch=driver.findElement(By.name("q"));

 
  Actions act=new Actions(driver);

 
  Action action=act.keyDown(googleSearch, Keys.SHIFT)

  .sendKeys("selenium")

  .keyUp(googleSearch, Keys.SHIFT)

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("a")

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("x")

  .keyDown(googleSearch, Keys.CONTROL)

  .sendKeys("v")

  .build();

   action.perform();

                 Thread.sleep(3000);

  driver.close();

 }

}

Please refer below youtube video to understand How to handle Keyboard Operations:


Actions Class in Selenium

Handling special keyboard and mouse events are done using the Advanced User Interactions API. It contains the Actions and the Action classes that are needed when executing these events.

Some Examples: Mouse double click, Drag and Drop, Handling tooltip, Mouse hover, entering uppercase letters in the textbook using Shift+Letters .

Actions Class: Actions class is an API for performing complex user web interactions like double-click, right-click, etc. and it is the only choice for emulating Keyboard and Mouse interactions.

Actions class implements the builder pattern. Builder Pattern is one of the design patterns. The intent of the Builder design pattern is to separate the construction of a complex object from its representation.

What is the difference between Actions Class and Action Interface in Selenium?

Actions is a class that is based on a builder design pattern.  This is a user-facing API for emulating complex user gestures.

Whereas Action is an Interface which represents a single user-interaction action. It contains one of the most widely used methods perform().

1. clickAndHold() -- Clicks (without releasing) at the current mouse location.
2. contextClick() - Performs a context-click at the current mouse location. (Right Click Mouse Action)
3. doubleClick() - Performs a double-click at the current mouse location.
4. dragAndDrop(source, target) - Performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.
5. dragAndDropBy(source, x-offset, y-offset) - Performs click-and-hold at the location of the source element, moves by a given offset, then releases the mouse.
6. moveToElement(toElement) - Moves the mouse to the middle of the element.
7. release() - Releases the depressed left mouse button at the current mouse location
8. sendKeys(onElement, charsequence) - Sends a series of keystrokes onto the element.
9. keyDown(modifier_key) - Performs a modifier key press. Does not release the modifier key - subsequent interactions may assume it's kept pressed.
10. keyUp(modifier _key) - Performs a key release.

Selenium has the built-in ability to handle various types of keyboard and mouse events. In order to do action events, you need to use org.openqa.selenium.interactions Actions class. The user-facing API for emulating complex user gestures. Use the selenium actions class rather than using the Keyboard or Mouse directly. This API includes actions such as drag and drop, clicking multiple elements.

To create an object ‘action‘ of Selenium Actions class, we use below command:
Actions action=new Actions(driver);

To focus on element using WebDriver:
action.moveToElement(element).perform();
element is the webelement which we capture

perform() method is used here to execute the action.

To click on the element:
action.moveToElement(element).click().perform();
click() method is used here to click the element.