Tuesday 28 May 2024

How to run power shell script file in selenium cucumber AfterAll hook using JVM shutdown method


@AfterAll(order = 1) // Cucumber AfterAll hook with order 2
	public static void putFile() throws JSchException, SftpException, IOException, InterruptedException {
		Log.info("AfterAll - with order=1");

		InetAddress localhost = InetAddress.getLocalHost();
		Log.info("System IP Address : " + (localhost.getHostAddress()).trim());

		if (localhost.getHostAddress().trim().equals("11.111.44.77")) {
			Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
				@Override
				public void run() {
					
					try {
						// Path to your PowerShell script
						String scriptPath = "C:\\APIAutomation\\qa-api-rc-cucumber-restassured.ps1";

						// Command to run PowerShell with execution policy bypass and script execution
						String[] command = { "powershell", "-ExecutionPolicy", "Bypass", "-File", scriptPath };

						// Create process builder
						ProcessBuilder pb = new ProcessBuilder(command);

						// Start the process
						Process process = pb.start();

						// Read the output
						BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
						String line;
						while ((line = reader.readLine()) != null) {
							System.out.println(line);
						}

						// Wait for the script to finish
						int exitCode = process.waitFor();
						System.out.println("Script execution finished with exit code: " + exitCode);

					} catch (IOException | InterruptedException e) {
						e.printStackTrace();
					}
					
				}
			}));
			System.out.println("Application Terminating ...");

		} else {
			System.out.println("Since this is not the lab server skipping sending email from linux server");
		}

	}
Hi

No comments:

Post a Comment