catchpoint/WebPageTest.api-nodejs

execAndWait does not work in the scripts

abhagupta opened this issue · 1 comments

Hi,
I tried giving a script similar to

 {navigate:	'https://www.amazon.com/'},
  {execAndWait:	"document.querySelector('#twotabsearchtextbox').value='TV'"},
  {execAndWait:	"document.querySelector('#pdagDesktopSparkleAsinsContainer .clickthroughLink').click()"},

Note the execAndWait did work when I used wpt.scriptToString function for above. setValue works but not execAndWait. The original WPT script (see below) works fine in WPT fine

navigate	https://www.amazon.com/
execAndWait	document.querySelector('#twotabsearchtextbox').value='TV'; document.querySelector('.nav-input').click();
execAndWait	document.querySelector('#pdagDesktopSparkleAsinsContainer .clickthroughLink').click();

Am I missing anything?

solution:

WebPageTest = require('webpagetest');

function _EXEC(wptServer, script, location) {
	return new Promise((resolve, reject) => {
		const wpt = new WebPageTest(wptServer);
		wpt.runTest(
			WebPageTest.scriptToString(script),
			{ location },
			(err, data) => (err) ? reject(err) : resolve(data)
		)
	})
}

const script = [
	{navigate: 'https://www.walmart.com/'},
	{execAndWait: ["document.querySelector('[data-automation-id=header-search-input]').value='TV'"]},
	{execAndWait: ["document.querySelector('[data-automation-id=GlobalHeaderSearchbar-submit]').click()"]},
	{execAndWait: ["document.querySelector('.search-result-productimage div a').click()"]},
	{execAndWait: ["document.querySelector('.prod-product-cta-add-to-cart button').click()"]},
	{execAndWait: ["document.querySelector('.cart-pos-view-cart button').click()"]},
	{logData: 1}
]

//const wptServer = 'https://www.webpagetest.org/'
const wptServer = 'http://wptserver6951.cloudapp.net/'
const location = 'CentralUS4:Chrome'

_EXEC(wptServer, script, location)
.then(data => {
	console.log(data)
})