Find if an option exist in select or not

Leave a comment

Here is the soluton to determine if the given option exist in the page (select item) or not

public boolean isSelectOptionPresent(WebDriver webdriver, String selectLocator, String optionToCheck) {
    if (webdriver == null) {
        throw new NullPointerException("WebDriver instance is null.");
    }

    if (null == optionToCheck || optionToCheck.isEmpty()) {
        throw new IllegalArgumentException("Invalid value
    }

    String[] availableOptions = webdriver.getSelectOptions(selectLocator);
    for(String option: availableOptions) {
        if (optionToCheck.equals(option) {
            return true;
        }
    }
    return false;
}

 

Validate group names in TestNG

Leave a comment

I came across a requirement to validate the group names for the TestNG tests. The validation is required to ensure that the QA engineers tag their tests with only the list of published group names. Since group names in TestNG are Strings, which can accept any value, it becomes difficult to pick all tests belonging to a particular category unless the group names are validated.

For example, if there are 50,000 tests which can be grouped under three buckets, as ‘ui’ and ‘backend’ and ‘cli’, it is common for engineer to write various names for these buckets like ‘gui’, ‘UI’, ‘browser’, etc. To avoid such variations in naming, the validation of group name is required.

Here is the code to validate groups before running tests.

package com.example.testng.listeners;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;

public class GroupValidator implements IMethodInterceptor {
	/**
	 * Checks if the group names of the list of tests being run are valid.
	 */
	@Override
	public List intercept(List methods,
			ITestContext context) {
		/* Put your valid group names in a map so that it is easy to validate
		 * One can also put the list in an external file and read it here.
		 */
		Map validGroupNames = new HashMap();
		validGroupNames.put("ui", true);
                validGroupNames.put("backend", true);
                validGroupNames.put("cli", true);
		for(IMethodInstance mInstance: methods) {
			ITestNGMethod method = mInstance.getMethod();
			if (method.isTest()) {
				System.out.println("Validating: " + method.getMethodName());
				for(String group : method.getGroups()) {
					if (! validGroupNames.containsKey(group)) {
						throw new RuntimeException("Group name validation failed. Cannoot run the tests");
					}
				}
			}
		}
		return methods;
	}
}

Add the above class as listener to your testng and it will validate the group names of the test methods.
NOTE that the code above allows a @Test without any group. The validation is only done when there is a group name specified.

Environment variables in gradle

Leave a comment

Read environment variables
The requirement is access environment variable in gradle or any language is very common. To access environment variable value from gradle, you can use System.getenv() api. This is the java’s System class API which gradle allows you to access with script. Here is an example :

task printHome {
    println System.getenv("HOME")
}
gradle -q printHome
/home/skgupta

More