viernes, 25 de noviembre de 2022

UI.Vision RPA (I)

1. Installation

1. Install UI.Vision RPA from chrome web store

2. Install UI.Vision XModules for desktop

3. Verify that it is installed on Chrome (see the blue icon)


4. Click the icon and verify this window is presented



2. Modes

UI.Vision has 2 modes: Bowser and Desktop

Selecting the modes: Pres the button on the top right part 


and in Vision tab select BrowserAutomation or Desktop Automation


The browser mode let you save macros directly as you execute the tasks, but in desktop mode, you must write the sentences with the help of the ide.

 

3. The browser mode

First, open the browser and press the blue UI symbol. Open a new tab in Chrome

Here is an example:
1. Open the browser
2. Navigate to "https://pre-myvillage.sedipualba.es/segex"
3. 
Let's execute this steps:









lunes, 25 de octubre de 2021

Selenium (3): Install in a server without GUI : Chrome & Gecko

1. New Post

Now you don't need to install Firefox or Chrome in a Linux Server without GUI, you only need to copy the drivers from
  • https://github.com/mozilla/geckodriver/releases for Gecko (Firefox) or
  • https://chromedriver.chromium.org/downloads for Chrome
And you have to set options to "headless"

Here is the code of a class to use headless chrome and gecko that works in an ubuntu server

package ximodante.basic.utils.selenium;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
//import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;

/**
 * IMPORTANT: You must have downloaded CHROME-DRIVER OR FIREFOX-DRIVER(Gecko Driver) and installed in your system
 * DOWNLOAD from:
 *  https://chromedriver.chromium.org/downloads
 *  https://github.com/mozilla/geckodriver/releases/
 * 
 * @author ximo dante
 *
 */
public class SeleniumEduImp implements ISeleniumEdu{
	private String driverName="webdriver.chrome.driver";
	private String browserPath="/home/informatica/MyPrograms/Selenium/chromedriver";
	private Integer left=1; 
	private Integer top=1; 
	private Integer width=300; 
	private Integer height=200; 
	private String[] opts=null; 
	private Long implicitWait=10L; 
	private String downloadDir="/home/informatica/downloads";
	private Integer timeout=10;
	private Integer cycleTimeout=10;
	
	private Wait<WebDriver> fluentWait=null;
	private WebDriver webDriver=null;
	private WebElement webElement=null;
	
	
	public SeleniumEduImp(String driverName, String browserPath, 
			Integer left, Integer top, Integer width, Integer height, String[] opts, 
			Long implicitWait, String downloadDir, Integer timeout, Integer cycleTimeout) {
		if (driverName  !=null) this.driverName = driverName;
		if (browserPath !=null) this.browserPath =browserPath;
		if (left        !=null) this.left=        left;
		if (top         !=null) this.top=         top;
		if (width       !=null) this.width=       width;
		if (height      !=null) this.height=      height;
		if (opts        !=null) this.opts=        opts;
		if (implicitWait!=null) this.implicitWait=implicitWait;
		if (downloadDir !=null) this.downloadDir= downloadDir;
		
		if (timeout     !=null) this.timeout=     timeout;
		if (cycleTimeout!=null) this.cycleTimeout=cycleTimeout;
		
		this.webDriver=getWebdriver(this.driverName, this.browserPath,
				this.left, this.top, this.width, this.height, 
				this.opts, this.implicitWait, this.downloadDir);
		//this.fluentWait=getFluentwait(this.webDriver, this.timeout, this.cycleTimeout);
		
	}
	
	
	
