Thursday, September 26, 2024

log4j

 InputStream log4j = TestBase.class.getClassLoader().getResourceAsStream("log4j.properties");
                PropertyConfigurator.configure(log4j);

Friday, August 26, 2022

Parallel testing with DataProviders in TestNG

Create a class for the DataProvider method.

DataProvider Class:
import org.testng.annotations.DataProvider;
 
public class DataProviderDemo {
     
     @DataProvider(name = "testData", parallel=true)
     public Object[][] dataProvFunc() {
           return new Object[][] {           
               {"","","Username cannot be empty"},        
               {"","Test","Username cannot be empty"},
               {"$%1234","2345%$","Invalid credentials"}          
             };
        }
    }
An extra parameter “parallel” is required to initiate parallel execution in TestNG using the data provider.

A new ThreadLocal is instantiated for each test class since it’s in the BeforeClass annotation.
private static final ThreadLocal<WebDriver> WEB_DRIVER_THREAD_LOCAL = new ThreadLocal<WebDriver>();

Below is the complete test code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
 
public class DataProviderParallelTests {
     
    public WebDriver driver;
    private static final ThreadLocal<WebDriver> WEBDRIVER_THREADLOCAL = new ThreadLocal<WebDriver>();
     
     @BeforeMethod
    public void setUp(){
 
        System.setProperty("webdriver.chrome.driver",
                "C:\\Users\\Vibha\\Software\\chromedriver\\chromedriver.exe");
        driver = new ChromeDriver();
        WEBDRIVER_THREADLOCAL.set(driver);
        System.out.println("Before method Thread Id:" + Thread.currentThread().getId());
         
    }
     
    @Test(dataProvider = "testData", dataProviderClass = DataProviderDemo.class)
    public void invalidLoginTest(String username, String password, String errorMessage) throws InterruptedException {
              
        driver = WEBDRIVER_THREADLOCAL.get();
        driver.manage().window().maximize();
        driver.get("https://opensource-demo.orangehrmlive.com/");
      
        Thread.sleep(2000);
        driver.findElement(By.name("txtUsername")).sendKeys(username);
        System.out.println("Username :" + username);
         
        Thread.sleep(2000);
        driver.findElement(By.name("txtPassword")).sendKeys(password);
        System.out.println("password :" + password);
         
        Thread.sleep(2000);
        String expectedError = driver.findElement(By.id("spanMessage")).getText();
        System.out.println("Error Message :" + expectedError);
        Assert.assertTrue(expectedError.contains(errorMessage));
 
    }
          
    @AfterMethod
    public void tear_down() {
          
         WebDriver driver = WEBDRIVER_THREADLOCAL.get();
         System.out.println("After method Thread Id:" + Thread.currentThread().getId());
            if (driver != null) {
                driver.quit();
         }
    }   
}

testng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" thread-count="2" data-provider-thread-count="2">
  <test name="Test">
    <classes>
      <class name="DataProvider.DataProviderParallelTests"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->


In this file, data-provider-thread-count is set to 2, then two browsers will be opened, and the first two tests will run from the list.

Thursday, August 4, 2022

TestNG Assertions

 The Testng assertions would be the primary source of highlighting whether the automated test case is passed or failed.

TestNG provides an Assert class that has multiple methods to raise asserts. To use TestNG assertions, it is important to import required package in your java class : org.testng.Assert

Syntax of Assertion in TestNG :

Below is the generic syntax of testng assertion:

Assert.methodName(actual, expected);
  • Assert : This is the class inbuilt in TestNG framework
  • methodName : This is the name of the Assert class method
  • actual : This is the first parameter of the assert method in which the value is passed that the user gets from application under test
  • expected : This is the second parameter of the assert method in which the user passes the expected value

