How to run my custom Java code (Snippet)

In the Subject7 platform, you can run your own Java code as part of your automation scripts. This is referred to as Snippet in the platform. In this article, we are going to create a "Hello World!" Java program, and we are going to run it as part of our test automation. 

Here is a quick summary of what will be covered: 

  • Create an automated test that does a google search for "Hello World!"
  • The first step of the test will do a GOTO_URL https://www.google.com
  • The second step will set a variable to be passed to the snippet
  • The third step runs our Java code that uses Selenium to add text to Google's search box 
  • The last step does a verify_text command after running our code as a Snippet to illustrate continuity from and to Java in the platform

 

Here is the sample Java code. You need to have Appium and Selenium Jar in your classpath. You also need to have Chrome driver on your machine and point to it as described in the code comments. 

 

package com.subject7.snippets;

import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* Created by rexfeizi on 12/2/18.
*/
public class MyFirstSnippet {


//This is called by Subject7 to pass all the variables set before reaching this
//command. Also all the data templates resolutions.
private Map<String, String> parametersMap;

//This is called by Subject7 Platform to pass the current Selenium driver used
//throughout the test to this snippet
private WebDriver driver;

//This is called by Subject7 Platform to pass the current Appium driver used
//throughout the test to this snippet
private AppiumDriver appiumDriver;


public void setWebDriver(WebDriver wb) {
this.driver = wb;
}

public void setParametersMap(Map<String, String> map) {
this.parametersMap = map;
}

public void setAppiumDriver(AppiumDriver appiumDriver) {
this.appiumDriver = appiumDriver;
}


/**
* This is the method that will be called from Subject7. It has to be public and
* should return a String
* @return Subject7 platform parses the return argument of the invoked method.
* "Status, message". Status could be either of this values case sensitive:
* ERROR, FAIL_STOP, FAIL_SKIP, PASS
* Message: Will be shown in execution reports local or cloud.
*/
public String executeMySnippet() {


String statusUsedByPlatform = "ERROR, default message";

try {
//This will be shown in Java console NOT Subject7 UI
System.out.println("Starting My Snippet Execution .");

if (driver == null)
return "ERROR, driver is null. ";

if (parametersMap == null)
return "ERROR, parameter map is null.";

String var = this.parametersMap.get("@my_search_keyword");

System.out.println("Current URL from driver:" + driver.getCurrentUrl());
System.out.println("Variable passed as in my_search_keyword=" + var);


WebElement element = driver.findElement(By.name("q"));
element.sendKeys(var + "\n"); // send also a "\n" for "enter

// wait until the google page shows the result
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));

statusUsedByPlatform = "PASS, Hello World!.";

//DO NOT CLOSE the driver or quite, otherwise, all commands in your
//automated test after this step cannot use the driver anymore.
} catch (Exception e) {
statusUsedByPlatform = "ERROR, " + e.getMessage();
e.printStackTrace();
}
return statusUsedByPlatform;
}


/**
* Main method simulates how your snippet will be called from Subject7.
* This is for the user to create and debug their code before using it with
* Subject7.
* This will not be called from Subject7 platform.
* @param args
*/
public static void main(String[] args) {


//These values will be passed from Subject7. We initialize here for testing
//purposes.
HashMap<String, String> parametersMap = new HashMap<String, String>();
parametersMap.put("@my_search_keyword","Hello World");

//Sample path: c:\myFolder\chromedriver.exe
System.setProperty("webdriver.chrome.driver", "FULL PATH TO YOUR CHROME DRIVER INCLUDING THE DRIVER FILENAEM");

ChromeDriver driver = null;
DesiredCapabilities capabilities = null;
capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));

//DO NOT CLOSE the driver in your main code. driver is used throughout the test case and Subject7 handles it.
driver = new ChromeDriver((Capabilities)capabilities);

//This is exactly what Subject7 platform does with Java reflection.
MyFirstSnippet myFirstSnippet = new MyFirstSnippet();
myFirstSnippet.setWebDriver((WebDriver)driver);
myFirstSnippet.setParametersMap(parametersMap);
myFirstSnippet.setAppiumDriver(null);

//This step is done is done in the platform, we just simulate it here in order
//to make sure our code is working
driver.navigate().to("https://www.google.com");

try {
String result = myFirstSnippet.executeMySnippet();
System.out.println(result); //Subject7 parses the result 'Pass|Fail|Fail_Stop, message for command'
} catch (Exception e) {
e.printStackTrace();

} finally {

//This is a local IDE test so we can and should close the driver.
driver.close();
}
}
}

Please make sure you read the above code and all its comments to understand exactly how the system works. Once you run this code in your favorite IDE, create a jar file out of it. 

Then log in to Subject7 and under Artifacts use File uploads to upload your jar file. 

mceclip0.png

Once you uploaded your jar file, it is time to create a snippet. Again, under Artifacts menu and this time choose Code Snippets. Then click "New Snippet" icon on the newly opened tab as shown in the following picture. 

mceclip1.png

mceclip2.png

Snippet form appears and here is the explanation of the required fields: 

Name: Just like any other artifact in the system, this is used for referencing in test steps. We will call this hello_world

Class: This is the fully qualified class name that contains the Java method that we want to call. In our case:  com.subject7.snippets.MyFirstSnippet

Dependencies: Basically we select the jar file that we previously uploaded. 

Data Template: This is used only if our snippet uses a data template. Our example just uses a variable and not a template at this point, so no action is required. 

Click on save and you will see a success message. 

Now, create a automated test in Subject7 and start the first command with 

1. Goto_URL HALT https://www.google.com

mceclip3.png

2. For the second step, just use SET_VAR to set a variable that will be used in the Snippet. Here is the command: SET_VAR HALT Static my_search_keyword "Hello World". Please note that the variable name must be the same as the one referenced in the code, here is the reference: 

String var = this.parametersMap.get("@my_search_keyword");

mceclip4.png

3. For the third step, select "Execute_Java" from the command wizards and the following form appears. 

mceclip5.png

Snippet: Choose hello_world created on the previous step. 

Class: Same as above (com.subject7.snippets.MyFirstSnippet) or leave blank. 

Method: Just the name of the method.  executeMySnippet

Used drivers: select either setWebdriver and setAppiumDriver methods in your Snippet

4. For the fourth step, just do a VERIFY_TEXT on "Hello World".  

mceclip6.png

That's it! Run your test using local or cloud run. Try changing things around to understand how everything comes together and send us questions and comments. 

 

 

 

 

 

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Please sign in to leave a comment.