Sunday, March 4, 2018

Automating Notepad with AutoIt

AutoIt - Introduction - Get Auto It, Install, Components

AutoIT tutorial 2 Run from SciTe Editor


AutoIT tutorial 3 Open notepad


AutoIT tutorial 4 Wait For window to open


AutoIT tutorial 5 type a text


AutoIT tutorial 6 Save Notepad file using File Save Menu


AutoIT tutorial 7 focus file, type name, click on save button


AutoIT tutorial 8 Close Window


Full Source Code: for Notepad Automation


      Run("notepad.exe")
      WinWaitActive("Untitled - Notepad")
      Send("This is sample text")
      WinMenuSelectItem("Untitled - Notepad","","&File","&Save")
      WinWaitActive("Save As")
      ControlFocus("Save As", "", "Edit1")
      ControlSend("Save As","","Edit1","demo3.txt")
      ControlClick("Save As","&Save","Button1")
      WinWaitActive("demo3.txt - Notepad")
      WinClose("demo3.txt - Notepad")

AutoIT tutorial 9 File Upload Dialog Automation



AutoIT tutorial 10 Integrate AutoIt Automation With WebDriver Automation


AutoIT tutorial 11 Parameterize AutoIt Script, Pass file path as argument


Full AutoIt Source Code: Upload Dialog Automation

      WinActivate("Open")
      WinWaitActive("Open")
      ControlSend("Open","","Edit1",$CmdLine[1]);
      ControlClick("Open","&Open","Button1")

WebDriver & AutoIt integration code


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

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * Upload file using Using AutIT
 * 
 * @author vikas
 *
 */
public class Example4 {

@Test
public void testCase1() {

System.setProperty("webdriver.chrome.driver", "e:\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://smallpdf.com/word-to-pdf");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement fileUploadElement = driver.findElement(By.className("omnibox-link"));
fileUploadElement.click();
// open the upload file / open file dialog
String filePath = "C:\\Users\\vikas\\Desktop\\input.docx";
ProcessBuilder pb = new ProcessBuilder("C:\\Users\\vikas\\Desktop\\S15\\uploadfile.exe", filePath);
try {
pb.start();
Thread.sleep(10000);
WebElement downlodFileElement = driver.findElement(By.xpath("//*[text()='Download File']"));
boolean isDownloadFileOptionDisplayed = downlodFileElement.isDisplayed();
Assert.assertTrue(isDownloadFileOptionDisplayed, "Download file link was not shown");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Saturday, March 3, 2018

Upload File using Robot Class



import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;

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;
import org.testng.annotations.Test;

public class UploadFileTest {

@Test
public void testFileUpload() {
System.setProperty("webdriver.chrome.driver", "e:\\drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://smallpdf.com/word-to-pdf");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebElement btnUpload = driver.findElement(By.className("omnibox-link"));
Actions a = new Actions(driver);
btnUpload.click(); // open file upload dialog
// E:\input.docx
try {
Thread.sleep(3000);
Robot robot = new Robot();
typeKeys( "e:\\input.docx",robot);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public static void typeKeys(String str, Robot r) {
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)==':'){
typeCharacter(r, "SEMICOLON",true);
}
else if(str.charAt(i)=='\\'){
typeCharacter(r, "BACK_SLASH",false);
//KeyEvent.VK_BACK_SLASH
}
else if(str.charAt(i)=='.'){
typeCharacter(r, "PERIOD",false);
//KeyEvent.VK_PERIOD
}
else{
char ch =  str.charAt(i);
if(Character.isUpperCase(ch)){
typeCharacter(r,""+ch,true );
}
else{
typeCharacter(r,""+ch,false );
}

}

}
r.keyPress(KeyEvent.VK_ENTER);
}

public static void typeCharacter(Robot robot, String letter, boolean needShift) {
try {


String variableName = "VK_" + letter.toUpperCase();
Class clazz = KeyEvent.class;
Field field = clazz.getField(variableName);
int keyCode = field.getInt(null);

robot.delay(1000);

if (needShift)
robot.keyPress(KeyEvent.VK_SHIFT);

robot.keyPress(keyCode);
robot.keyRelease(keyCode);

if ( needShift)
robot.keyRelease(KeyEvent.VK_SHIFT);
} catch (Exception e) {
System.out.println(e);
}
}

}