Ganesh H
Ganesh H

Reputation: 1787

How to execute powershell script in protractor tests?

I am trying to execute powershell script inside protractor test

Protractor spec.ts

 it("Should Execute Powershell Script", async () => {
    browser.get('http://mywebsite.com')
       var spawn = require('child_process').spawn;
       var child = spawn('powershell.exe', ['-noexit', './test.ps1']);
});

test.ps1

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Chrome')
$wshell.SendKeys('Ganesh Hegde')
$wshell.SendKeys("{ENTER}")

The powershell script is not getting executed could you please help me?

Upvotes: 1

Views: 559

Answers (1)

tehbeardedone
tehbeardedone

Reputation: 2858

Here is how I was able to get it to work with async.

Add SELENIUM_PROMISE_MANAGER: false to your config.

Then use spawnSync instead of spawn.

const { browser } = require('protractor');
const { spawnSync } = require('child_process');

describe('spawn test', () => {
  it('should execute powershell script', async () => {
    await browser.get('https://google.com')
    await spawnSync('powershell.exe', ['-noexit', './test.ps1']);
  });
});

This ran the script but it doesn't look like -noexit is working. I could see the text entered into the search input, the results popped up for just a second and then the script exited.

Upvotes: 1

Related Questions