DB connection fails when run in a test
I have the below function. When I run the function on its own, it passes everytime. When I run this function in a scripted test, it fails everytime with the error 'Cannot open database "BDS.SynergyPlusWfm.Working". I change nothing at all. Why would it pass run on its own but fail when run from a scripted test? Ive ruled out environment issues as it passes fine when run on its own. Its driving me mad so any help is appreciated. Test complete is on windows server 2022. The SQL instance in in a podman container on a windows server 2025 box.
.
function verifyDBTable(sqlQuery, csvFilePath, noResults) {
try {
logFolderCreation("Verify DB Table");
// Build connection string
var connectionString =
"Provider=MSOLEDBSQL;" +
"Data Source=" + Project.Variables.SQLServer + ";" +
"Initial Catalog=BDS.SynergyPlusWfm.Working;" +
"User ID=" + Project.Variables.SQLUser + ";" +
"Password=" + Project.Variables.SQLPassword + ";" +
"Encrypt=true;" +
"TrustServerCertificate=true;" +
"Persist Security Info=True;";
// File paths
var actualFilePath = checkVariable("<_projectPath>\\Files\\TestData\\Actual\\" + csvFilePath);
var expectedFilePath = checkVariable("<_projectPath>\\Files\\TestData\\Expected\\dataBase\\" + csvFilePath);
// Reset output file
if (aqFile.Exists(actualFilePath)) {
aqFile.Delete(actualFilePath);
aqFile["Create"](actualFilePath)
}
aqFile["Create"](actualFilePath)
var file = aqFile.OpenTextFile(actualFilePath, aqFile.faWrite, aqFile.ctUTF8);
// Open DB connection
var dbConn = ADO.CreateADOConnection();
dbConn.ConnectionString = connectionString;
dbConn.LoginPrompt = false;
dbConn.Open();
if (!dbConn.Connected) {
Log.Error("Database connection failed.");
return;
}
Log.Message("Connected to database successfully.");
// Execute SQL and fetch results
var recSet = dbConn.Execute_(sqlQuery);;
if (recSet.EOF) {
if (noResults != null) {
Log.Checkpoint("No results were returned as expected.");
} else {
Log.Error("Query returned no results.");
}
recSet.Close();
dbConn.Close();
file.Close();
return;
}
// Write header
var header = [];
for (var i = 0; i < recSet.Fields.Count; i++) {
header.push(recSet.Fields.Item(i).Name);
}
file.WriteLine(header.join(","));
// Write each row
while (!recSet.EOF) {
var row = [];
for (var j = 0; j < recSet.Fields.Count; j++) {
var field = recSet.Fields.Item(j);
var val = "";
try {
var fieldType = field.Type;
if (field.Value === null || field.Value === undefined) {
val = "";
} else if (fieldType === 135 || fieldType === 7 || fieldType === 146) {
val = aqConvert.DateTimeToFormatStr(field.Value, "%Y-%m-%d %H:%M:%S");
} else {
val = String(field.Value);
}
// Escape for CSV format
val = val.replace(/"/g, '""');
if (val.indexOf(",") > -1 || val.indexOf('"') > -1) {
val = '"' + val + '"';
}
} catch (ex) {
val = "<IGNORE>";
}
row.push(val);
}
file.WriteLine(row.join(","));
recSet.MoveNext();
}
// Clean up
file.Close();
recSet.Close();
dbConn.Close();
compareCSVFiles(actualFilePath, expectedFilePath);
} catch (e) {
Log.Error("Error exporting DB results to CSV: " + e.message);
}
}
I solved it in the end. It was the WS2 IP. It wasn't resolving properly