Upload file
Upload file
This example illustrates how to automate file upload in a web application, as well as using two drivers in one task.
Application class opens web browser and returns MainPage object.
package eu.ibagroup.easyrpa.fileupload; import eu.ibagroup.easyrpa.engine.rpa.Application; import eu.ibagroup.easyrpa.engine.rpa.driver.BrowserDriver; import eu.ibagroup.easyrpa.engine.rpa.element.BrowserElement; import eu.ibagroup.easyrpa.fileupload.page.MainPage; public class ViljamisApplication extends Application<BrowserDriver, BrowserElement> { public ViljamisApplication(BrowserDriver driver) { super(driver); } @Override public MainPage open(String... args) { String gmailUrl = args[0]; getDriver().get(gmailUrl); return createPage(MainPage.class); } }
The MainPage class contains method openFileWindow(DesktopDriver). This method opens an "Open" window, so it should return an OpenFilePage class object. However, OpenFilePage refers to another application, which is responsible for its creation. Therefore, the method receives as a parameter the driver to work with the application it opens, then it performs actions to open an "Open" window, creates the necessary application and returns the necessary page by calling the corresponding method from the application.
package eu.ibagroup.easyrpa.fileupload.page; import eu.ibagroup.easyrpa.engine.rpa.driver.DesktopDriver; import eu.ibagroup.easyrpa.engine.rpa.element.BrowserElement; import eu.ibagroup.easyrpa.engine.rpa.page.WebPage; import eu.ibagroup.easyrpa.engine.rpa.po.annotation.FindBy; import eu.ibagroup.easyrpa.engine.rpa.po.annotation.Wait; import eu.ibagroup.easyrpa.fileupload.OpenFileApplication; import org.openqa.selenium.interactions.Actions; public class MainPage extends WebPage { @FindBy(xpath = "//input[@name='image']") @Wait(waitFunc = Wait.WaitFunc.VISIBLE) private BrowserElement chooseFileInput; @FindBy(xpath = "//input[@type='submit']") @Wait(waitFunc = Wait.WaitFunc.VISIBLE) private BrowserElement uploadInput; public OpenFilePage openFileWindow(DesktopDriver desktopDriver) { Actions builder = new Actions(getDriver()); builder.moveToElement(chooseFileInput).click().perform(); OpenFileApplication openFileApplication = new OpenFileApplication(desktopDriver); return openFileApplication.open(); } public void upload() { uploadInput.click(); } }
Structurally, the file opening window is not a separate window, but an element of the browser window.
Structurally, the file opening window is not a separate window, but an element of the browser window. Therefore, to access the "Open" window elements, we need to switch the DesktopDriver to the main browser window.
The OpenFilePage class stores a string with the window name and switches in the init method. When creating an object, this method is executed and switches the driver to a browser window, giving access to its elements.
The ChooseFile method enters the path to the file and clicks the "Open" button.
package eu.ibagroup.easyrpa.fileupload.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; public class OpenFilePage extends DesktopPage { @FindBy(className = "Edit", name = "File name:") @Wait(3) private DesktopElement fileNameInput; @FindBy(className = "Button", name = "Open") @Wait(3) private DesktopElement openBtn; @AfterInit public void init() { String title = "Input type='file' test - Google Chrome"; getDriver().waitAndSwitchToWindow(title, 5); // switch to the root Chrome window first getDriver().waitAndSwitchToWindow("Open", 5); } public void chooseFile(String fullFilePath) { fileNameInput.sendKeys(fullFilePath); openBtn.click(); } }
UploadFile- task-class containing the main algorithm of the task.
package eu.ibagroup.easyrpa.fileupload.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.annotation.DriverParameter; import eu.ibagroup.easyrpa.engine.apflow.ApTask; import eu.ibagroup.easyrpa.engine.rpa.driver.BrowserDriver; import eu.ibagroup.easyrpa.engine.rpa.driver.DesktopDriver; import eu.ibagroup.easyrpa.engine.rpa.driver.DriverParams; import eu.ibagroup.easyrpa.fileupload.ViljamisApplication; import eu.ibagroup.easyrpa.fileupload.page.MainPage; import eu.ibagroup.easyrpa.fileupload.page.OpenFilePage; @ApTaskEntry(name = "Get product list from InvoicePlane") public class UploadFile extends ApTask { @Driver(param = { @DriverParameter(key = DriverParams.Browser.SELENIUM_NODE_CAPABILITIES, initializerName = DriverParams.BrowserCapabilities.CHROME) }) private BrowserDriver browserDriver; @Driver private DesktopDriver desktopDriver; @Configuration(value = "viljamis.app.url", defaultValue = "https://viljamis.com/filetest/") private String viljamisUrl; private String filePath; @Configuration(value = "viljamis.file.name", defaultValue = "AboutNotepad.txt") private String fileName; @AfterInit public void init() { filePath = getConfigurationService().get("viljamis.file.path", System.getProperty("user.home")); } @Override public void execute() { ViljamisApplication viljamisApplication = new ViljamisApplication(browserDriver); MainPage mainPage = viljamisApplication.open(viljamisUrl); OpenFilePage openFilePage = mainPage.openFileWindow(desktopDriver); openFilePage.chooseFile(filePath + "\\" + fileName); mainPage.upload(); } }
Automation process and runner classes.