问题
I've recorded a test using Selenium IDE
and am submitting the generated .side
file to selenium-side-runner
to run on a Selenium Grid
built using Zalenium
. Is it possible to run a command that calls driver.manage().addCookie()
from the test that was submitted to selenium-side-runner
? I want to do this to send messages back to Zalenium with test progress and status
I added a command executeScript
to the Selenium IDE editor with a target of driver.manage().addCookie({name: 'test', value: 'test'})
I see that the command that selenium-side-runner generated in commons.js
was
await driver.executeScript(`driver.manage().addCookie({name:'test', value: 'test'});`);
Doing this causes the browser to report an error JavascriptError: javascript error: driver is not defined
I think what I need is the code to be generated without the driver.executeScript wrapper. Is there a way to accomplish this without exporting my Selenium IDE
test to NUnit
?
回答1:
I was able to make this functionality work by crudely modifying the selenium-side-runner
package on my Windows dev machine
In file ~\node_modules\selenium-side-runner\node_modules\selianize\dist\selianize.cjs.js
Change
function generateScript(script, isExpression = false) {
return `await driver.executeScript(\`${isExpression ? `return (${script.script})` : script.script}\`${script.argv.length ? ',' : ''}${script.argv.map(n => `vars["${n}"]`).join(',')});`;
}
to
function generateScript(script, isExpression = false) {
if (script.script.indexOf('zalenium') > -1)
{
return script.script;
} else
{
return `await driver.executeScript(\`${isExpression ? `return (${script.script})` : script.script}\`${script.argv.length ? ',' : ''}${script.argv.map(n => `vars["${n}"]`).join(',')});`;
}
}
Now when running a test with selenium-side-runner, calling "executeScript" with any value that contains zalenium
will generate the command verbatim in the test script
来源:https://stackoverflow.com/questions/62712675/how-to-add-cookie-to-selenium-ide-test-running-in-grid-via-selenium-side-runner