There are several ways that you could create a common reusable shared function in Karate Test Automation but I method I prefer is to write it in Java and then call it from the feature file.
By writing it in Java, it is easier to step through to debug if the function you are building has bugs in it. I’ve created a Java file named MyRandom.java and placed it at the location shown in the screenshot below.
The content of the MyRandom.java
Define the function in feature file background section and you will be able to call it like a normal function. See screenshot below.
I’m assuming JDK or JRE is installed and configured but currently missing Maven. To setup first download the binary tar.gz archive from https://maven.apache.org/download.cgi
Next deflate it to your preferred location. I like to put mine at ~/Documents/
mv apache-maven-3.8.7-bin.tar.gz ~/Documents
cd ~/Documents
tar -xzvf apache-maven-3.8.7-bin.tar.gz
Now setup your PATH by editing .zprofile
at home folder
cd
vim .zprofile
Copy the below into your .zprofile
and amend your path accordingly.
export M2_HOME='/Users/kevin/Documents/apache-maven-3.8.7'
PATH="${M2_HOME}/bin:${PATH}"
export PATH
Close your terminal and reopen a new one. The changes should take effect already. Test your Maven by checking for its version.
Normally Apache Tomcat comes with the default index.html file. So if you go to http://localhost:8080/index.html
with your web browser it will serve you the index.html file with HTTP status 200.
At the terminal, the HTTP status can be obtained with the following command.
curl -sI http://localhost:8080/index.html|head -1|awk '{print $2}'
This works if the Apache Tomcat is serving a file. What if it is a load balancer that listens to port 8080 but does not serve any files. To test if it is working and listening to port 8080. Use the following command.
nc -z localhost 8080 2>&1|grep succeeded|wc -l
Luhn algorithm is the commonly used logic to produce the checksum of credit card number which is the last number of the long series of numbers.
The code below generates a valid Luhn algorithm Visa credit card number that start with the number 4. Explanation of the algorithm is in the comment.
My cheat sheet of array related functions. I’ll add more along the way when I use more of it.
Declaring Array
int[] ia = new int[3];
ia[0] = 9;
ia[1] = 8;
ia[2] = 7;
Char Array To String
char[] ca = {'a', 'p', 'p', 'l', 'e'};
String s = String.valueOf(ca);
System.out.println(s);
Array Sort
String[] fruits = {"pear", "apple", "banana", "orange"};
Arrays.sort(fruits);
for(String fruit:fruits){
System.out.println(fruit);
}
Array List Sort
ArrayList<String> fruits = new ArrayList<String>();
fruits.add("orange");
fruits.add("pear");
fruits.add("apple");
fruits.add("banana");
Collections.sort(fruits);
System.out.println(fruits);# size
System.out.println(fruits.size()); # 4# get item 0
System.out.println(fruits.get(0)); # apple