【问题标题】:How can I run the same Coded UI test with multiple input files如何使用多个输入文件运行相同的编码 UI 测试
【发布时间】:2014-09-25 14:38:48
【问题描述】:

我正在寻找一种能够使用不同的输入文件运行相同编码的 ui 测试类的方法,例如我在应用程序中有一个端到端流程的测试,我希望能够在两个不同的用户在应用程序内执行不同的工作流时运行这个测试。我不想每次都运行这两个测试(这可以在数据输入 csv 中有两行)。到目前为止,我无法找到这样做的方法。任何帮助/指导表示赞赏。

【问题讨论】:

  • 您是在寻找软件测试包解决方案(花钱),还是代码解决方案?我过去曾研究过前者并提出了 Ranorex (ranorex.com/?gclid=CICptb_V_MACFaTm7Aodrw4ASg)。
  • 谢谢。并不是真的在寻找像 Renorex 这样的付费解决方案。我想通过 C# 代码做到这一点。
  • 不同的输入文件是什么意思?测试数据是否被驱动并且您想使用两个不同的数据文件来驱动它?或者,您能否通过数据驱动测试并在数据源的两行中给出两个文件名?
  • 是的,测试是数据驱动的。我希望能够使用不同的数据文件运行相同的测试。将行保留在同一个输入文件中不是一种选择,因为我想一次只运行一个特定的测试。我在想我可以将与每个测试相关的数据保存在一个单独的文件中,并且可以以某种方式定义一种在运行之前将输入文件名传递给测试的方法。我不想在代码中保留数据文件名。希望这能更好地解释我的情况。

标签: c# automated-tests ui-automation coded-ui-tests


【解决方案1】:

我能想到三种可能性。


1.

您可以将 CSV 安排为具有两组列,例如

UserName1,Password1,DataAa1,DataBb1,UserName2,Password2,DataAa2,DataBb2

在测试方法中更改数据源访问以使用类似

string testCode = (...) ? "1" : "2";

... = TestContext.DataRow["UserName" + testCode].ToString();
... = TestContext.DataRow["Password" + testCode].ToString();

这需要其他东西来指定要使用的数据文件。这可以通过环境变量来完成。


2.

在解决方案中包含三个 CSV 文件。其中两个是两次运行的 CSV 文件。例如SourceData1.csvSourceData2.csv。第三个文件是SourceData.csv,在[DataSource(...) 属性中命名为"|DataDirectory|\\SourceData.csv"。在“.testsettings”文件中给出批处理文件的名称,该文件选择所需的SourceData1.csvSourceData2.csv 文件并使用xcopy 复制该文件并覆盖SourceData.csv


3.

假设测试当前写成

[TestMethod, DataSource(...)]
public void MyCodedUiTestMethod() {
    ...body of the test
}

然后更改为有两个调用第三种方法的测试方法。这两种方法指定不同的 CSV 文件,调用的方法从正在读取的文件中访问值。

[TestMethod, DataSource(... SourceData1.csv ...)]
public void MyFirstCodedUiTestMethod() {
    BodyOfTheTest();
}

[TestMethod, DataSource(... SourceData2.csv ...)]
public void MySecondCodedUiTestMethod() {
    BodyOfTheTest();
}

public void BodyOfTheTest() {
    ...body of the test

    ... = TestContext.DataRow["UserName"].ToString();
    ... = TestContext.DataRow["Password"].ToString();
}

注意TextContext 在类的所有方法中都是可见的,因此TestContext.DataRow... 表达式可以写在指定[DataSource...] 属性的方法之外。

【讨论】:

    【解决方案2】:

    如果是同一个测试用例,那么您应该有相同的输入参数集,使用您的测试参数创建一个类,然后将类实例列表序列化为具有不同参数集的 XML 文件。运行测试用例时,您可以反序列化 XML 文件(在 TestInitialize() 块内)并遍历每个类实例并传递实例编码的 UI 测试方法。您可以根据您在 xml 文件中的类实例的数量多次调用测试方法。这是我用于编码 UI 测试的数据驱动测试的方法。

    使用测试参数创建一个类

     public class ClientDetails
        {
            public String ClientType { get; set; }
            public String clientCode { get; set; }
            public String Username { get; set; }
            public String Password { get; set; }
        }
    

    创建一些类实例并首先序列化为 XML 文件

    // location to store the XML file following relative path will store outside solution folder with
    // TestResults folder
    string xmlFileRelativePath = "../../../TestClientInfo.xml";
    
    public List<ClientDetails> ListClientConfig = new List<ClientDetails>();
                ClientDetails Client1 = new Classes.ClientDetails();
                Client1.ClientType = "Standard";
                Client1.clientCode = "xxx";
                Client1.Username = "username";
                Client1.Password = "password";
    
       ClientDetails Client2 = new Classes.ClientDetails();
                Client2.ClientType = "Easy";
                Client2.clientCode = "xxxx";
                Client2.Username = "username";
                Client2.Password = "password";
    
    ListClientConfig.Add(Client1);
    ListClientConfig.Add(Client2);
    
    XmlSerialization.genericSerializeToXML(ListClientConfig, xmlFileRelativePath );
    

    在测试方法或您喜欢的任何地方检索存储的 XML 对象(最好在 TestInitialize() 块内)

     [TestMethod]
     public void CommonClientExecution()
     {
        List<ClientDetails> ListClientConfig = XmlSerialization.genericDeserializeFromXML(new     ClientDetails(), xmlFileRelativePath );
    
         foreach (var ClientDetails in ListClientConfig )
         {
           // you test logic here...
         }
    }
    

    用于序列化对象集合的 XML 序列化方法

    using System.Xml;
    using System.Xml.Serialization;
    
    class XmlSerialization
        {        
            public static void genericSerializeToXML<T>(T TValue, string XmalfileStorageRelativePath)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));           
                FileStream textWriter = new FileStream((string)System.IO.Path.GetFullPath(XmalfileStorageRelativePath), FileMode.OpenOrCreate, FileAccess.ReadWrite);
                serializer.Serialize(textWriter, TValue);
                textWriter.Close();
            }
    
    
            public static T genericDeserializeFromXML<T>(T value, string XmalfileStorageFullPath)
            {          
                T Tvalue = default(T);
                try
                {
                    XmlSerializer deserializer = new XmlSerializer(typeof(T));
                    TextReader textReader = new StreamReader(XmalfileStorageFullPath);
                    Tvalue = (T)deserializer.Deserialize(textReader);
                    textReader.Close();
    
                }
                catch (Exception ex)
                {
                    // MessageBox.Show(@"File Not Found", "Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                return Tvalue;
            }
        }
    

    【讨论】:

    • 谢谢塞尔瓦。您能否为我提供一些示例代码以开始此操作?我是 C# 新手,我认为我自己无法解决这个问题。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多