Thursday, June 27, 2024

Handling Window-Based Popups:

 

Handling Window-Based Popups:

Selenium can be used to automate web applications but not Windows-based applications directly. However, there are several ways to handle Windows-based popups, such as file upload dialogs.

 

ex:   Fileupload ---  resume

 

3 ways   to upload file (or) handle windows based popups :

 

1. Using Robot() class

2. AutoIT - 3rd party s/w

3. sendkeys()

 

1. Upload File using Robot Class :

The Robot class in Java is a pre-defined class that can be used to perform keyboard and mouse operations. This includes pressing and releasing keys, which can be useful for handling file upload dialogs.

 

 

package UploadfileBasics;

 

import java.awt.AWTException;

import java.awt.Robot;

import java.awt.Toolkit;

import java.awt.datatransfer.StringSelection;

import java.awt.event.KeyEvent;

import java.time.Duration;

 

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 UploadFileUsingRobotClass {

 

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

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();               

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

                        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

 

 

                        // click   'choose file' btn                   

//                     driver.findElement(By.xpath("//input[@id='fileid']")).click();//  not working                      

 

                        // org.openqa.selenium.chromium.ChromiumDriver lambda$new$3

                        //                     WARNING: Unable to find version of CDP to use for . You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.4.0` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.

                        //                     Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument

 

 

                        //  click on 'choose file' btn using 'actions' class

                        Actions act  = new Actions(driver);

                        WebElement chooseFileBtnEle = driver.findElement(By.xpath("//input[@id='fileid']"));

                        act.click(chooseFileBtnEle).perform();

                                   

 

                        Thread.sleep(4000);

 

                        // 'Robot' is pre-defined class in Java

                        // can be used to perform keyboard events/operations

                        //  press ctrl , enter ,  ctrl  +  v

                        //  Release ctrl , enter, ctrl +  v

 

                        //create obj for robot -class

                        Robot r = new Robot();

                        // pkg - import java.awt

                        // throws AWT exception

 

 

                        // upload some file path - copy file name

                        // Clipboard copy

                        // clip board- is tmp place, where it copies the data

 

                        // C:\brahma\Sample.txt

                        StringSelection ss  =  new StringSelection("C:\\brahma\\Sample.txt");

                        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

 

                        // Press ctrl  +  v

                        r.keyPress(KeyEvent.VK_CONTROL);

                        r.keyPress(KeyEvent.VK_V);

 

                        // release -V and ctrl

                        r.keyRelease(KeyEvent.VK_V);

                        r.keyRelease(KeyEvent.VK_CONTROL);

 

 

                        // wait for 5 sec

                        Thread.sleep(5000);

 

                        // press "enter"           button in keyboard   

                        r.keyPress(KeyEvent.VK_ENTER);

 

                        // release 'enter' btn from k/b

                        r.keyRelease(KeyEvent.VK_ENTER);

 

                        System.out.println("ends here");

 

            }

 

}

 

 

HW  Develop a Re-usable Method -to upload the given file ?

UploadFileUsingRobotClass(String filename)

{

  .....code

}

 

Method call :

---------------

String filePath= "C:\\brahma\\Practise\\qtp practise\\web apps\\debug.txt";

                        UploadFileUsingRobotClass(filePath);

-------------------

 

HW Upload any excel file from ur system  by calling resuable method -UploadFileUsingRobotClass(filePath);-?

 

2.AutoIt:

What is AutoIt?

AutoIt is a third-party software that can be used to handle and automate Windows-based elements, components, and controls.

It is especially useful when Selenium alone cannot automate these elements.

Disadvantages of Selenium

  • Selenium cannot automate Windows-based elements, components, or controls directly.

Downloading AutoIt

  1. Go to AutoIt Downloads.
  2. Click the Download AutoIT button. The autoit-v3-setup.exe file will be downloaded to your system.
  3. Unzip the file and double-click the autoit-v3-setup.exe file.
  4. Follow the setup wizard by clicking Next and Next.

Verify AutoIt Installation

  • Check if AutoIt is installed in the following path:

            C:\Program Files (x86)\AutoIt3

 

 

