Forum Discussion

TK6666's avatar
TK6666
Contributor
2 years ago

DB Reset script-to run before tests can be executed

Hi

 

Please could you advise me on how I overcome this issue.

I have a PowerShell script that I need to incorporate as part of my keyword tests in order to have the DB reset before I can run my tests.

The issue I have is that I have manually recoded the steps to run this test but if i add it into my execution plan, the script has not finish running and the 1st test case starts running which then overlap's with the PowerShell script.

how do I overcome this were I need the script to complete running first before the 1st test case starts running?

 

also, which is the best way to set this powershell script and get it into the keyword test to function correctly. I have run through the documentation as best as can but because I am not technical I need some guidance

This is the power shell script

invoke-command -computer 10.3.0.6 -filepath "C:\SQLAUTOMATION\RevertToSnapshot.ps1"

start-process "C:\SQLAUTOMATION\SQLCompare\0 - FullProcess.bat"

 

 

4 Replies

    • Marsha_R's avatar
      Marsha_R
      Champion Level 3

      Once the database script is added to the top of your test cases, then add a loop right after it to look for the last record in the database. If it is not there, wait some more and let the loop check again. If it is there, then fall through the loop and start the actual test cases.

       

       

  • sothqamb's avatar
    sothqamb
    Occasional Contributor

    I am using powershell to do something very similar. I am not sure its possible to do what you want purely within a keyword test, and if it were it would probably be just easier to do in a Script Test.

     

    Here's a functional snippet of what I'm using based from the Documentation I could find from TestComplete. Hopefully it will be easy to understand, all you should need to do is just copy and paste this into a Script Test and change the text '<your command here>' to the command you provided above. This text is in the middle of the last block of code.

    I am assuming that the coding language chosen at the creation of your project was JavaScript. If it was python instead any changes should be easy to make. If it is a different language it could be a little more difficult but definitely doable.

     

    // This function will steal the characters being output by the powershell and display them in the Details tab when the test is over/paused
    // powershellObject: this is the object which holds the powershell.exe that is being used.
    // endChar: this holds a text character eg. ">". The function will stop reading from the powershell's output until either it sees this character, or it reaches the end of the output.
    function readTilChar(powershellObject, endChar){
    var out = "";
    var curChar;
    while (!pwershellObj.StdOut.AtEndOfStream){
    curChar = pwershellObj.StdOut.Read(1);
    out += curChar;
    //Log.Message(curChar)
    if (curChar == endChar) break;
    }
    //Log.Message(out)
    return out;
    }


    // This function will start powershell.exe, display all of the text that is autmaticaally shown in the powershell when its started, then return the object which holds the powershell
    function startPowerShell(){
    var powershell = WshShell.Exec("powershell.exe");
    var out = readTilChar(powershell, ">");
    Log.Message("PowerShell initiation complete: See the output in the Details panel of the test log", out);
    return powershell
    }

    // This function will type the command into the terminal and begin it's execution
    // powershellObject: this is the object which holds the powershell.exe that is being used.
    // command: this is the command that gets run by the powershell. It needs to be of type string. (something like this: "ls")
    function sendCommand(powershellObject, command){
    if (powershellObject != null){
    powershellObject.StdIn.Write(command + "\n")
    }
    }

    // This function will reset the Database
    function resetDatabase(){
    //start the powershell
    var powershell = startPowerShell()

    //Enter what you want the powershell to run here within the ""
    sendCommand(powershell, "<your command here>")

    //This just grabs any output that would happen from your command
    var output = readTilChar(powershell, ">")

    //Create a log in TestComplete and put the output from your command in the Details tab
    Log.Message("Database successfully reset, check Details tab for powershell output.", output)
    }

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I've created a PowerShell script which restores SQL database on my local machine. Also, created a TestedApp for PowerShell with command line parameters to load the script.

     

    The script code will wait until the PowerShell process no longer exists, and then perform the next steps.