How to access endpoint information from within tests


#1

In order to write adaptive tests, it is useful or even necessary to access endpoint-specific information. Imagine running tests against endpoints with different proxies behind them in order to test language or country-specific behavior. Depending on the used endpoint, the tests should then be expecting different results.

Fortunately, it is actually quite easy to accomplish this with Ranorex Webtestit and in the following paragraphs, it is explained how to do this in TypeScript/Protractor projects.
If you are more of a Java fan, go right down to the explanation for Java/TestNG projects.

TypeScript/Protractor

Let’s stick with the example where we want to adapt our tests depending on the used proxy. In order to do that we are going to create a file containing our helper function which we can then use in all our tests.

First of all, create a new folder helpers in the project and there add a new file helpers.ts. In there we define our helper functions and as we want to check whether our endpoints are using a proxy, we add the following function:

export function isBehindProxy(): boolean {
  try {
    const capabilities = JSON.parse(process.env.RX_ENDPOINT_CAPABILITIES);
    if (capabilities.proxy) {
      return true;
    }
  } catch { /**/ }
  return false;
}

We can now use our helper function isBehindProxy() in our test and page object files after importing it:

import { isBehindProxy } from '../../helpers/helpers';

...

if (isBehindProxy()) {
    // TODO add your handling code here
}

Besides the RX_ENDPOINT_CAPABILITIES you can also access other endpoint information like the browser name, whether it is headless, the endpoint type and many more (needless to say that you are able to access all properties of process.env):

const isHeadless = process.env.RX_ENDPOINT_HEADLESS;
const type = process.env.RX_ENDPOINT_TYPE;

Java/TestNG

In Java/TestNG projects we have a similar approach where we also create a Helpers class - the only difference to TypeScript is that the endpoint information has to be accessed a little bit different.

Same as above, first create a folder helpers in the package and a file Helpers.java. In there create a public final class with a private constructor, where you can add your helper functions.
In this example we also want to check if the endpoint uses a proxy by accessing the remote capabilities:

package uitest.helpers;
 
import java.util.Properties;
import org.json.JSONObject;
 
public final class Helpers {
     private Helpers() {}
 
     public static boolean isBehindProxy() {
        try {
            Properties properties = new Properties();
            String resource = System.getProperty("application.properties", "/debug.properties");
            properties.load(Helpers.class.getResourceAsStream(resource));
            properties.load(Helpers.class.getResourceAsStream(properties.getProperty("capabilities")));

            String remotecapabilities = properties.getProperty("remote.capabilities");
            JSONObject cap = new JSONObject(remotecapabilities);
            if (cap.has("proxy")) {
                return true;
            }
        } catch (Exception e) { /* Handle your exception */ }
        return false;
    }
}

In order to use the JSONObject from org.json (you can also use other libraries to parse the capabilities string to JSON), we need to add the dependency in the pom.xml file:

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>

After saving the file, the dependencies are installed automatically, but some times you may need to close and reopen the project so that all newly added dependencies can be resolved.

Now we can import our Helpers class in our test or PO files: import uitest.helpers.*; and use our function to determine whether the endpoint uses a proxy:

if (Helpers.isBehindProxy()) {
    // TODO: add your code here
}

Furthermore, you can access also other properties, for example, check which proxy is exactly used or whether the browser is headless or get the browser name. All these properties can be accessed in the same way as the remote capabilities:

String headless = properties.getProperty("headless");
String browserName = properties.getProperty("browserName");

Conclusion

After reading this article, you finally know how to access endpoint-specific information and how to write tests which behavior depends on the endpoint they are running on.

Other articles which may be of interest to you are how to add remote endpoints or how to take screenshots on test failure in Java/TestNG projects or in Typescript/Protractor projects.


Configuring local endpoints
Language dependent / localized testing
listed #2