0

I'm running Selenium tests in C# and I'm looking for a way to externalize the list of tests to a text file so they can be easily edited without need to touch the code. The problem is how to then call each line of the text file as a method? Actions seem to be the best solution but I'm having trouble converting my text string to an Action. I'm open to all suggestions on how best to do this. Thanks, J.

Note: Even though using reflection and invoke was the solutions, it doesn't work when I have methods with different number of parameters.

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());
        }
    }

}
}
bearaman
  • 1,071
  • 6
  • 23
  • 43

1 Answers1

0

Imperfect But simplifies:

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} );
        }
    }
}
dovid
  • 6,354
  • 3
  • 33
  • 73