This is a migrated thread and some comments may be shown as answers.

Stored Procedures as coded step

17 Answers 204 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Amardeep
Top achievements
Rank 1
Amardeep asked on 10 Jan 2014, 10:22 AM
Hi,

I want use the stored procedure as a Setup coded step where I want to attach database from server location to machine where I am running the test and upgrade it. After that I want to run my recorded steps.

How can I implement that?

Regards
A
using System.Data.SqlClient;
CREATE PROCEDURE SP_UpgradeDatabaseTest @RegressionRunID int
AS
 
BEGIN
    DECLARE @CMDSQL             varchar(1000),
            @DatabaseID         int,
            @DatabaseName       varchar(250),
            @CurrentLocation    varchar(250),
            @TargetLocation     varchar(250),
            @Result             int
 
 
    SET     @TargetLocation = 'C:\UpgradeDatabaseTest\'
 
    -- Tidy up previous run... detach databases
    DECLARE Database_cursor CURSOR
    FOR
    SELECT name FROM sys.databases
    JOIN DatabasesForUpgrading ON name = DatabaseName
 
    OPEN Database_cursor
 
    FETCH NEXT FROM Database_cursor
    INTO    @DatabaseName
    WHILE @@FETCH_STATUS = 0
    BEGIN
     
        EXEC sp_detach_db @DatabaseName
 
        FETCH NEXT FROM Database_cursor INTO    @DatabaseName
 
    END
         
    CLOSE Database_cursor
    DEALLOCATE Database_cursor
 
    EXEC master..xp_cmdshell 'Del C:\UpgradeDatabaseTest\*.mdf'
    EXEC master..xp_cmdshell 'Del C:\UpgradeDatabaseTest\*.ldf'
 
    -- Get list of databases to upgrade, copy, attach, and upgrade
    DECLARE Database_cursor CURSOR FOR SELECT DatabaseID, DatabaseName, Location FROM DatabasesForUpgrading WHERE Upgrade =1
    OPEN Database_cursor
 
    FETCH NEXT FROM Database_cursor
    INTO    @DatabaseID,
            @DatabaseName,
            @CurrentLocation
 
    WHILE @@FETCH_STATUS = 0
    BEGIN
        -- copy database to expected location
        DECLARE @PassedVariable VARCHAR(100)
 
        SET @CurrentLocation = @CurrentLocation + RTRIM(@DatabaseName) + '*.*'
         
        SET @CMDSQL = 'C:\NR_UpgradeDatabaseDBRepository\CopyFiles.bat ' + @CurrentLocation + ' ' + @TargetLocation
                 
        EXEC master..xp_cmdshell @CMDSQL
         
        -- Attach Database to Server
        SELECT @CMDSQL = 'CREATE DATABASE ' +  @DatabaseName +
        ' ON (FILENAME = ''' + @TargetLocation + @DatabaseName + '.mdf''' + '),
        (FILENAME = ''' + @TargetLocation + @DatabaseName + '_Log.ldf'')
        FOR ATTACH'
     
        EXECUTE (@CMDSQL)
 
        -- Run Upgrade Database
        SET @CMDSQL = '"c:\program files (x86)\Athene\Data Management\Upgrade Database" DW-E5530,' + @DatabaseName
        EXEC master..xp_cmdshell @CMDSQL
 
        --Check Result of upgrade
        SET @CMDSQL = 'findstr /c:"The database has been updated" c:\programdata\metron\logs\*' + @DatabaseName + '*.log'
        EXEC @Result = master..xp_cmdshell @CMDSQL
 
        IF @Result = 0
            -- Success
            INSERT RR_UpgradeDatabase (RegressionRunID, DatabaseID, Result)
            VALUES (@RegressionRunID, @DatabaseID, 1)
        ELSE
            -- Failure
            INSERT RR_UpgradeDatabase (RegressionRunID, DatabaseID, Result)
            VALUES (@RegressionRunID, @DatabaseID, 0)
     
        FETCH NEXT FROM Database_cursor
        INTO    @DatabaseID,
                @DatabaseName,
                @CurrentLocation
 
    END
         
        CLOSE Database_cursor
        DEALLOCATE Database_cursor
 
END

17 Answers, 1 is accepted

Sort by
0
Boyan Boev
Telerik team
answered on 15 Jan 2014, 10:32 AM
Hi Amardeep,

Thank you for contacting us.

Here is how to access database via coded step.

Hope this helps.

Regards,
Boyan Boev
Telerik
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bryan
Top achievements
Rank 1
answered on 08 Oct 2014, 02:46 PM
Or, set up a test with no steps and bind it to your stored procedure. Then use the "Setup" test as a "Test as Step" in the first step of your dependent test.
0
Boyan Boev
Telerik team
answered on 13 Oct 2014, 06:57 AM
Hello Bryan,

Yes, you can put this coded step in a test as step.

Thank you for your input Bryan we appreciate it.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bryan
Top achievements
Rank 1
answered on 13 Oct 2014, 01:03 PM
Boyan,

The approach I suggested does not require the use of a coded step.
0
Boyan Boev
Telerik team
answered on 14 Oct 2014, 11:24 AM
Hi Bryan,

The coded step is needed for executing queries in the data base or modifying it.

Thank you.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bryan
Top achievements
Rank 1
answered on 14 Oct 2014, 12:10 PM
Boyan,

I believe you are mistaken. We are currently using a test with NO steps with a binding to a stored procedure that performs multiple deletes and creates to set up our environment. Please make sure that this functionality is not removed from Telerik as it is crucial to our testing efforts and we should not have to create a coded step to execute a stored procedure.
0
Boyan Boev
Telerik team
answered on 14 Oct 2014, 12:57 PM
Hi Bryan,

Please excuse me for misunderstanding.

You are absolutely correct and this functionality is not removed from Test Studio.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bryan
Top achievements
Rank 1
answered on 20 Jan 2015, 11:07 PM
Boyan,

I tried using the coded step as you suggested, but it times out before the stored procedure finishes running and forcefully closes the connection. Using the timeout in the connection string also had no effect.

​
////Define a new SQL connection with a connection string.
////The connection string will be different depending on your environment and the name of the database, table, etc.
////See http://www.connectionstrings.com for connection string examples.
SqlConnection thisConnection = new SqlConnection("Server=st_db;Database=QualityAssurance;"
                                             + " User Id=xxxxxxxxx; Password=xxxxxxxxx;"
                                             + " Connection Timeout=600");
thisConnection.Open();
 
////Write the name of the database to the log.
//Log.WriteLine(thisConnection.Database);
 
//Create a SQL command to insert a new value into the "City" column.
SqlCommand thisCommand = thisConnection.CreateCommand();
thisCommand.CommandText = "EXECUTE [QualityAssurance].[dbo].[CreateWPIData] "
                                   + "  @ApplicationName = 'Just' "
                                   + "  ,@ApplicationVersion = 'Delete' "
                                    + " ,@TestPlanName = 'It' "
                                    + " ,@TestSetName = 'All' "
                                    + " ,@DeleteIntegrationRecords  = 'Y' "
                                    + " ,@DeleteWrExpertDataClaims  = 'Y' "
                                    + " ,@ResetIntegrationCompany   = 'Y' "
                                + " ; ";
thisCommand.ExecuteNonQuery();
0
Boyan Boev
Telerik team
answered on 23 Jan 2015, 09:17 AM
Hi Bryan,

You don't need to use code when it works without a single row of code as you described in your initial post.

Could you please elaborate a bit more why do you use coded step?

Hope to hear from you soon.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bryan
Top achievements
Rank 1
answered on 23 Jan 2015, 01:51 PM
Boyan,

I get the timeout issue when it is used as a coded step and when it is used as a bound data set. All evidence points to Telerik forcefully closing the connection.
0
Boyan Boev
Telerik team
answered on 28 Jan 2015, 07:08 AM
Hello Bryan,

Could you please send us the application log so we can debug this further. Please clear it first and then reproduce the issue.

Hope to hear from you soon.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bryan
Top achievements
Rank 1
answered on 02 Feb 2015, 05:29 PM
Boyan,

I have included both the application log and the test log in the attached zip file.
Additionally, I created a new procedure that intentionally takes 5 minutes to complete to duplicate the issue in any database you desire. The code for this stored procedure is below.

​
-- =======================================
-- Author:      Bryan Boutwell
-- Create date: 2015-02-02
-- Description: Sample stored procedure that intentionally takes
--              5 minutes to complete to demonstrate Telerik
--              Test Studio issue with executing long running
--              stored procedures.
-- =======================================
 
CREATE PROCEDURE [dbo].TELERIK_TIMEOUT
AS
BEGIN
    WAITFOR DELAY '0:5:00';
END
GO
0
Boyan Boev
Telerik team
answered on 05 Feb 2015, 09:11 AM
Hi Bryan,

Thank you for the logs and the sample.

I will consult this with our developers and will update this forum thread accordingly.

Thank you for your cooperation.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Boyan Boev
Telerik team
answered on 09 Feb 2015, 01:35 PM
Hello Bryan,

Please excuse us for the delayed reply.

Since this exception is not coming from Test Studio (System.Data.SqlClient.SqlConnection.OnError) could you please send us a sample test project where we can reproduce behavior. 

How do you configure exactly the project?

What code you have in the coded step?

A Jing video would also help us.

Hope to hear from you soon.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Bryan
Top achievements
Rank 1
answered on 10 Feb 2015, 03:17 PM
Boyan,

To clarify, the error is actually NOT being thrown by SQL. The connection is being forcibly closed by Telerik. My assumption is that there is a timeout configuration in Telerik that is causing the connection to be forcibly closed even though the stored procedure is still running.

A sample project is attached. You will need to perform the following database steps on your end to make it run.
1 - create the stored procedure "TELERIK_TIMEOUT" in the database you want to run the test against.
-- =======================================
 
-- Author:      Bryan Boutwell
 
-- Create date: 2015-02-02
 
-- Description: Sample stored procedure that intentionally takes
 
--              5 minutes to complete to demonstrate Telerik
 
--              Test Studio issue with executing long running
 
--              stored procedures.
 
-- =======================================
 
  
 
CREATE PROCEDURE [dbo].TELERIK_TIMEOUT
 
AS
 
BEGIN
 
    WAITFOR DELAY '0:5:00';
 
END
 
GO

2 - update the connection string in the coded step to connect to the correct database.
0
Boyan Boev
Telerik team
answered on 13 Feb 2015, 02:46 PM
Hi Bryan,

Thank you for the project.

I was able to reproduce the issue and have logged a bug report about this. You can track and vote for it here.

I hope this will be fixed as soon as possible.

Thank you for you feedback.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
0
Boyan Boev
Telerik team
answered on 13 Feb 2015, 04:07 PM
Hi Bryan,

After further discussion about the issue we have discovered what causes it.

This is not a Test Studio bug. The .NET framework defaults to a 30 second timeout. You have to increase the timeout in the SqlCommand. See this article.

You should add this row after creating the command:

thisCommand.CommandTimeout = 500;

Let me know if you need further assistance.

Regards,
Boyan Boev
Telerik
 
The New Release of Telerik Test Studio Is Here! Download, install,
and send us your feedback!
Tags
General Discussions
Asked by
Amardeep
Top achievements
Rank 1
Answers by
Boyan Boev
Telerik team
Bryan
Top achievements
Rank 1
Bryan
Top achievements
Rank 1
Share this question
or