In this article we will learn about open source tool AutoIT which is used to automate window bases application.
1). Download and install AutoIT
2). Different Components in AutoIT
3). Basic commonds in AutoIT
4). Reading window based popup properties using AutoIT
5). Seenium Integration with AutoIT
6). html script for choose me file
open the google.com and type Download AutoIT
click on link https://www.autoitscript.com/site/autoit/downloads/
now go to download section as below
zip file will be downloaded, please unzip it and click on application (.exe) file and install as simple software installation
Accept the Licence Agreement
Select as per your operating system
Select edit the script option so that we can edit the script
Keep it as a default setting and click on next
Select the destination folder
Install the software
click the finish button
w indo click on window start button and see the AutoIT and its components are installed as below
Three Main AutoIT components are
1). SciTE Script Editor
like as other editor ike eclipse, Intellij Idea, this is editor for AutoIT commonds
2). AutoIT Window Info
By this we can get the info like "Title", "body", "Id" etc of target window, which we can use in our script
once scripting completed we will save the file with extention .au3
3). Run Script
Now by this runner we will open our saved file and run them
Basic Commonds in AutoIT
All The commonds are very easy and avaiable at autoit website https://www.autoitscript.com/autoit3/docs/functions.htmBut here is few important commonds which we mostly use in our selenium Automation
1)Run : Runs an external Program
Syntax :
Run("program", "workingdir"(optional), "show_flag"(optional), "opt_flag"(Optional))
For example : Open a notpage
Run(notepad.exe)
For Example: Open a notepad with window maximized
Run("notepad.exe", "", @SW_SHOWMAXIMIZED)
2)WinWait : Pauses execution of the script until the requested window exists.
Syntax :
WinWait("title", "text"(Optional), "timeout"(optional))
For Example : Open a notepad and wait for 5 seconds
Run("notepad.exe")
WinWait("[CLASS:Notepad]", "", 5)
3)WinWaitActive : Pauses execution of the script until the requested window is active.
Syntax :
WinWaitActive("title", "text"(Optional), "timeout"(optional))
For Example : Open a notepad and wait for 5 seconds
Run("notepad.exe")
WinWaitActive("[CLASS:Notepad]", "", 5)
Tips : The window is polled every 250 milliseconds or so.
4) Sleep : Pause script execution
Syntax :
Sleep(Amount of time to pause(in millisecond))
Example : Sleep(3000)
5) Send : Sends simulated keystrokes to the active window.
Syntax :
Send ("keys", flag(optional))
Example 1:
press window + r
type notepad.exe and press Enter key
Send("#r")
Send("notepad.exe{ENTER}")
Example 2:
Send raw text
Send("welcome at www.way2testing.com")
Example 3 :
Press Enter
Send("{ENTER}")
Example 4:
Press Escape key
Send{ESCAPE} or Send{ESC}
6) WinClose : Closes a window
Syntax:
WinClose("title", "text"(Optional))
or Example :
Open a Notepad
Enter some text and close it
Run("notepad.exe")
WinWaitActive("Untitled - Notepad")
Send("welcome at www.way2testing.com")
WinClose("*Untitled - Notepad")
7) ControlFocus : Sets input focus to a given control on a window.
Syntax :
ControlFocus("title", "text(text of window to access)", controlID)
8) ControlSetText : Sets text of a control.
Syntax :
ControlSetText("title", "text(text of window to access)", controlID, "new Text( The new text to be set into the control)", flag(optional))
9)ControlClick : Sends a mouse click command to a given control.
Syntax :
ControlClick("title", "text(text of window to access)", controlID, button(optional), clicks(optional), x(optional) , y(optional))
first read the edit text part of the window
now read the button attribute
Now AutoIT script will be like this
Now right click on saved .au3 file and choose option "compile script(64bit)"
Runtime.getRuntime().exec("complete path of above compiled file upto exe");
Scenarios : Suppose we have scenario that we need to
1) : Open the browser
2) : Open the www.way2testing.com practice page
3): click the button "Choose File" , window based popup will open
4) : now enter the complete path of file in input box of popup
5) : click on Open button
import java.io.IOException;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Capturepopup {
WebDriver driver;
@BeforeTest
public void launch() throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "./Driver\\chromedriver.exe");
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
System.setProperty("webdriver.chrome.silentOutput", "true");
ChromeOptions options = new ChromeOptions();
// options.addExtensions(new File("/path/to/extension.crx"));
options.addArguments("--incognito");
options.addArguments("incognito");
options.addArguments("--remote-allow-origins=*");
// WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(options);
// driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
@Test
public void TestAutoIt() throws InterruptedException, IOException {
driver.navigate().to("https://www.way2testing.com/p/this-webpage-is-designed-for-selenium.html");
Thread.sleep(2000);
WebElement element = driver.findElement(By.xpath("//input[@type = 'file']"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView();", element);
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(element));
Actions act = new Actions(driver);
act.moveToElement(element).click().build().perform();
Thread.sleep(2000);
Runtime.getRuntime().exec("D:\\Software\\AutoIt\\Programs\\way2testingWindowPopup.exe");
}
}
Below is html script for demo of AUTOIT
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Choose File Example </head>
<body>
<h2 style = "color:blue">Welcome at www.way2testing.com</h2>
<h2>Choose File Example</h2>
<!-- Button to trigger file input -->
<button onclick="chooseFile()">Choose File</button>
<!-- Hidden file input -->
<input type="file" id="fileInput" style="display: none;">
<script>
// Function to trigger file input when button is clicked
function chooseFile() {
document.getElementById('fileInput').click();
}
// Function to handle file selection
document.getElementById('fileInput').addEventListener('change', function() {
var file = this.files[0];
if (file) {
console.log('Selected file:', file);
// You can perform further operations with the selected file here
} else {
console.log('No file selected');
}
}); </script>
</body>
</html>
No comments:
Post a Comment