1

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?

4
  • I think you should consider to use sendKeys. stackoverflow.com/questions/19914915/… Commented Oct 30, 2019 at 16:45
  • 1
    Could it be the site you are using? I copied your exacts script and replaced the url with google.com and it worked fine. Commented Oct 30, 2019 at 17:55
  • 1
    I take that back. It works fine if you don't use async. I originally did not have that and it was working. When I added the async keyword and set SELENIUM_PROMISE_MANAGER: false, in my config it stopped working. I removed the config property and async keyword from the test and it started working again. Commented Oct 30, 2019 at 18:06
  • @funatsu.fumiya No, I can't use send keys there is a purpose I asked.However thanks for reply Commented Oct 31, 2019 at 4:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.