	private Wait<WebDriver> getFluentwait(WebDriver driver, int timeout, int cycleTimeout) {
		return new FluentWait<WebDriver>(driver)       
	   		.withTimeout(Duration.ofSeconds(timeout))
		.pollingEvery(Duration.ofSeconds(cycleTimeout))    
		.ignoring(NoSuchElementException.class); 
	}
	
	
	private WebDriver getWebdriver(String driverName, String browserPath, 
			Integer left, Integer top, Integer width, Integer height, String[] opts, 
			Long implicitWait, String downloadDir) {
		System.setProperty(driverName, browserPath);
				
		WebDriver driver =null;
		if (driverName.contains(".chrome.")) {
			ChromeOptions options=new ChromeOptions();
			if (opts!=null) options.addArguments(opts);
			//options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent");
			if (downloadDir!=null) {
				if (downloadDir.trim().length()>0) {
					Map<String, Object> prefs = new HashMap<String, Object>();
					prefs.put("download.default_directory",  downloadDir);
					prefs.put("download.prompt_for_download" ,false);
					prefs.put("download.directory_upgrade", true);
					prefs.put("download.safebrowsing.enabled", true);
					prefs.put("useAutomationExtension", false); //Headless only!!!
					options.setExperimentalOption("prefs", prefs);
					
				}
			}
			driver = new ChromeDriver(options);
		}
		else if (driverName.contains(".gecko.")) {
			FirefoxOptions options=new FirefoxOptions();
			
			FirefoxProfile profile = new FirefoxProfile();
			profile.setPreference("browser.download.folderList",2);
			profile.setPreference("browser.download.dir", downloadDir);
			profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/zip,application/pdf");
						
			options.setProfile(profile);
			if (opts!=null) options.addArguments(opts);
			//options.addArguments("--headless", "--disable-gpu", "--window-size=1920,1200","--ignore-certificate-errors", "--silent");
			driver = new FirefoxDriver(options);
			
			
		}
		if (width>100 && height>100) driver.manage().window().setSize(new Dimension(width,height));
		if (top>=0 && left>=0) driver.manage().window().setPosition(new Point(left, top));
	    
	    driver.manage().deleteAllCookies();
	    if (implicitWait>0) driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(implicitWait)); //????
	    return driver;
	}
	
	//============================================================================
	//Public 
	
	/**
	 * Quit the browser
	 */
	@Override
	public boolean quit() {
		//this.webDriver.quit();
		try {this.webDriver.close();}catch (Exception e) {e.printStackTrace();}
		try {this.fluentWait=null;}catch (Exception e) {e.printStackTrace();}
		try {this.webDriver.quit();}catch (Exception e) {e.printStackTrace();}
		return true;
	}
	
	@Override
	public boolean isSessionNull() {
		return this.webDriver==null;
	}
	
	/**
	 * Go to a web page
	 */
	@Override
	public void gotoURL(String url) {
		this.webDriver.get(url);
		this.fluentWait=getFluentwait(this.webDriver, this.timeout, this.cycleTimeout);
	}
	/**
	 * Find a web element by xpath
	 * @param xpath
	 * @return
	 */
	@Override
	public boolean findElementByXpath(String xpath) {
		boolean found=false;
		try {
			this.webElement=this.webDriver.findElement(By.xpath(xpath));
			if (this.webElement!=null) found=true;
		} catch (Exception e) {
			System.out.println ("No trobat "+ xpath);
		}	
		return found;
	}
	
	@Override
	public boolean findElementById(String id) {
		boolean found=false;
		try {
			this.webElement=this.webDriver.findElement(By.id(id));
			if (this.webElement!=null) found=true;
		} catch (Exception e) {
			System.out.println ("No trobat "+ id);
		}	
		return found;
	}
	
	/**
	 * click the component
	 */
	@Override
	public void elementClick() {
		this.webElement.click();
	}
	
	/**
	 * Reset the content of the component
	 */
	@Override
	public void elementClear() {
		this.webElement.clear();
	}
	
	/**
	 * Send keys to the component
	 * @param keysToSend
	 */
	@Override
	public void elementSendKeys(CharSequence... keysToSend) {
		this.webElement.sendKeys(keysToSend);
	}
	
	/**
	 * Return the text of the control (if not hidden)
	 * @return
	 */
	@Override
	public String getElementText() {
		return this.webElement.getText();
	}
	
	
	@Override
	public boolean clickByXpath(String xpath) {
		boolean found=false;
		this.webElement=this.webDriver.findElement(By.xpath(xpath));
		if (this.webElement!=null) found=true;
		this.webElement.click();
		return found;
	}
	
	@Override
	public boolean elementSendKeysByXpath(String xpath, CharSequence... keysToSend) {
		boolean found=false;
		this.webElement=this.webDriver.findElement(By.xpath(xpath));
		if (this.webElement!=null) found=true;
		this.webElement.sendKeys(keysToSend);
		return found;
	}
	
	@Override
	public String getElementTextByXpath(String xpath) {
		this.webElement=this.webDriver.findElement(By.xpath(xpath));
		return this.webElement.getText();
	}
	
	@Override
	public String getCurrentUrl() {
		return this.webDriver.getCurrentUrl();
	}
	
	@Override
	public String getAttribute(String attributerName) {
		return this.webElement.getAttribute(attributerName);
	}
	
	@Override
	public String getPageCode() {
		return this.webDriver.getPageSource();
	}
	
	@Override
	public void saveScreenShot(String fileName) throws IOException {
		TakesScreenshot scrShot =(TakesScreenshot) this.webDriver;
		File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
		File DestFile=new File(fileName);
		FileUtils.copyFile(SrcFile, DestFile);
	}
	
	@Override
	public void close() {
		this.webDriver.close();
	}
	
	public static void main (String[] args) {
		var selenium=new SeleniumEduImp(
				"webdriver.chrome.driver",
				"/home/informatica/MyPrograms/Selenium/chromedriver", 
				0, //left),
				0, //top
				100, //width
				800, //height
				new String[]{
						"--headless",
						"user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"
				}, //Opts null,
				//new String[]{"headless"}, //Opts null,
				//null, // quitar
				10L,
				"/home/informatica/MyResources",
				20,
				null
				);
		
		boolean ok=false;
		boolean found=false;
		int maxTries=5;
		int nTries=0;
		
		selenium.gotoURL("http://localhost:8080/W01-CSV/provaservlet");
		
	}
	
		
}

 

