Issues with Ultra VNC connections Eggplant

There are some times when the automation scripts built on Eggplant have problems related to VNC connections where some portion of screen doesn’t update.
How to fix it ?

1. Search notepad and open it as Administrator
2. Select this file “c:\Program Files\uvnc bvba\UltraVNC”. Make sure to select “All Files” in the file options
3. Make sure the “ultrsvnc.ini” file has these settings
TurboMode=1
PollUnderCursor=0
PollForeground=1
PollFullScreen=1
OnlyPollConsole=0
OnlyPollOnEvent=0
EnableDriver=1
EnableHook=1
EnableVirtual=0
SingleWindow=0
SingleWindowName=

4. Save the ini file

Given an array of size N, rotate it by D elements.

using System;

public class Program
{
public static void Main()
{
ArrayOperations obj = new ArrayOperations();
int[] arr = new int[]{1,2,3,4,5,6,7};
obj.Rotate(arr,7);
Console.WriteLine("");
obj.Rotate(arr,0);
Console.WriteLine("");
obj.Rotate(arr,2);
}
}
//Given an array of size N, rotate it by D elements.
//D <= N
//example : 1 2 3 4 5 when rotated by 2 elements, it becomes 3 4 5 1 2
//the solution has Time complexity on O(n) and space complexity on O(n)
public class ArrayOperations
{
public void Rotate(int[] arr, int RotateBy)
{
if(RotateBy <= arr.Length)
{
int[] arr2 = new int[arr.Length];
//Put a first for loop from RotateBy to end of the array
//Assign the values in the new array which start from 0
for(int i= RotateBy ; i < arr.Length ; i++)
{
arr2[i-RotateBy] = arr [i];
}
//Second loop is from 0 to until we reach RotateBy position
//Assign the values in the new array which start from arr.Length - RotateBy as the values prior to this has been assigned already in above loop
for(int i= 0 ; i < RotateBy ; i++)
{
arr2[arr.Length - RotateBy + i] = arr[i] ;
}
PrintArray(arr2) ;
}
else
{
Console.WriteLine("Can not rotate the array more than its length");
}
}

public void PrintArray(int[] arr)
{
for(int i=0 ; i< arr.Length; i++)
{
Console.Write(arr[i].ToString() + " ");
}
}
}

Add Generated column to the MySQL Table

This code can help you add a column in your table that can be generated from the other columns


ALTER TABLE `tablename`
CHANGE COLUMN `col_generated` `col_generated` VARCHAR(45) GENERATED ALWAYS AS (timediff(col_start_time, '%Y-%m-%d %h:%i:%s'), STR_TO_DATE(col_end_time,'%Y-%m-%d %h:%i:%s'))) STORED ;

The above code above will generate a column with the time that gets calculated as a result of difference in the time in the other two columns.

Close the application process on SUT using Eggplant

It is little tricky to talk to the file system or the task manager in the most reliable ways using eggplant.
Below is the code to close the process using command prompt with Eggplant instead of using the Task manager images.

function CloseApplicationProcess
try
TypeText windowsKey
TypeText controlKey ,"r"
TypeText "Cmd.exe"
TypeText enterKey
wait 1
TypeText "taskkill /FI " && quote &"Imagename eq test.exe" & quote & " -f"
TypeText enterKey
wait 1
TypeText "exit"
TypeText enterKey
return true
Catch anException
tracescreen on
put the exception's location into excepLoc
replace return in excepLoc with ", "
log anException & " | " & excepLoc & " | " & " Function : CloseApplication"
return false
end try
end CloseApplicationProcess

you can do a lot more by interacting with the command prompt, like dealing with the file system, running the batch files, restarting the services etc.
Whatever you could do using command prompt on a machine, can be done this way using Eggplant on the SUT directly.

Send Email using SenseTalk in Eggplant

This is the code to send email using the code and not setting up SMTP setting on the Eggplant functional.


Set email_host to "testhost.com" //Set the host variable to your host
Set email_port to "00" //Set the port
Set email_from to "test@testhost.com"
Set email_password to DecodeText("notARealEncodedPassword") \\You can encode the test using eggplant functional and pass it here so that its not plain text
Set email_to to "abc@testhost.com" \\Set the value same as
Set Content to "Test Email!"

sendMail (smtp_host:email_host, smtp_type:"Login",smtp_port:email_port,smtp_user: email_from, smtp_password: DecodeText(email_password) , To:email_to, Subject:Content)

Reading Data From Excel using SenseTalk in Eggplant

How to read data from excel using SenseTalk in Eggplant


//Here we are reading the data from excel and treating it as Database

set myExcelDB to table ("Sheet1") of (type:"excel", file:"C:\TestExcel\Test.xlsx")
put the records of myExcelDB into Patients
put the number of items in Patients into totalItems

//Repeate for all the iterations
repeat totalItems times
//This will be set as false in case any errors occours in between
set columnData to item repeatIndex() of Patients
log columnData.PatientLastName
log columnData.PatientFirstName
end Repeat

This is how your excel file would looks like for the code above.

This is what the execution looks like for the code above.

Access Database using Powershell

function Invoke-SQL {
param(
[Parameter(Mandatory)]
[string]$dataSource,
[Parameter(Mandatory)]
[string]$database,
[Parameter(Mandatory)]
[string]$sqlCommand
)
Begin {
$connectionString = “Data Source=$($dataSource); Integrated Security=SSPI; Initial Catalog=$($database)”

$retVal = “”

$connection = New-Object system.data.SqlClient.SQLConnection($connectionString)
$command = New-Object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$command.CommandTimeout = 0
try {
$retVal = $connection.Open()
} catch {
print “Invoke-SQL: connectionString=[$($connectionString)] Error=[$($Error[0])] retVal=[$($retVal)]”

}

$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
try {
$retVal = $adapter.Fill($dataSet)
} catch {
print “Invoke-SQL: sqlCommand=[$($sqlCommand)] Error=[$($Error[0])] retVal=[$($retVal)]”

}

$connection.Close() > $null
$dataSet.Tables
}
}

Update Excel file using Powershell

Function UpdateExcel($strQuery,$strFileName)
{
$strProvider = “Provider=Microsoft.ACE.OLEDB.12.0”
$strDataSource = “Data Source = $strFileName”
$strExtend = “Extended Properties=Excel 8.0”
#$strQuery = “Update [$strSheetName] set Name = ‘Som’ ”
$objConn = New-Object System.Data.OleDb.OleDbConnection(“$strProvider;$strDataSource;$strExtend”)
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand($strQuery)
$sqlCommand.Connection = $objConn
$objConn.open()
$sqlCommand.ExecuteNonQuery()
$objConn.Close()
$objConn.Dispose()
}

Read Excel using Powershell

Function ReadExcel($strQuery,$strFileName)
{
$strProvider = “Provider=Microsoft.ACE.OLEDB.12.0”
$strDataSource = “Data Source = $strFileName”
$strExtend = “Extended Properties=Excel 8.0”
$objConn = New-Object System.Data.OleDb.OleDbConnection(“$strProvider;$strDataSource;$strExtend”)
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand($strQuery)
$sqlCommand.Connection = $objConn
$objConn.open()
$sqlReader = $sqlCommand.ExecuteReader()
$Datatable = New-Object System.Data.DataTable
$DataTable.Load($SqlReader)
$sqlReader.Close()
$objConn.Close()
return $DataTable
}