How to use AskUI within Java Projects

A Thumbnail showing the title of the Blog Artilcle: How to use AskUI within Java Projects and with that the Java logo
linkedin icontwitter icon

Introduction

AskUI is a powerful tool for UI automation that enables seamless interaction with graphical user interfaces. In this guide, we will walk through the process of integrating AskUI with Java to automate UI testing.

Step 1: Create an AskUI Script

Before calling AskUI in Java, you need to set up an AskUI script. Install AskUI and configure the repository following the official documentation. For this example, we use a sample.test.ts script that logs into a website.

sample.test.ts

1import { aui } from "./helpers/askui-helper";describe('jest with askui', () => { 
2
3it('Fill the entry form with right username and order a purchase ', async () => {  
4
5const standardUsername: string = 'standard_user';        
6const password: string = 'secret_sauce';                
7await aui.execOnShell("start chrome").exec();        
8await aui.waitFor(333).exec();        
9await aui.type("https://www.saucedemo.com/").exec();        
10await aui.waitFor(333).exec();        
11await aui.pressKey('enter').exec();        
12await aui.waitUntil(aui.expect().text('Swag Labs').exists());        
13await aui.click().text("Username").exec();        
14await aui.type(standardUsername).exec();        
15await aui.click().text("Password").exec();        
16await aui.type(password).exec();        
17await aui.waitFor(333).exec();        
18await aui.pressKey('enter').exec();        
19await aui.waitFor(333).exec();  
20
21});
22
23});

Step 2: Set Up Java Environment

Now, create a Java environment in VS Code. Install necessary Java dependencies and create a new Java file under src named AskUIJavaRunner.java.

AskUIJavaRunner.java

public class AskUIJavaRunner {    
public static void main(String[] args) {        
System.out.println("Running AskUI tests in Java");        
try {            
ProcessBuilder processBuilder = new ProcessBuilder("npx", "jest");            
processBuilder.inheritIO();            
Process process = processBuilder.start();            
process.waitFor();        
} catch (Exception e) {            
e.printStackTrace();        
}    
}}

Step 3: Configure AskUI Controller in helper.ts

Ensure you have the AskUI controller set up properly before running the Java integration.

helper.ts

import { UiControlClient, UiController } from 'askui';
import { AskUIAllureStepReporter } from '@askui/askui-reporters';
let aui: UiControlClient;
let controller: UiController;jest.setTimeout(60 * 1000 * 60);beforeAll(async () => {    
controller = new UiController();    
await controller.start();        
aui = await UiControlClient.build({        
reporter: new AskUIAllureStepReporter(),        
credentials: {            
workspaceId: process.env.ASKUI_WORKSPACE_ID,            
token: process.env.ASKUI_TOKEN        
}    
});    
await aui.connect();});afterAll(async () => {    
if (aui) await aui.disconnect();    
if (controller) await controller.stop();});
export { aui };

Step 4: Update Jest Configuration

To run the AskUI test scripts inside the Java environment, configure Jest with proper regex expressions.

jest.config.ts

import type { Config } from '@jest/types';const config: Config.InitialOptions = {    
preset: 'ts-jest',    
sandboxInjectedGlobals: ['Math'],    
setupFilesAfterEnv: ['./helpers/askui-helper.ts'],    
testEnvironment: '@askui/jest-allure-circus',    
testMatch: [        
"**/*.test.ts",        
"**/__tests__/**/*.[jt]s?(x)",        
"**/?(*.)+(spec|test).[tj]s?(x)"    
],    

moduleFileExtensions: ['ts', 'js'],    
transform: {        
'^.+\.tsx?$': 'ts-jest'    
}};

export default config;

Step 5: Install Java Dependencies

Ensure you have installed all necessary Java packages. Additionally, install AskUI in your project.

Step 6: Run the Java Project

Run the Java program from the VS Code terminal using the following commands:

javac -d target src\AskUIJavaRunner.java
java -cp target AskUIJavaRunner

Conclusion

By following these steps, you can successfully call and execute AskUI scripts from Java, integrating UI automation into your Java projects. Happy coding!

Ron van Cann
·
On this page