RPA script examples
Get available and busy RPA nodes number
We can call RPA Grid Console page and parse required totals out:
Expand to see the code sample
<config>
<script><![CDATA[
import com.thoughtworks.selenium.*;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.interactions.Actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
String HUB_CONSOLE_URL = "http://127.0.0.1:4444/grid/console";
Integer TIMEOUT = 240;
]]></script>
<robotics-flow>
<robot name="seleniumDriver" driver="internet explorer" close-on-completion="true" start-in-private="true">
<script><![CDATA[
ieDriver = seleniumDriver.getWrappedObject();
ieDriver.manage().timeouts()
.implicitlyWait(TIMEOUT, TimeUnit.SECONDS)
.pageLoadTimeout(TIMEOUT, TimeUnit.SECONDS);
ieDriver.get(HUB_CONSOLE_URL);
String tmp = "";
List allAutoit = ieDriver.findElements(By.xpath("//div[@type='browsers']/p/a"));
int allAutoitNum = allAutoit.size();
List allIE = ieDriver.findElements(By.xpath("//div[@type='browsers']/p/img"));
int allIENum = 0;
for (WebElement element : allIE) {
tmp = element.getAttribute("title");
if (tmp.length()>0 && (tmp.contains("explorer") || tmp.contains("executed"))) allIENum++;
}
List busyAutoit = ieDriver.findElements(By.xpath("//div[@type='browsers']/p/a"));
int busyAutoitNum = 0;
for (WebElement element : busyAutoit) {
tmp = element.getAttribute("class");
if (tmp.length()>0) busyAutoitNum++;
}
List busyIE = ieDriver.findElements(By.xpath("//div[@type='browsers']/p/img"));
int busyIENum = 0;
for (WebElement element : busyIE) {
tmp = element.getAttribute("class");
if (tmp.length()>0 && tmp.equals("busy")) busyIENum++;
}
]]></script>
</robot>
</robotics-flow>
<export include-original-data="true">
<single-column name="total_ie_nodes">
<template>${allIENum.toString()}</template>
</single-column>
<single-column name="busy_ie_nodes">
<template>${busyIENum.toString()}</template>
</single-column>
<single-column name="total_autoit_nodes">
<template>${allAutoitNum.toString()}</template>
</single-column>
<single-column name="busy_autoit_nodes">
<template>${busyAutoitNum.toString()}</template>
</single-column>
</export>
</config>
important
Constant string HUB_CONSOLE_URL = "http://127.0.0.1:4444/grid/console" ; should be changed to pull page from URL where RPA Bot Relay is installed in your environment.
Escape special symbols for sending to RPA Node
When we need to send a password to RPA, we should escape special symbols. See this sample function in the example below.
Expand to see the code sample
<script><![CDATA[
import org.openqa.selenium.remote.*;
import org.openqa.selenium.*;
import org.apache.commons.lang3.StringEscapeUtils;
public String escaped(String text, RemoteWebDriver driver) {
String res = text;
String brName = driver.getCapabilities().getBrowserName();
if ("autoit".equals(brName)) {
res = res.replaceAll("([\\{\\}\\!\\#\\^\\+]+)","{$1}");
} else {
res = StringEscapeUtils.escapeJava(res);
}
return res;
}
]]></script>
Web scrape using RPA, XML transformation, XPath
Sample below demonstrates an approach to HTML page parsing and scrapping using RPA.
Expand to see the code sample
<config>
<include-config code="Selenium_functions" />
<script></script>
<var-def name="xmlValue" />
<var-def name="search_case_number">
<template>${search_case_number}</template>
</var-def>
<var-def name="first_name">
<template>${first_name}</template>
</var-def>
<var-def name="last_name">
<template>${last_name}</template>
</var-def>
<var-def name="dob">
<template>${dob}</template>
</var-def>
<var-def name="found_information" />
<var-def name="xmlValueProfile" />
<var-def name="linked_attorney"/>
<var-def name="found_dob" />
<var-def name="case_id" />
<var-def name="linked_case_id" />
<var-def name="department"/>
<var-def name="offence_date"/>
<var-def name="officer_badge"/>
<var-def name="site_chaged">
<template>false</template>
</var-def>
<var-def name="site_chaged">
<template>false</template>
</var-def>
<var-def name="source_name">
<template>Duval County Clerk of Courts</template>
</var-def>
<var-def name="source_search_url">
<template>https://core.duvalclerk.com/CoreCms.aspx?mode=PublicAccess</template>
</var-def>
<var-def name="matched">
<template>false</template>
</var-def>
<try>
<body>
<robotics-flow>
<robot name="seleniumDriver" driver="firefox" close-on-completion="false">
<script></script>
</robot>
</robotics-flow>
<var-def name="xmlValue">
<html-to-xml prunetags="head,meta" omitdoctype="true">
<template>${profilePage}</template>
</html-to-xml>
</var-def>
<case>
<if condition='${site_chaged.toString().equals("false") && !xmlValue.toString().contains("No results found for specified search criteria")}'>
<var-def name="results">
<xpath expression="//div[@class='displayOnly']//table[@class='resultGrid']//tr">
<template>${xmlValue}</template>
</xpath>
</var-def>
<var-def name="case_number" />
<loop item="item">
<list>
<var name="results" />
</list>
<body>
<empty>
<var-def name="case_number">
<xpath expression="//tr//div/text()">
<var name="item" />
</xpath>
</var-def>
<var-def name="dob_found">
<xpath expression="//tr/td[2]/div/span[2]/text()">
<var name="item" />
</xpath>
</var-def>
<var-def name="information">
<xpath expression="//tr/td[2]//span/text()">
<var name="item" />
</xpath>
</var-def>
<var-def name="caseid">
<xpath expression="//tr//td[3]/span[1]/@id">
<var name="item" />
</xpath>
</var-def>
<script><![CDATA[
String search = dob.toString().trim();
String found = case_number.toString().trim().replace("\n"," ").replaceAll("\\s+"," ").toUpperCase();
String foundDob = dob_found.toString().trim().replace("\n","").replaceAll("\\s+","").replace("DOB:","");
String foundInfo = information.toString().trim().replace("\n"," ").replaceAll("\\s+"," ");
String caseId = caseid.toString().trim();
if (search.equals(foundDob)) {
sys.defineVariable("matched","true",true);
sys.defineVariable("linked_case_id",case_number.toString().trim(),true);
sys.defineVariable("found_information",foundInfo,true);
String val = "//div[@class='igtab_THContent']//span[@id='" + caseId +"']/parent::*/parent::*/parent::*";
sys.defineVariable("case_id",val,true);
}
]]></script>
</empty>
</body>
</loop>
<script></script>
<var-def name="xmlValueProfile">
<html-to-xml prunetags="head,meta" omitdoctype="true">
<template>${profilePageCase}</template>
</html-to-xml>
</var-def>
<var-def name="linked_attorney">
<xpath expression="//div[@class='igtab_THContent']//table[@id='c_AttorneyBlock_c_AttorneyClerkGridView']//tr[2]/td/span/text()">
<var name="xmlValueProfile" />
</xpath>
</var-def>
<var-def name="department">
<xpath expression="//div[@class='igtab_THContent']//table[@id='c_CaseSummaryClerkGridView']//tr[1]/td[4]/text()">
<var name="xmlValueProfile" />
</xpath>
</var-def>
<var-def name="offence_date">
<xpath expression="//div[@class='igtab_THContent']//table[@id='c_CaseSummaryClerkGridView']//tr[2]/td[4]/text()">
<var name="xmlValueProfile" />
</xpath>
</var-def>
<var-def name="officer_badge">
<xpath expression="//div[@class='igtab_THContent']//table[@id='c_CaseSummaryClerkGridView']//tr[3]/td[4]/text()">
<var name="xmlValueProfile" />
</xpath>
</var-def>
</if>
</case>
<script></script>
</body>
<catch>
<var-def name="site_chaged">
<template>true</template>
</var-def>
</catch>
</try>
<var-def name="currentDate">
<script return="result"></script>
</var-def>
<var-def name="last_checked">
<script return="result"></script>
</var-def>
<var-def name="searchResultsLink">
<s3 access-key="" secret-key="" bucket="rpa-tmp">
<s3-put-public path="gis/search_duval_${currentDate}/search_${last_checked}.html" content="${xmlValue}" content-type="text/html" content-disposition="inline" />
</s3>
</var-def>
<var-def name="profilePageLink">
<s3 access-key="" secret-key="" bucket="rpa-tmp">
<s3-put-public path="gis/search_duval_profile${currentDate}/search_${last_checked}.html" content="${xmlValueProfile}" content-type="text/html" content-disposition="inline" />
</s3>
</var-def>
<!-- Export values to the output CSV file -->
<export include-original-data="true">
<single-column name="matched" value='${matched}' />
<single-column name="found_dob" value='${found_dob}' />
<single-column name="linked_case_id" value='${linked_case_id}' />
<single-column name="linked_attorney" value='${linked_attorney}' />
<single-column name="found_information" value='${found_information}' />
<single-column name="site_chaged" value='${site_chaged}' />
<single-column name="search_s3_link_2" value="${searchResultsLink}" />
<single-column name="profile_s3_link_2" value="${profilePageLink}" />
<single-column name="source_search_url" value="${source_search_url}" />
<single-column name="source_name" value="${source_name}" />
<single-column name="department" value="${department}" />
<single-column name="offence_date" value="${offence_date}" />
<single-column name="officer_badge" value="${officer_badge}" />
</export>
</config>
Catch RPA processing issues
This sample shows how to catch RPA processing issue and use it inside a business process like a true/false flag site_chaged – flag is true by default.
Expand to see the error flag example
<config>
<script></script>
<var-def name="xmlValue" />
<var-def name="search_case_number">
<template>${search_case_number}</template>
</var-def>
<var-def name="first_name">
<template>${first_name}</template>
</var-def>
<var-def name="last_name">
<template>${last_name}</template>
</var-def>
<var-def name="dob">
<template>${dob}</template>
</var-def>
<var-def name="site_chaged">
<template>false</template>
</var-def>
<var-def name="site_chaged">
<template>false</template>
</var-def>
<var-def name="source_name">
<template>Duval County Clerk of Courts</template>
</var-def>
<var-def name="source_search_url">
<template>https://core.duvalclerk.com/CoreCms.aspx?mode=PublicAccess
</template>
</var-def>
<try>
<body>
<robotics-flow>
<robot name="seleniumDriver" driver="chrome"
close-on-completion="true">
<script></script>
</robot>
</robotics-flow>
</body>
<catch>
<var-def name="site_chaged">
<template>true</template>
</var-def>
</catch>
</try>
<export include-original-data="true">
<single-column name="site_chaged" value='${site_chaged}' />
</export>
</config>
Get clipboard value
This sample shows how to get clipboard value using Sikuli library.
Expand to see the sample
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<var-def name="sikuliClipboard"><![CDATA[
import org.sikuli.script.*;
return App.getClipboard();
]]></var-def>
<robotics-flow>
<robot driver="desktop">
<script></script>
</robot>
</robotics-flow>
<export include-original-data="true"></export>
</config>
Improve switchTo action in case of window opened by another driver
This sample shows how to improve the switchTo action, when you try to switch to a window opened by other driver or opened before.
note
The switchTo action is executing for a long time.
To improve this, before running the switchTo action, you should set the page load time to a small value.
Precondition
Notepad should be opened.
Expand to see the switchTo() improved sample
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<robotics-flow>
<robot driver="desktop">
<capability name="SEARCH_ALL_WINDOWS" value="true"/>
<script></script>
</robot>
</robotics-flow>
<export include-original-data="true"></export>
</config>
Set timeout for switchTo action
The switchTo().window("windowName", "timeoutInMilis") method is added to minimize waiting time in case window was not opened previously. This method does not use the pageLoadTimeout option, instead it uses the timeoutInMilis parameter. In each case, the value of this timeout is unique, i.e., when using it one should understand how long it takes to wait for a specific window.
You can use one value pageLoadTimeout (which is large) for all
driver.swithTo().window(), but in this case there will be big time delays when the requested window was not opened.
Precondition
Notepad should be opened.
Expand to see the set timeout for switchTo action example
<config xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" scriptlang="groovy">
<robotics-flow>
<robot driver="desktop">
<capability name="SEARCH_ALL_WINDOWS" value="true"/>
<script></script>
</robot>
</robotics-flow>
<export include-original-data="true"></export>
</config>