Skip to main content

Notepad automation

Notepad automation

This example shows how to automate a Windows application using the Desktop driver.

The example consists of several parts:

  1. Opening a Notepad application
  2. Opening the About Notepad RPA window
  3. Scrapping application information
  4. Saving the information received in Notepad
  5. Saving file - interaction with the Save As window

Application class opens Notepad and returns MainPage object.

NotepadApplication.java
package eu.ibagroup.easyrpa.notepad;

import eu.ibagroup.easyrpa.engine.rpa.Application;
import eu.ibagroup.easyrpa.engine.rpa.driver.DesktopDriver;
import eu.ibagroup.easyrpa.engine.rpa.element.DesktopElement;
import eu.ibagroup.easyrpa.notepad.page.MainPage;

public class NotepadApplication extends Application<DesktopDriver, DesktopElement> {

	public NotepadApplication(DesktopDriver driver) {
		super(driver);
	}

	@Override
	public MainPage open(String... args) {
		String appPath = args[0];
		getDriver().get(appPath);
		return createPage(MainPage.class);
	}
}

As you can see, the "About Notepad" window is an internal element of the main application window. The same goes for the "Save As" window, so we do not have to switch between the windows.

 


The MainPage class contains methods for working with the main application window: typing text, opening the "About Notepad" window, saving a document, closing the application.

MainPage.java
package eu.ibagroup.easyrpa.notepad.page;

import eu.ibagroup.easyrpa.engine.annotation.AfterInit;
import eu.ibagroup.easyrpa.engine.rpa.element.DesktopElement;
import eu.ibagroup.easyrpa.engine.rpa.locator.DesktopSearch;
import eu.ibagroup.easyrpa.engine.rpa.page.DesktopPage;
import eu.ibagroup.easyrpa.engine.rpa.po.annotation.FindBy;
import eu.ibagroup.easyrpa.engine.rpa.po.annotation.Wait;
import lombok.SneakyThrows;
import org.apache.commons.io.FilenameUtils;
import org.openqa.selenium.Keys;

import java.io.File;

public class MainPage extends DesktopPage {

	@FindBy(name = "Help")
	@Wait(3)
	private DesktopElement helpMenu;

	@FindBy(name = "About Notepad")
	@Wait(3)
	private DesktopElement aboutNotepadMenuItem;

	private String title;

	public AboutNotepadPage openAboutWindow() {
		helpMenu.click();
		aboutNotepadMenuItem.click();
		return createPage(AboutNotepadPage.class);
	}

	@AfterInit
	public void init() {
		title = "Untitled - Notepad";
		this.switchToMainWindow();
	}

	public void switchToMainWindow() {
		getDriver().waitAndSwitchToWindow(title, 5);
	}

	public void printText(String text) {
		getDriver().findElement(DesktopSearch.query(DesktopSearch.UIQuery.builder().name("Text Editor").build())).sendKeys(text);
	}

	public void saveAs(File file) {
		getDriver().findElement(DesktopSearch.query(DesktopSearch.UIQuery.builder().name("Text Editor").build())).click();
		getDriver().getInputDevices().getKeyboard().pressKey(Keys.LEFT_CONTROL);
		getDriver().getInputDevices().getKeyboard().sendKeys("s");
		getDriver().getInputDevices().getKeyboard().releaseKey(Keys.LEFT_CONTROL);
		getDriver().waitAndSwitchToWindow("#32770", "Save As", 5);
		DesktopElement textBox = getDriver().findElement(DesktopSearch.query(DesktopSearch.UIQuery.builder().className("Edit").name("File name:").build()));
		textBox.clear();
		textBox.sendKeys(file.getAbsolutePath());
		getDriver().findElement(DesktopSearch.query(DesktopSearch.UIQuery.builder().name("Save").build())).click();
		title = FilenameUtils.removeExtension(file.getName()) + " - Notepad";
		switchToMainWindow();
	}

	@SneakyThrows
	public void close() {
		switchToMainWindow();
		this.getDriver().close();
	}
}

The "About Notepad" window contains several similar elements with text information about the application.
The getContent() method returns a list of strings with text from these elements.
The getContent() method closes the window.