Editor  to write code in autoIT :

·  Navigate to:

plaintext

Copy code

C:\Program Files (x86)\AutoIt3\SciTE\SciTe.exe

·  Open SciTE by double-clicking SciTe.exe.

·  This will open the editor where you can write commands and call methods.

 

 

;To open notepad file

;This is comment . In autoIt,Commnets starts with ; semi colon

Run("notepad.exe")

 

Sleep(4000)

; To send data - This is Ram

Send("This is Ram")

 

 

;Send data - 'How are you?'

Send("'How are you?'")

 

;Send data - "I am fine"

 

Sleep(4000)

; close note pad window

;winclose("title", "[text"])

WinClose("*Untitled - Notepad")

 

Sleep(4000)

 

;to click Don't save btn-

; ControlClick("title","text", "ctrlId")

;   ctrlID =   class + Instance  =   Button2

ControlClick("Notepad","","Button2")

 

Sleep(2000)

;ControlClick("Notepad","",Button2) not  working as ctril id must be enclosed in dbl quotes

 

 

 

Inspect "Windows" Element :

 

C:\Program Files (x86)\AutoIt3

dbl  click Au3Info.exe >  it opens new window with name 'AutoIt v3 Window info'

 

inspect notepad window :

 

Click finder tool icon > drag and drop to the notepad

>   displays some properties of notepad

Title:

Class:

Instance:2

 

Save >C:\brahma\Practise\seleniumpractise\AutoITFiles\OctAutoITFiles

 

File name =   NotepadBasics.au3

 

AutoIT script file extension - .au3

 

Compile  :

 

Go SciTE Editor > Tools > Compile/Build >  it creates new .exe file with same file name i.e NotepadBasics.exe in same location

 

// Note:  if 'compile' fails,  Click Build

Run autoit code:

Dbl click on NotepadBasics.exe >  it runs auto it code

 

Run .exe file from eclipse:

           

package UploadfileBasics;

 

import java.io.IOException;

 

public class RunAutoItcodeNotepad {

 

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

                        // TODO Auto-generated method stub

                        // dbl clck .exe from C:\brahma\Practise\seleniumpractise\AutoITFiles\May62024\BasicNotePadCode.exe

 

                       

                        // To run autoIt.exe file

            //C:\brahma\Practise\seleniumpractise\AutoITFiles\May62024\BasicNotePadCode.exe

                       

            Runtime.getRuntime().exec("C:\\brahma\\Practise\\seleniumpractise\\AutoITFiles\\May62024\\BasicNotePadCode.exe");

 

            //C:\\brahma\\Practise\\seleniumpractise\\AutoITFiles\\Jun2023AutoItFiles\\BasicNotePad.exe

                        // throws IOException

 

 

 

 

 

            }

 

}

 

 

           

                       

---------------------------

// HW write AutoIT code for below steps  and Run from Eclipse

                        // open notepad

                        // send some data

                        // close notepad window

                        // Click 'Save' button

 

 

Upload file using "AutoIT":

;  wait for open window and make it active

;WinWaitActive("title","",3000)

WinWaitActive("Open","",3000)

 

 

 

 

            ;           // Enter File name  i.e C:\brahma\Practise\qtp practise\web apps\debug.txt

 

                        ; ControlSetText("title", "text ", "ctrlId", " Value to be entered")

                        ;  ctrlid = class +Instance  = Edit1

              ControlSetText("Open","","Edit1","C:\brahma\Practise\qtp practise\web apps\debug.txt")

 

 

                        ;Sleep for 3 sec

Sleep(3000)

 

 

 

; click open Button

;ControlClick("title","text","ctrlid")

;ctrlid = class + Instance no=Button1

ControlClick("Open", "", "Button1")

 

 

compile :

 

                        // click   choose file btn using actions

                       

                       

                        // Run autoIt code

//                     C:\brahma\Practise\seleniumpractise\AutoITFiles\Jun2023AutoItFiles

                       

code:

 

package UploadfileBasics;

 

import java.io.IOException;

