Wednesday, March 23, 2022

Automate and Validate QR Code Details

 I have achieved this with help of the following two libraries from Google,

    Google zxing – core
    Google zxing – javase

Validate QR code from the web application directly:
String QRCodeImage=”//img[@src=’https://assets.outlook.com/qrprod/-1603550372.png ‘] “;
String urlOfQRCode = driver.findElement(By.xpath(QRCodeImage)).getAttribute(“src”);
// Create an object of URL Class
URL url = new URL(urlOfQRCode);
// Pass the URL class object to store the file as image
BufferedImage bufferedimage = ImageIO.read(url);
// Process the image
LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedimage);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
// To Capture details of QR code
Result result = new MultiFormatReader().decode(binaryBitmap);
Assert.assertEquals(“Expected_Result”, result.getText());


Validate QR code from the local system:
// Pass the QR code object to store the file as image
BufferedImage bufferedimage = ImageIO.read(new File(“your_QR_code_image_path”));
// Process the image
LuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedimage);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(luminanceSource));
// To Capture details of QR code
Result result = new MultiFormatReader().decode(binaryBitmap);
Assert.assertEquals(“Expected_Result”, result.getText());

 

Monday, March 14, 2022

Credit card number validation

 package com.gabrielbauman.gist;

import java.util.regex.Pattern;

public enum CardType {

    UNKNOWN,
    VISA("^4[0-9]{12}(?:[0-9]{3}){0,2}$"),
    MASTERCARD("^(?:5[1-5]|2(?!2([01]|20)|7(2[1-9]|3))[2-7])\\d{14}$"),
    AMERICAN_EXPRESS("^3[47][0-9]{13}$"),
    DINERS_CLUB("^3(?:0[0-5]\\d|095|6\\d{0,2}|[89]\\d{2})\\d{12,15}$"),
    DISCOVER("^6(?:011|[45][0-9]{2})[0-9]{12}$"),
    JCB("^(?:2131|1800|35\\d{3})\\d{11}$"),
    CHINA_UNION_PAY("^62[0-9]{14,17}$");
    private Pattern pattern;

    CardType() {
        this.pattern = null;
    }

    CardType(String pattern) {
        this.pattern = Pattern.compile(pattern);
    }

    public static CardType detect(String cardNumber) {

        for (CardType cardType : CardType.values()) {
            if (null == cardType.pattern) continue;
            if (cardType.pattern.matcher(cardNumber).matches()) return cardType;
        }

        return UNKNOWN;
    }

}

 

 

package com.gabrielbauman.gist;

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class CardTypeTest {

    @Test
    public void testDetection() {
        assertEquals(CardType.VISA, CardType.detect("4000056655665556"));
        assertEquals(CardType.MASTERCARD, CardType.detect("5105105105105100"));
        assertEquals(CardType.AMERICAN_EXPRESS, CardType.detect("371449635398431"));
        assertEquals(CardType.DISCOVER, CardType.detect("6011000990139424"));
        assertEquals(CardType.DINERS_CLUB, CardType.detect("30569309025904"));
        assertEquals(CardType.JCB, CardType.detect("3530111333300000"));
        assertEquals(CardType.UNKNOWN, CardType.detect("0000000000000000"));
    }
}

Wednesday, March 9, 2022

Selenium WebDriver Architecture

 It consists of Selenium IDE, Selenium RC, Selenium WebDriver and Selenium Grid.

Selenium IDE:
Selenium IDE (Integrated Development Environment) is a Firefox & Chrome plugin. It allows us to record and playback the scripts.

Selenium RC:
It is basically a HTTP Proxy Server which injects JavaScript into a browser to automate the actions.

Selenium WebDriver:
Selenium WebDriver is a browser automation framework that accepts commands and sends them to a browser. It is implemented through a browser-specific driver. It controls the browser by directly communicating with it.
Selenium WebDriver supports Java, C#, PHP, Python, Perl, Ruby. Operation System Windows, Mac OS, Linux, Solaris. Browsers Mozilla Firefox, Internet Explorer, Microsoft Edge, Google Chrome, Safari, Opera Android, iOS, HtmlUnit.

Selenium Grid:
It is a Selenium API which can be used to run automation tests on multiple browsers and operating systems.

The Selenium WebDriver Architecture follows the popular Client-Server architecture and consists mainly of four components:

  • Selenium Client and Language Bindings
  • JSON Wire Protocol over HTTP
  • Browser Drivers
  • Web Browsers