Javafx application - Robot action unable to perform on login screen - Stuck at Launch need to move on testcase loginlogout call when calling from main.
I would like to run login logout testcase on javafx application. I have run the main method. I am able to launch the application. And then how to reach to the testcase call.
NOTE: If I run this through Junit this is working fine. But the requirement is to run via Main.
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.testfx.api.FxRobot;
import org.testfx.api.FxRobotContext;
import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.Start;
import io.mosip.registration.config.AppConfig;
import io.mosip.registration.controller.Initialization;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.geometry.VerticalDirection;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import qa114.pages.DemographicPage;
import qa114.pages.HomePage;
import qa114.pages.LoginPage;
import qa114.pojo.schema.Root;
import qa114.pojo.schema.Schema;
import qa114.runapplication.StartApplication;
import qa114.utility.JsonUtil;
import qa114.utility.PropertiesUtil;
/***
*
* Login and Logout RegClient
* Steps Run this using Junit
* First start method invokes and this will launch Registration Client and through dependency injection
*
* Fxrobot will take control of primary stage and perform keyboard and mouse driven activities.
*
*/
@ExtendWith(ApplicationExtension.class)
public class LoginLogoutTest extends Application{
FxRobot robot;
Schema schema;
Root root;
Scene scene;
Node node;
Boolean flagContinueBtnFileUpload=true;
Boolean flagContinueBtnBioUpload=true;
private static ApplicationContext applicationContext;
private static Stage applicationPrimaryStage;
private static String upgradeServer = null;
private static String tpmRequired = "Y";
LoginPage loginPage;
HomePage homePage;
PropertiesUtil propertiesUtil;
FxRobotContext context;
Boolean result=false;
@FXML
private WebView webView;
@Start
public void start(Stage primaryStage) {
String[] args=new String[2];
try {
System.setProperty("java.net.useSystemProxies", "true");
System.setProperty("file.encoding", "UTF-8");
io.mosip.registration.context.ApplicationContext.getInstance();
if (args.length > 1) {
upgradeServer = args[0];
tpmRequired = args[1];
io.mosip.registration.context.ApplicationContext.setTPMUsageFlag(args[1]);
}
applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
System.out.println("Automaiton Script - ApplicationContext has taken");
Initialization initialization=new Initialization();
initialization.setApplicationContext(applicationContext);
applicationPrimaryStage=primaryStage;
System.out.println("Title:"+primaryStage.getTitle());
initialization.start(primaryStage);
System.out.println("Automaiton Script - ApplicationPrimaryStage has started");
primaryStage=initialization.getPrimaryStage();
System.out.println("Automaiton Script - Done with Start invoke");
context = new FxRobotContext();
context.setPointPosition(Pos.CENTER);
} catch(Throwable e) {
e.printStackTrace();
}
}
@ParameterizedTest
@CsvFileSource(resources = "/login.csv" , numLinesToSkip = 1)
void loginlogout(String userid,String password) throws Exception {
//Set FxRobotContext
robot=new FxRobot();
loginPage=new LoginPage(robot);
//Load Login screen
loginPage.loadLoginScene(robot,applicationPrimaryStage);
//Enter userid and password
loginPage.setUserId(robot,userid);
homePage=loginPage.setPassword(robot, password);
//Logout Regclient
result =loginPage.logout(robot);
assertTrue("TestCase Failed",result);
//return result;
}
public static void main(String[] args) throws Exception {
LoginLogoutTest loginLogoutTest=new LoginLogoutTest();
Application.launch(LoginLogoutTest.class, args); //TILL HERE WORKING FINE
loginLogoutTest.loginlogout("1111","6666"); // NEED TO REACH HERE
}
}
SOLVED- Using thread able to achieve this..
public static void main(String[] args) throws InterruptedException { //1
LoginLogoutTest lg=new LoginLogoutTest();
Thread thread4 = new Thread() { //3 parallel running
@Override
public void run() {
try {
System.out.println("thread4 inside calling testcase");
Thread.sleep(120000); //Needed
robot=new FxRobot();
LoginLogoutTest lg=new LoginLogoutTest();
lg.loginlogout("110123", "mosip123");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
thread4.start();
Application.launch(StartApplication.class, args);//2 infine loop never yeild
} }