import java.time.Duration;

 

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 UploadFileUsingAutoIt {

 

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

//                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();               

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

                        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

 

 

                        // click   'choose file' btn                   

//                     driver.findElement(By.xpath("//input[@id='fileid']")).click();//  not working                      

 

                        // org.openqa.selenium.chromium.ChromiumDriver lambda$new$3

                        //                     WARNING: Unable to find version of CDP to use for . You may need to include a dependency on a specific version of the CDP using something similar to `org.seleniumhq.selenium:selenium-devtools-v86:4.4.0` where the version ("v86") matches the version of the chromium-based browser you're using and the version number of the artifact is the same as Selenium's.

                        //                     Exception in thread "main" org.openqa.selenium.InvalidArgumentException: invalid argument

 

                        System.out.println("starts execution");

                        //  click on 'choose file' btn using 'actions' class

                        Actions act  = new Actions(driver);

                        WebElement chooseFileBtnEle = driver.findElement(By.xpath("//input[@id='fileid']"));

                        act.click(chooseFileBtnEle).perform();

 

                        Thread.sleep(4000);

 

                        // Run UploadFile.exe

            Runtime.getRuntime().exec("C:\\brahma\\Practise\\seleniumpractise\\AutoITFiles\\May62024\\UploadFileCode.exe");

                       

                        System.out.println("ends here");

 

 

 

 

            }

 

}

 

//  not workig  ?????

 

FAQ What is the difference between AutoIT and Robot ?

 

AutoIT supports the following operating systems: only Windows

            Windows XP and Windows Server 2003

            Windows Vista and Windows Server 2008/2008 R2             

            Windows 7 / 8 / 10 / 11

 

AutoIT  s/w is Not available for Linux or macOS operating systems.

It can be used to automate windows based components (windows textbox,  Windows button , windows combobox)

 

Robot - is predefined class in Java

It can be used to handle keyboard events / operations

Robot class code will be run in  windows , Linux, Mac o/s

Robot class  code -- will run in all o/s's.

 

Usualy we prefer Robot class to upload file as We can run the same code in windows, Linux and MAc o/s

 

Feature / Aspect

AutoIt

Robot Class in Java

 

Supported Operating Systems

Only Windows (XP, Vista, 7/8/10/11, Server editions)

Windows, Linux, macOS

 

Automation Capabilities

Handles Windows-based GUI components (textboxes, buttons, etc.)

Handles keyboard and mouse events universally

 

Scripting Language

AutoIt scripting language specific to Windows automation

Java programming language

 

File Upload Handling

Ideal for Windows-specific file upload dialogs

Platform-independent; suitable for cross-OS file uploads

 

Cross-Platform Support

Not available for Linux or macOS

Runs on Windows, Linux, and macOS

 

Community Support

Active community and support for Windows automation

Integrated with Java community support

 

upload file using Sendkeys():

 

To upload a file using sendKeys() method in Selenium, you typically interact with an <input type="file"> element.

text box  =  Path of  file , click upload button

<input type="file" id="fileid" name="filename"> --> use sendKeys("file path");

 

 

code:

package UploadfileBasics;

 

import java.time.Duration;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class UploadFileUsingSendKeys {

 

            public static void main(String[] args) {

                        System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");

 

                        //                     //open chrome browser

                        WebDriver  driver =  new ChromeDriver();               

                        driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");

                        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

 

                        // Send file name to 'Upload file ' Text box -C:\brahma\Practise\qtp practise\web apps\debug.txt

                 driver.findElement(By.name("uploadname")).sendKeys("C:\\brahma\\Practise\\qtp practise\\web apps\\debug.txt");

 

                        // if ele  type = "file"  , we can use sendkeys()

                        driver.findElement(By.id("fileid")).sendKeys("C:\\brahma\\Practise\\qtp practise\\web apps\\debug.txt");

 

                        // In My Project, I got the same element

                        // Send file name to 'Upload file ' Text box -C:\brahma\Practise\qtp practise\web apps\debug.txt

                       

                        // here we dont need to click 'choose file button', no need to enter file name , click Open button

 

 

                        System.out.println("ends here");

 

 

            }

 

}

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...