我在C#中运行Selenium测试,我正在寻找一种方法将测试列表外部化为文本文件,这样就可以轻松编辑它们而无需触摸代码。 问题是如何将文本文件的每一行调用为方法?动作似乎是最好的解决方案,但我在将文本字符串转换为Action时遇到问题。 我对所有关于如何做到最好的建议持开放态度。 谢谢, 学家
注意:尽管使用反射和调用是解决方案,但是当我的方法具有不同数量的参数时,它不起作用。
using McAfeeQA.Core.Log;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using System;
namespace SeleniumProject
{
class Program
{
static void Main(string[] args)
{
try
{
// Read through all tests and run one after another
// Global.Tests is a list of strings that contains all the tests
foreach (string testcase in Global.tests)
{
// How to run each of the below commented methods in a foreach loop???
}
//Tests.CreateNewAdminUser("admin123", "password", "admin");
//Navigation.Login(Global.language, Global.username, Global.password);
//Tests.testPermissionSets();
//Navigation.Logoff();
}
catch (Exception ex)
{
Console.WriteLine("Program exception: " + ex.ToString());
}
}
}
}
答案 0 :(得分:0)
不完美但简化:
private void StoreMetod(string FileName, Type classType)
{
using (var fileSt = new System.IO.StreamWriter(FileName))
{
foreach (var Method in classType.GetType().GetMethods())
{
fileSt.Write(Method.Name);
fileSt.Write("\t");
fileSt.Write(Method.ReturnType == null ? "" : Method.ReturnType.FullName );
fileSt.Write("\t");
foreach (var prm in Method.GetParameters())
{
//ect...
}
}
}
}
private void LoadMethod(string FileName, Object instant)
{
using (var fileSt = new System.IO.StreamReader (FileName))
{
while (!fileSt.EndOfStream)
{
var lineMethodArg = fileSt.ReadLine().Split('\t');
var methodName = lineMethodArg[0];
var typeReturnName = lineMethodArg[1];
//set parameters, Return type Ect...
var objectReturn = instant.GetType().GetMethod(methodName).Invoke(instant, {prms} );
}
}
}