Notepad automation
Notepad automation
This example shows how to automate a Windows application using the Desktop driver.
The example consists of several parts:
- Opening a Notepad application
- Opening the About Notepad RPA window
- Scrapping application information
- Saving the information received in Notepad
- Saving file - interaction with the Save As window
Application class opens Notepad and returns MainPage object.
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.
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.
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.
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.