================================================

2. OLD Post Don't use it!

 1. Install xvfb

1
2
3
4
5
6
7
8
#install Xvfb
sudo apt-get install xvfb

#set display number to :5
Xvfb :1 -screen 5 1024x768x8 &
export DISPLAY=:1.5    

#you are now having an X display by Xvfb


2. Install chrome


 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.debwget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

 sudo dpkg -i google-chrome-stable_current_amd64.deb

Gives an error (because expects the system to have a GUI) . But you must check the version in the log

in my case it is 95.0.4638.47+


3. Download the chromedriver

in 

https://chromedriver.chromium.org/downloads 

select the desired version in this case 95.0.4638.17

download it in a directory that will be referenced by the java software!!!


4. What else

Now from the java program, the references to the chromedriver should be to the new server, and follow the steps of the last posts.

lunes, 11 de enero de 2021

Sikulix (3): Sikulix and Selenium for selecting a certificate (best option)

 1. Introduction

Here is an example where :

1. Get a browser window and open a web that expects a user certificate,


2. Click an image with Selenium




3. Use Sikulix to recognize the text "XIMO DANTE" and click it for selecting the certificate




4. Click on the image of the button. This image was obtained by the Sikulix IDE.

The ide was opened with:

java - jar path_to_sikulix.jar/sikulix-2.04.jar

And the image of the button was captured with the option of capturing images of the ide.


Here is the java code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package mypack;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.sikuli.script.Match;
import org.sikuli.script.Pattern;
import org.sikuli.script.Region;

import utils.SikulixUtils;

public class Myclass {