AboutNotepadPage.java
package eu.ibagroup.easyrpa.notepad.page;

import eu.ibagroup.easyrpa.engine.annotation.AfterInit;
import eu.ibagroup.easyrpa.engine.rpa.element.DesktopElement;
import eu.ibagroup.easyrpa.engine.rpa.page.DesktopPage;
import eu.ibagroup.easyrpa.engine.rpa.po.annotation.FindBy;
import eu.ibagroup.easyrpa.engine.rpa.po.annotation.Wait;

import java.util.List;
import java.util.stream.Collectors;

public class AboutNotepadPage extends DesktopPage {

	@FindBy(tagName = "Text")
	private List<DesktopElement> textContent;

	@FindBy(tagName = "Button", name = "OK")
	@Wait(3)
	private DesktopElement okBtn;

	@AfterInit
	public void init() {
		getDriver().waitAndSwitchToWindow("About Notepad", 5);
	}

	public List<String> getContent() {
		return this.textContent.subList(0, 1).stream().map(DesktopElement::getName).collect(Collectors.toList());
	}

	public void close() {
		this.okBtn.click();
	}
}

CreateNote - task-class containing the main algorithm of the task.
CreateNote.java
package eu.ibagroup.easyrpa.notepad.task;

import eu.ibagroup.easyrpa.engine.annotation.AfterInit;
import eu.ibagroup.easyrpa.engine.annotation.ApTaskEntry;
import eu.ibagroup.easyrpa.engine.annotation.Configuration;
import eu.ibagroup.easyrpa.engine.annotation.Driver;
import eu.ibagroup.easyrpa.engine.apflow.ApTask;
import eu.ibagroup.easyrpa.engine.rpa.driver.DesktopDriver;
import eu.ibagroup.easyrpa.notepad.NotepadApplication;
import eu.ibagroup.easyrpa.notepad.page.AboutNotepadPage;
import eu.ibagroup.easyrpa.notepad.page.MainPage;

import java.io.File;
import java.util.List;
import java.util.UUID;

@ApTaskEntry(name = "Create Note And Save")
public class CreateNote extends ApTask {

	@Driver
	private DesktopDriver desktopDriver;

	@Configuration(value = "notepad.app.path", defaultValue = "C:\\Windows\\system32\\notepad.exe")
	private String appPath;

	private String filePath;

	@AfterInit
	public void init() {
		filePath = getConfigurationService().get("notepad.file.path", System.getProperty("user.home"));
	}

	@Override
	public void execute() {

		NotepadApplication notepadApplication = new NotepadApplication(desktopDriver);
		MainPage mainPage = notepadApplication.open(appPath);
		AboutNotepadPage aboutNotepadPage = mainPage.openAboutWindow();
		List<String> content = aboutNotepadPage.getContent();
		aboutNotepadPage.close();
		mainPage.switchToMainWindow();
		for (String s : content) {
			mainPage.printText(s + "\n");
		}

		File file = new File(filePath + "\\" + UUID.randomUUID().toString() + ".txt");
		mainPage.saveAs(file);
		mainPage.close();
	}
}

Automation process and runner classes.

NotepadAp.java
package eu.ibagroup.easyrpa.notepad;

import eu.ibagroup.easyrpa.engine.annotation.ApModuleEntry;
import eu.ibagroup.easyrpa.engine.apflow.ApModule;
import eu.ibagroup.easyrpa.engine.apflow.TaskOutput;
import eu.ibagroup.easyrpa.notepad.task.CreateNote;

@ApModuleEntry(name = "KB - Notepad Automation Demo")
public class NotepadAp extends ApModule {

	public TaskOutput run() throws Exception {
		return execute(getInput(), CreateNote.class).get();
	}
}


NotepadApLocalRunner.java
package eu.ibagroup.easyrpa.notepad;

import eu.ibagroup.easyrpa.engine.boot.ApModuleRunner;

public class NotepadApLocalRunner {

	public static void main(String[] args) {
		ApModuleRunner.localLaunch(NotepadAp.class);
	}
}