How to handle Authentication Required popup using AutoIT?

While browsing some websites, Authentication required popup comes asking username and password. Observe below snap.

Now, this can be handled using either robot class or by using AutoIT script.
Let us see how this Authentication Required popup is handled using AutoIT script.
Script used to handle this Authentication popup is as below –
WinWaitActive(“Authentication Required”,””,”10″)

 

If WinExists(“Authentication Required”) Then

 

Send(“username{TAB}”)

 

Send(“Password{Enter}”)

 

EndIf
Add this script in SciTE editor. Save the file with some name by .au3 extension.
Now compile the script by right clicking on it, with respective version.
Once compilation is successful, .exe file will be created with same name.
Now, we can use this .exe file & run this via our selenium Webdriver script.
                        Runtime.getRuntime().exec(“C:/Users/Prakash/Desktop/TestScript.exe”);
That’s, all, this is how we can handle Authentication Required Popup using AutoIT

How to upload file in selenium using AutoIT

In earlier articles, we saw that, we can upload the file using sendKeys() method or by using robot class.
Refer below articles for this.

Upload file in selenium using sendkeys() method.

Upload file in selenium using robot class

Now, in case both of the above methods won’t work, then we have to use AutoIT tool (which a third-party tool used to handle window popups). Let’s talk how this can be done.
AutoIt v3 is a freeware BASIC-like scripting language designed for automating Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks.
This method is for handling the Windows File Upload dialog, which cannot be handled using Selenium. Please follow below steps:
         
           1.       Download AutoIT setup file from here (https://www.autoitscript.com/site/autoit/downloads/)
      
           2.       Open Au3Info.exe for which helps to find window information details.
           
          3.       Open SciTE Script Editor.
            Script used to upload the file is as below. Write the script below in script editor.
               ControlFocus(“File Upload”,””,”Edit1″)

               ControlSetText(“File Upload”,””,”Edit1″,”C:UsersPrakashDesktopTOSCA tutorial.pptx”)

               ControlClick(“File Upload”,””,”Button1″)
              We can get Title and control id from application Au3Info.exe. Just Drag and drop the finder tool at required position. 

              In this script Control Focus is used to focus on the field where we enter the file to be uploaded path details. Note that Control id is the combination of Class and Instance.
     
        Syntax –  ControlFocus(“title”,”text”,”Control id”)
                                                       
                          ControlSetText is used to write the text (file path of the file to be uploaded)
                          Syntax – ControlSetText(“title”,”text”,”Control id”,”file path”)
 
                          ControlClick is used to click on the button.
                           Syntax – ControlClick(“title”,”text”,”Control id”)
 
              2.       Save the Script with some name with .au3 extension. 
 
              3.       Then right click on it, click on Compile.
               4.       Then Double click on the same file.
              5.       Now you will see the .exe file with same name is generated.
                   We are going to use this .exe file in our java – selenium program (to upload the file using AutoIT).
        
             6.       Use below piece of code for executing the .exe file created above
                 
               Runtime.getRuntime().exec(“C:UsersPrakashDesktopTestScript.exe”);
          
          Refer below example –

package basicsOfSelenium;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AutoIT {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub

WebDriver driver = new FirefoxDriver();
driver.navigate().to("http://www.tinyupload.com/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();

// Click on Browse Button
driver.findElement(By.xpath("html/body/table/tbody/tr[4]/td/table/tbody/tr/td[2]/form/table/tbody/tr[2]/td[1]/input[2]")).click();

//After pop-up open, below script is used to upload the file
Runtime.getRuntime().exec("C:UsersPrakashDesktopTestScript.exe");

}
  
Hope this helps !!!!

How to upload file in Selenium WebDriver using Robot class

Upload file using Robot class –

Suggested post to read –  How to upload file in Selenium WebDriver using sendKeys() method

In this article, we will see how can we upload file in Selenium WebDriver using robot class.

Upload file in Selenium WebDriver using ROBOT Class

This is another way using which we can upload file. Here we can do key press and key release using robot class.

This method is for handling the Windows File Upload dialog, which cannot be handled using Selenium. Please follow below steps:

1.   Click on the File Upload / Choose File button, so that the File Upload dialog is displayed.
        driver.findElement(By.xpath(“//*[@id=’uploadfile_0′]”)).click();

2. Copy your file’s absolute path to the clipboard
    StringSelection ss = new StringSelection(” C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx”);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

3. Paste the file’s absolute path into the File name field of the File Upload dialog box

    //native key strokes for CTRL, V and ENTER keys

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

Refer below code –

package basicsOfSelenium;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;


public class FileUploadInSelenium_UsingRobotClass {

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

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://demo.guru99.com/selenium/upload/");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  //open upload window
     driver.findElement(By.xpath("//*[@id='uploadfile_0']")).click();

     //put path to your image in a clipboard
     StringSelection ss = new StringSelection("C://Users//Prakash//Desktop//Desktop//TOSCA tutorial.pptx");
     Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

     //imitate mouse events like ENTER, CTRL+C, CTRL+V
     Robot robot = new Robot();
     robot.delay(250);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.keyRelease(KeyEvent.VK_ENTER);
     robot.keyPress(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_V);
     robot.keyRelease(KeyEvent.VK_CONTROL);
     robot.keyPress(KeyEvent.VK_ENTER);
     robot.delay(90);
     robot.keyRelease(KeyEvent.VK_ENTER);

  
 }

}

Refer below articles to learn more about Actions class in Selenium WebDriver –

Hope this helps !!

How to upload file in Selenium WebDriver using sendKeys() method

Let us talk about how to upload file in Selenium WebDriver. We can upload the file in selenium WebDriver using various methods. By using sendKeys() method is one of the simplest one.
Upload file in Selenium WebDriver using sendKeys
This is the simplest way to upload the file in selenium Webdriver. Here we will use sendKeys() method to send the absolute file path to be uploaded. But this can be achieved only when the type attribute for filepath textbox / browse button is “file”. Observe below screenshot for this.
As in above screenshot, since type = file, we can use sendKeys() method to send the file path & then click on upload / submit file button to complete upload process.
First make sure that the input element is visible
Don’t click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.
Instead you can use:
driver.findElement(By.xpath(“//*[@id=’uploadfile_0′]”)).sendKeys(“C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx”);
//*[@id=’uploadfile_0′] is the xpath of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you.
Keep in mind that the upload file will work only If the element you send a file should be in the form    <input type=”file”>
Below is the sample example of how to upload file –

package basicsOfSelenium;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FileUploadInSelenium1 {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to("http://demo.guru99.com/selenium/upload/");
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.manage().window().maximize();
  
  //send the file path to be uploaded to the element, note - this will work only if attribute type = file
  driver.findElement(By.xpath("//*[@id='uploadfile_0']")).sendKeys("C:UsersPrakashDesktopDesktopTOSCA tutorial.pptx");
  
  //click on accept term checkbox
  driver.findElement(By.xpath("//*[@id='terms']")).click();
  //click on submit button - to complete file upload
  driver.findElement(By.xpath("//*[@id='submitbutton']")).click();

  
 }

}
Well, this was all about upload file in Selenium webdriver by using sendKeys() method. There is another way to do it is by using AutoIT. Refer below link to know more about AutoIT

AutoIT Tutorials – Upload and download files in Selenium WebDriver

Hope this helps !!!