c# - Specflow: to call a variable in a class, using a string, without a switch -
this test automation using c# , specflow (cucumber c#)
i have class urls this:
class paths { public static string google_home = "www.google.com"; public static string another_url = "www.something.com"; public static string facebook_home = "www.facebook.com"; }
and have step definition this:
[then(@"i navigate ""(.*)""")] public void inavigateto (string url) { configuration.driver.navigate().gotourl(paths.url); }
of course scrip doesnt work, want explain idea. using step definition this:
i navigate "google_home"
i obtain string "google_home", , want use string directly choose option paths.google_home. dont want use switch assignation, since there hundreds of urls.
edit: linked solution this:
var values = typeof(paths).getfields(); configuration.driver.navigate().gotourl(values[0].getvalue(null).tostring());
i have no idea of knowing number of array "values" corresponds string looking for. such dont consider solution.
its not difficult do:
public static class paths { public static string path1 = ""; public static string path2 = ""; } [attributeusage(attributetargets.method, allowmultiple = false)] public class navigationtargetattribute : attribute { public string target { get; set; } public navigationtargetattribute(string target) { target = target; } } public class mynavigationclass { [navigationtarget(paths.path1)] public void mynavigatemethod() { string target = getnavigationtarget(); } private string getnavigationtarget([callermembername]string caller = null) { object[] attribs = this.gettype() .getmethod(caller) .getcustomattributes( typeof(navigationtargetattribute), false); if (attribs == null) return "some default target"; return ((navigationtargetattribute)attrib[0]).target; } }
i wouldn't name then
, do. custom attribute set decorate methods, , not allow multiple copies of same attribute.
the getnavigationtarget
uses c#4 feature called callermembername
returns name of method calling function, located in system.runtime.compilerservices
namespace.
there things want check, example valid navigation target. i'm not sure string in example can add transformation functions in attribute if want.
Comments
Post a Comment