As I attempt to populate text boxes on a website using JavaScript in Selenium WebDriver, my coworker shared this GitHub repository with me for speeding up the process.
However, upon running the program, it encounters an error at
((JavascriptExecutor)driver).executeScript("(function( window ) { 'use strict';"
. While it proceeds to traverse through each text box, it fails to add the intended text. Despite spending a considerable amount of time reviewing and comparing our code, my coworker and I remain baffled by the issue. Below is the snippet of my code along with the encountered errors.
Any assistance would be greatly appreciated.
Java Code
InputStream inputStream = ClientInformationComponent.class.getResourceAsStream("AutoFillClientInsuredComponent.js");
StringWriter writer = new StringWriter();
try
{
IOUtils.copy(inputStream, writer, "UTF-8");
}
catch (IOException e)
{
e.printStackTrace();
}
for (ClientDAO client : clients)
{
String script = writer.toString();
try
{
if (driver instanceof JavascriptExecutor)
{
// ERROR OCCURS HERE webdriver.components.ClientInformationComponent.fill(ClientInformationComponent.java:128)
((JavascriptExecutor)driver).executeScript("(function( window ) { 'use strict;'"
+ "var lastName = '" + client.getLastName() + "';"
+ "var firstName = '" + client.getFirstName() + "';"
+ "var middleName = '" + client.getMiddleName() + "';"
+ "var suffix = '" + client.getSuffix() + "';"
+ "var streetAddress = '" + client.getMemberAddress() + "';"
+ "var city = '" + client.getMemberCity() + "';"
+ "var zipCode = '" + client.getMemberZipCode() + "';" + "var birthDate = '" + client.getDateOfBirth() + "';"
+ "var homePhoneNumber = '" + client.getHomePhone() + "';"
+ script);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
JavaScript File
var document = window.document,
fieldValueMap = {
"LastName" : lastName,
"FirstName" : firstName,
"MiddleName" : middleName,
"Suffix" : suffix,
"MemberAddress" : streetAddress,
"MemberCity" : city,
"MemberZipCode" : zipCode,
"MemberZipCodeSuffix" : memberZipCodeSuffix,
"DateOfBirth" : birthDate,
"HomePhone" : homePhoneNumber
};
Object.keys( fieldValueMap ).forEach(function( name ){
var input = document.querySelector( "form input[name='" + name + "']" )
|| document.querySelector( "form select[name='" + name + "']" )
|| document.querySelector( "form textarea[name='" + name + "']" );
input && input.type !== "hidden" && ( input.value = fieldValueMap[ name ] );
});
})( window );
Error Log
org.openqa.selenium.WebDriverException: JavaScript error (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 47 milliseconds
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:02:37'
System info: host: 'COL-ISD-D56345', ip: '10.8.11.30', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_65'
Session ID: 10b4929d-001c-412c-94bc-ce9d006b6dd0
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0, ignoreZoomSetting=false, enablePersistentHover=true, ie.ensureCleanSession=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=accept, version=9, ie.usePerProcessProxy=false, ignoreProtectedModeSettings=false, cssSelectorsEnabled=true, requireWindowFocus=false, initialBrowserUrl=http://localhost:22000/, handlesAlerts=true, ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, ie.browserCommandLineSwitches=, takesScreenshot=true}]
Command duration or timeout: 356 milliseconds
Build info: version: '2.44.0', revision: '76d78cf323ce037c5f92db6c1bba601c2ac43ad8', time: '2014-10-23 13:11:40'
System info: host: 'COL-ISD-D56444', ip: '10.8.12.163', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=9, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, webdriver.remote.sessionid=863d35b9-ff0d-4b83-b4e0-983d9272353d, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:22000/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=accept}]
Session ID: 863d35b9-ff0d-4b83-b4e0-983d9272353d