0
votes

i want to return a string "Passed"From method Return Class Maintain, when the test passes, And string "Failed" when the Test fails,

What I am attempting to do is make public static void Return_Pass() more dynamic and write it once instead of writing it multiple times, by passing Passed or Failed int the T-SQL statment in the method public static void Return_Pass()

public class MainTain
{
    public static void Return()
    {
        try
        {
            Thread.Sleep(5000);
            Database.Return();           
            var icon = Browser.Driver.FindElement(By.XPath("//label[contains(text(), 'Search:')]"));
            icon.SendKeys("TEST8");
            Thread.Sleep(1000);                
            Browser.Driver.FindElement(By.XPath("//button[@title='Edit user']")).Click();    
            Thread.Sleep(500);
            Browser.Driver.FindElement(By.XPath("//button[.= 'Return']")).Click();
            Thread.Sleep(500);                
            Maint_Tests_Pass.Return_Pass();
            Console.WriteLine(P);           
        }
        catch (Exception e)
        {

            Browser.Closed();
            Maint_Tests_Fail.Return_Fail();
            Environment.Exit(-1);
        }
    }
}

To method Return_Pass Class Maint_Tests_pass

    public class Maint_Tests_Pass
{

    public static void Return_Pass()
    {
        string connetionString;
        SqlConnection cnn;

        connetionString = "Server=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\marco zani\\Documents\\marco.mdf\";Integrated Security=True;Connect Timeout=30";
        cnn = new SqlConnection(connetionString);
        SqlCommand command;
        string sql = "INSERT INTO TestRun (Date,Message) VALUES (GETDATE(),'Test Return Passed')";
        command = new SqlCommand(sql, cnn);

        cnn.Open();
        command.ExecuteReader();
        cnn.Close();
        cnn.Dispose();
    }
}

i am fairly new to C# any help is appreciated..

method return_pass now

public static void Return_Pass(bool pass, string test)
    {
        string connetionString;
        SqlConnection cnn;

        connetionString = "Server=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\marco zani\\Documents\\marco.mdf\";Integrated Security=True;Connect Timeout=30";
        cnn = new SqlConnection(connetionString);
        SqlCommand command;
        string passValue = pass ? "Passed" : "Failed";
        string testreturn = test;
        string sql = $"INSERT INTO TestRun (Date,Message) VALUES (GETDATE(),'{test} {passValue}')";
        //string sql = "INSERT INTO TestRun (Date,Message) VALUES (GETDATE(),'Test Return Passed')";
        command = new SqlCommand(sql, cnn);

        cnn.Open();
        command.ExecuteReader();
        cnn.Close();
        cnn.Dispose();
    }

return method now

 public static void Return()
    {
        try
        {
            Thread.Sleep(5000);
            Database.Return();           
            var icon = Browser.Driver.FindElement(By.XPath("//label[contains(text(), 'Search:')]"));
            icon.SendKeys("TEST8");
            Thread.Sleep(1000);                
            Browser.Driver.FindElement(By.XPath("//button[@title='Edit user']")).Click();    
            Thread.Sleep(500);
            Browser.Driver.FindElement(By.XPath("//button[.= 'Return']")).Click();
            Thread.Sleep(500);                
            Maint_Tests_Pass.Return_Pass(true,"Test Return");                        
        }
        catch (Exception e)
        {

            Browser.Closed();
            Maint_Tests_Pass.Return_Pass(false, "Test Return");
            Environment.Exit(-1);
        }
    }
3

3 Answers

0
votes

use string instead of void then return your value

public static string Return_Pass()
{

...

return "yourvalue";
}
0
votes

Based on my understanding of the question

public static void Return_Pass(bool pass)
{
    // ...
    // ...
    string passValue = pass ? "Passed" : "Failed";
    string sql = $"INSERT INTO TestRun (Date,Message) VALUES (GETDATE(),'Test Return {passValue}')";
    // ...
    // ...
}
0
votes

Rather than having two separate functions, have one that takes a boolean and inserts the appropriate record.

public static void Record(bool passed)
{
    string connetionString;
    SqlConnection cnn;

    connetionString = "Server=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\\Users\\marco zani\\Documents\\marco.mdf\";Integrated Security=True;Connect Timeout=30";
    cnn = new SqlConnection(connetionString);
    SqlCommand command;
    string sql = $"INSERT INTO TestRun (Date,Message) VALUES (GETDATE(),'Test Return {(passed ? "Passed" : "Failed"}')";
    command = new SqlCommand(sql, cnn);

    cnn.Open();
    command.ExecuteReader();
    cnn.Close();
    cnn.Dispose();
}

And call thusly:

try
{
    // as before
    Maint_Tests_Pass.Record(true);
}
catch(Exception ex)
{
    Maint_Tests_Pass.Record(false);
}

Now, it's beyond the scope of both your question and this answer, but when you've got this working, how about looking at parameterised SQL queries:

https://www.completecsharptutorial.com/ado-net/insert-records-using-simple-and-parameterized-query-c-sql.php

Have fun!