C#方法无法打印正确的值

时间:2011-06-13 17:06:20

标签: c# variables selenium

我在下面有两个方法General和SearchTab。当我查看我的报告文件时,我看到常规方法的值为:

测试名:

TestMachine:Maya

TestUser:管理员

TestTime:6/13/2011 12:02

TestStatus:FAIL

TestExpectedResult:

TestActualResult:

TestComments:

TestName,TestExpectedResult,TestActualResult和TestComments是空白的,但它们应该有值:

TestName:测试1:常规,

TestExpectedResult:'Home'选项卡应该存在,

TestActualResult:找到主页标签

TestComments:找到主页标签

此外,TestStatus应该是PASS而不是FAIL。

看起来即使我在方法General中重新分配了这些变量的值,它们仍然会打印在构造函数中分配给它们的值。

第二种方法SearchTab的问题是相同的。

请帮我解决问题。

namespace Automation
{
[TestClass]
public class FunctionalTest
{
    public ISelenium Sel;
    public StringBuilder Err;
    public string Report = "C:\Report.XLS";
    public string[] arrTestResults = new string[8];
    public string TestName;
    public string TestMachine;
    public string TestUser;
    public string TestTime;
    public string TestStatus;
    public string TestExpectedResult;
    public string TestActualResult;
    public string TestComments;

    // Constructor
    public FunctionalTest()
    {

        TestName = arrTestResults[0];
        TestMachine = arrTestResults[1] = System.Environment.MachineName.ToString();
        TestUser = arrTestResults[2] = System.Environment.UserName.ToString();
        TestTime = arrTestResults[3] = System.DateTime.Now.ToString();
        TestStatus = arrTestResults[4] = "FAIL";
        TestExpectedResult = arrTestResults[5];
        TestActualResult = arrTestResults[6];
        TestComments = arrTestResults[7];
    }

    public void WriteReport(string[] arrResults)
    {
        int iLastRow, iCnt1;

        if (File.Exists(Report))
        {
            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            Excel.Range range;

            Object misValue = System.Reflection.Missing.Value;
            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Add(misValue);
            xlWorkBook = xlApp.Workbooks.Open(Report, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            iLastRow = range.Rows.Count;
            for (iCnt1 = 1; iCnt1 < 9; iCnt1++)
            {
                xlWorkSheet.Cells[iLastRow + 1, iCnt1] = arrResults[iCnt1 - 1];
            }
            xlWorkBook.Save();
            xlApp.Quit();
            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }
        else
        {
            Console.WriteLine("Report File " + Report + " does not exist");
        }
    }

    [TestMethod]
    public void General()
    {
        TestName = "Test 1: General";
        TestExpectedResult = "'Home' tab should be present";
        if (Sel.IsElementPresent("TAB_Home")))
        {

                TestStatus = "PASS";
                TestActualResult = "Home tab found";
                TestComments = TestActualResult;
        }
            else
            {
                TestActualResult = "Home tab not found";
                TestComments = TestActualResult;

            }

            //Write to report
            WriteReport(arrTestResults);
        }
    }

    [TestMethod]
    public void SearchTab()
    {
        TestName = "Test 2: Search Tab";
        TestExpectedResult = "Search tab should be present";
        // Assigning TestStatus to FAIL because TestStatus is PASS right now from the previous test method
        TestStatus = "FAIL";

        if (Sel.IsElementPresent("TAB_Search"))
        {
            sActual = "Search Tab found";
            arrTestResults[7] = sActual;
            TestComments = TestActualResult;
            TestStatus = "PASS";
        }
        else
        {
            TestActualResult = "Search tab not found";
            TestComments = TestActualResult;
        }

        //Write to report
        WriteReport(arrTestResults);
    }


}
}

2 个答案:

答案 0 :(得分:1)

不要在构造函数或General方法中赋值。根据您的代码,General似乎是一个测试案例。如果是测试用例,那么不要指望General应首先调用,因为你将其定义为第一种方法。

使用SetupTest()设置值。请注意,SetupTest()不是测试用例,但会在执行每个测试用例之前调用它。

不要打印,提醒警报..

[SetUp]
public void SetupTest()
{
  ...your initialization code...
}

答案 1 :(得分:1)

您的初始化会创建初始数组值,而不会更改它们。您正在更改字段值:

string TestName = "test";

TestName = arrTestResults[0];

// At this point, changing TestName does not affect arrTestResults[0]

此外,您不应使用公共字段。相反,你应该使用属性。

public string TestName { get; set; }  // auto-property

OR

private string _testName = string.Empty;
public string TestName { get { return _testName; } set { _testName = value; }}

您需要为更改数组的属性编写setter,或者更好地在报告中使用相应的字段/属性:

xlWorkSheet.Cells[iLastRow + 1, 0] = TestName;

当然,这也可以重构得更好。