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();
		}

	}

}

No hay comentarios:

Publicar un comentario