Types of Assertions:

  1. Hard Assertion 
  2. Soft Assertions

    Hard Assertion:
    Whenever a hard assertion is applied and an assertion statement fails, the assertion in testng throws an exception immediately and terminates the further execution of the same test case and simply continues with the execution of the next test case in the test suite. As soon as the hard assertion condition fails, the test case is marked as failed
    EX: Assert.assertEquals(actualValue,expectedValue);

    Soft Assertions: To implement soft assertion in testng, we use a SoftAssert class and it’s method assertAll() to throw all the exceptions collected during the test case execution. The soft assert basically performs assertion and if a condition fails to meet, it doesn’t throw an exception immediately, instead it continues with the next statement of the same test case until the method assertAll() gets called to throw all the caught exceptions.
    EX:
    SoftAssert softassert = new SoftAssert();

    softassert.assertEquals(actualValue,expectedValue);

    softassert.assertAll();

    When to use Hard and Soft Assertion

            Hard Assertions

Soft Assertions

Use case: Terminates the test case execution with exception as soon as the assertion condition doesn’t meet.

Use case: Validates all the assertion conditions, collects exceptions in case the assertion condition doesn’t meet and throws all exceptions when assertAll() method is called.

When to use: The scenario in which hard assertion is used best would be the login test scenario where if the login test fails, the test case execution must be terminated with an exception as there is no point of moving further without logging into the system.

When to use: Soft Assertion is best used in cases where the test statements of a test case are not dependent on each other. For example, if you are validating a form in which there are multiple fields to be validated, hence it is recommended to soft assert all the fields and then call assertAll() to throw all exceptions at the end of the test case.

TestNG Assert Methods

  1. Assert.assertEqual(String actual, String expected): This assertion method accepts two parameters i.e. the actual value and expected value to validate if the actual string is equal to the expected string or not. The assertion exception is thrown if both the strings are not equal.
  1. Assert.assertEqual(String actual, String expected, String message): This assertion method is similar to the assertion method discussed above, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is not met, the assertion error is thrown along with a message passed here.
  1. Assert.assertEquals(boolean actual, boolean expected): This assertion method accepts two boolean values and validates if both are equal or not.
  1. Assert.assertTrue(condition): This assertion method is used to assert whether the condition passed in a parameter returns true or not. If the condition returns false, the assertion error is thrown.
  1. Assert.assertTrue(condition, message): This assertion method is similar to the assertion method discussed in previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as false, the assertion error is thrown along with a message passed here.
  1. Assert.assertFalse(condition): This assertion method is used to assert whether the condition passed in a parameter returns false or not. If the condition returns true, the assertion error is thrown.
  1. Assert.assertFalse(condition, message): This assertion method is similar to the assertion method discussed in the previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as true, the assertion error is thrown along with a message passed here.
  1. Assert.assertNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns null or not. If the condition doesn’t return null, the assertion error is thrown.
  1. Assert.assertNotNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns value except null or not. If the condition returns null, the assertion error is thrown.

 

Thursday, July 7, 2022

Automation Code Review Checklist

 A code review is a quality assurance activity to ensure that check-ins are reviewed by someone other than the author. If not pair-programming when writing the code, mature development shops often practice this activity as it’s an excellent way to catch errors early in the process. 

Automation Code Review Check List
#NoItemComments
1DuplicationTo avoid code redundancy and to save the development time
2Exceptions HandlingAll exceptions handled properly, even unexpected ones.
3Framework HacksTest automation projects need a strong overall design strategy to make use of framework desired feature.
4Bad Config Changesconfiguration changes can have huge impacts
5Poor CodeOrganized structure is necessary for project scalability and teamwork
6DocumentationDocumentation is vital for good testing and good maintenance. When a test fails, the doc it provides significantly assist triage
7Test CoverageKeep the original test procedure handy, and watch out for missing coverage.
8Hard-Coded ValuesHard-coded values often indicate hasty development
9Typos and Bad FormattingTypos and bad formatting reflect carelessness, cause frustration, and damage reputation.
10Proof of SuccessTests need to run successfully in order to pass review, and proof of success (such as a log or a screen shot) must be attached to the review.

 

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