  public static void main(String[] args) {
    //1. URL
    String myUrl="https://sede.seg-social.gob.es/wps/portal/sede/sede/Ciudadanos/CiudadanoDetalle/!ut/p/z0/rU_LDoIwEPwVOHA02wIhciTGEJQmGkOEXkgDBeujPFqN_r3UO5y8TDKzk5kdoJADlewlWqZFJ9l94gUNSg8HPg4RTmOUbFGUkX0WekcvTjCcuIId0AWTG5gUdyQb0gLtmb6shGw6yA2OD66sj1XxUYtGVKzuFOQIz56gENdhoBHQqpOavzXkite8_DEpJo-DjOCgSjxrVjNplJk4B803LW-ahv_7FRfhdeBDf4vPh7QhKrLtLwe74DE!/";
		
    //2. Get the driver
    System.setProperty("webdriver.chrome.driver", "/home/ximodante/Selenium/chromedriver");
    WebDriver driver =null;
    ChromeOptions options=new ChromeOptions();
    driver = new ChromeDriver(options);
    driver.manage().deleteAllCookies();
    driver.manage().timeouts().implicitlyWait(10L, TimeUnit.SECONDS); //Set implicit wait
	    
    //3. Get the browser wait
    Wait<WebDriver> wait= new FluentWait<WebDriver>(driver)       
	.withTimeout(Duration.ofSeconds(4))
	.pollingEvery(Duration.ofSeconds(1))    
	.ignoring(NoSuchElementException.class); 
	   
    //4. click on the icon for accessing by certificatwe
    String myXpath= "//ul[@id='listServ_d1b8a537-f135-47cc-b77e-9597f62af41b']/li[@class='first pr1']/a[@target='_blank']";
    WebElement webElement=null;
    try {
	webElement=wait.until(new Function<WebDriver, WebElement>() {       
    	  public WebElement apply (WebDriver browser) { 
	    WebElement wE=null;
	    for( WebElement webElem: browser.findElements(By.xpath(myXpath))) {
	      System.out.println("WebElem="+webElem.toString());
	      wE=webElem;
	      if ((wE.isDisplayed() && wE.isEnabled()) )
		break;
	    }
	    return wE;
	 }  
	});
   } catch (Exception e) {e.printStackTrace();}
	    
   //Click the element
   webElement.click();
    
   //5. Now displays a window offering the available certificates
   //   and use Sikulix for selecting certificate for XIMO DANTE
   //   using OCR and click on button to accept the certificate
	 
   Point top=driver.manage().window().getPosition();
   Dimension dim=driver.manage().window().getSize();
   try {
        //  OCR recognition
	utils.SikulixUtils.click(top.getX(), top.getY(), dim.getWidth(), dim.getHeight(), "XIMO DANTE");
	Region region=new Region (top.getX(), top.getY(), dim.getWidth(), dim.getHeight());
	//The image of the button to click
	Pattern image=new Pattern("/myImages/bt_Dacord.jpg");
	Match match=region.find(image);
	match.click();
   } catch (Exception e) {e.printStackTrace();}
		
  }
}

and the Utility class:

package utils;

import java.io.File;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Match;
import org.sikuli.script.Pattern;
import org.sikuli.script.Region;
import org.sikuli.script.Screen;

public class SikulixUtils {

	/**
	 * Finds text in a region
	 * 
	 * @param leftTop
	 * @param widthHeight
	 * @param text
	 * @throws Exception
	 */
	public static void click(Point leftTop, Dimension widthHeight, String text) throws Exception {
		click(leftTop.getX(), leftTop.getY(), widthHeight.getWidth(), widthHeight.getHeight(), text);
	}

	public static void click(int left, int top, int width, int height, String text) throws FindFailed {
		Region region = new Region(left, top, width, height);
		// Match match=region.findWord("ACCVCA-120");
		// Match match=region.findT(text);
		Match match = region.findLine(text);
		match.click();
	}

	/**
	 * Clicks on an image given by its path (from resource folder) and a region to
	 * look for the image
	 * 
	 * @param leftTop
	 * @param widthHeight
	 * @param isExecutedFromJar
	 * @param resourceFolder
	 * @param fileName
	 * @throws Exception
	 */
	public static void click(Point leftTop, Dimension widthHeight, boolean isExecutedFromJar, String subResourceFolder,
			String fileName) throws Exception {
		click(leftTop.getX(), leftTop.getY(), widthHeight.getWidth(), widthHeight.getHeight(), isExecutedFromJar,
				subResourceFolder, fileName);
	}

	public static void click(int left, int top, int width, int height, boolean isExecutedFromJar,
			String subResourceFolder, String fileName) throws Exception {
		String imgPath = "";
		boolean hasFolder = true;
		if (hasFolder && subResourceFolder != null)
			hasFolder = false;
		if (hasFolder && subResourceFolder.trim().length() == 0)
			hasFolder = false;

		if (!hasFolder)
			imgPath = FileUtils01.getFilePath(isExecutedFromJar, subResourceFolder.trim() + File.separator + fileName);
		else
			imgPath = FileUtils01.getFilePath(isExecutedFromJar, fileName);

		Region region = new Region(left, top, width, height);
		Pattern image = new Pattern(imgPath);

		// Match match=region.findWord("ACCVCA-120");
		Match match = region.find(image);
		match.click();

	}

	public static void clickCertificate() {
		String certificateZkhan = "C:/zkhan.PNG";
		String okButton = "C:/ok.PNG";
		Screen screen = new Screen();
		try {
			screen.click(certificateZkhan);
			screen.click(okButton);
		} catch (FindFailed findFailed) {
			findFailed.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}

And another dependency

package utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.net.URLDecoder;
import java.security.CodeSource;

public class FileUtils01 {

	/**
	 * Folder that contains jar file or folder that contains project folder
	 * 
	 * @return
	 * @throws Exception
	 */
	public static String getJarContainingFolder() throws Exception {
		Class<?> aClass = MethodHandles.lookup().lookupClass();
		CodeSource codeSource = aClass.getProtectionDomain().getCodeSource();

		File jarFile;

		if (codeSource.getLocation() != null) {
			jarFile = new File(codeSource.getLocation().toURI());
		} else { // It is not a jar file
			String path = aClass.getResource(aClass.getSimpleName() + ".class").getPath();
			String jarFilePath = path.substring(path.indexOf(":") + 1, path.indexOf("!"));
			jarFilePath = URLDecoder.decode(jarFilePath, "UTF-8");
			jarFile = new File(jarFilePath);
		}
		String s = jarFile.getParentFile().getAbsolutePath();
		System.out.println("S------>:" + s);
		if (s.endsWith(File.separator + "target")) { // Maven target directory for compiled classes
			s = s.substring(0, s.lastIndexOf(File.separator));
			s = s.substring(0, s.lastIndexOf(File.separator));
		}
		return s;
	}

	public static byte[] readFile(String fileName) throws IOException {
		File file = new File(fileName);// filename should be with complete path
		FileInputStream fis = new FileInputStream(file);
		byte[] b = new byte[(int) file.length()];
		fis.read(b);
		fis.close();
		;
		return b;
	}

	public static void writeToFile(String fileName, String myString) throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
		writer.write(myString);
		// do stuff
		writer.close();
	}

	public static String getFilePath(boolean isExecutedFromJAR, String relativeFilePath) throws Exception {
		if (isExecutedFromJAR)
			return FileUtils01.getJarContainingFolder() + File.separator + relativeFilePath;
		else {
			return System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator
					+ "resources" + File.separator + relativeFilePath;
		}
	};

	/**
	 * 
	 * @param filePath
	 * @return
	 * @throws Exception
	 * 
	 *                   Example
	 *                   ExcelFile=getRelativeFromJarFile("excel\MyExcel.xlsx")
	 */
	public static String getRelativeFromJarFile(String filePath) throws Exception {
		return FileUtils01.getJarContainingFolder() + File.separator + filePath;
	}

	/**
	 * test in main class
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			System.out.println(getJarContainingFolder());
			System.out.println(System.getProperty("java.class.path"));
			System.out.println(System.getProperty("user.dir"));
			byte[] b = readFile("/home/eduard/Audit.0.log");
			String sb = new String(b);
			System.out.println(sb);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}