/// <summary> /// Method to add a variable to a TestState. This is useful for adding vars during /// a test routine in C# that is used in the xml script. Long term it will be beter /// to have the script interact with the teststate and get values directly. /// </summary> /// <param name="name">id of the variable</param> /// <param name="val">value for the variable</param> public void AddVariable(string name, string val) { TestState ts = null; // local if (!varsDefined) ts = TestState.getOnly(m_appSymbol); // sets a global singleton // a new Var creates a Logger that needs a TestState to get the script path varsDefined = true; Var var = new Var(); var.Id = name; var.Set = val; m_vars.Add(var); // // have to execute the instruction to get it added to the TestState // var.Execute(); }
public void ParseVarTest() { TestState ts = TestState.getOnly("LT"); ts.Script = "UtilitiesTest.xml"; Var ins2 = new Var(); ts.AddNamedInstruction("one",ins2); ins2.Set = "4"; ins2.Execute(); Assert.IsTrue(GuiPath.parseIndexTesting("stuff[$hi]", "stuff[$hi]", 0, null)); Assert.IsTrue(GuiPath.parseIndexTesting("stuff[$one]", "stuff", 4, null)); Assert.IsTrue(GuiPath.parseIndexTesting("stuff3[$one;3]", "stuff3", 43, null)); Assert.IsTrue(GuiPath.parseIndexTesting("stuff[3$one]", "stuff[3$one]", 0, null)); }
/// <summary> /// Creates a variable to be referenced by other instruction attributes. /// </summary> /// <param name="xn">The XML repersentation of the instruction to be checked</param> /// <param name="con">The current context object</param> public static Var CreateVar(XmlNode xn, Context con) { Var var = new Var(); var.Id = XmlFiler.getAttribute(xn, "id"); Logger.getOnly().isNotNull(var.Id, "Var instruction must have a id."); Logger.getOnly().isTrue(var.Id != "", "Var instruction must have a non-empty id."); string s = XmlFiler.getAttribute(xn, "set"); // select should be move to var.execute so it is dynamic! string select = XmlFiler.getAttribute(xn, "select"); if (select != null && select != "") { // the variable can only have one text node or one other node assigned to it. XmlNodeList pathNodes = selectNodes(con, select, "var"+var.Id); Logger.getOnly().isNotNull(pathNodes, "var " + var.Id + " select='" + select + "' returned no result"); if (pathNodes.Count > 0) { // append first node to set string XmlNode modNode = pathNodes.Item(0); // which property of the node to get? string prop = null; string propName = XmlFiler.getAttribute(xn, "prop"); if (propName == null && modNode is XmlElement) propName = "path"; if (propName == null) propName = "value"; if (propName != null && propName == "value") prop = XmlPath.ResolveModelPath(modNode, modNode.Value); if (propName != null && propName == "name") prop = modNode.Name; if (propName != null && propName == "type") prop = modNode.NodeType.ToString(); if (propName != null && propName == "path") { XmlPath xp = new XmlPath(modNode); if (xp.isValid()) prop = xp.Path; else prop = null; } s += prop; } else s += "#NoSelection#"; } var.Set = s; string when = XmlFiler.getAttribute(xn, "when"); string add = XmlFiler.getAttribute(xn, "add"); if (add != null) var.Add = add; if (var.Set == null && when == null) { // if there is a select/when then don't complain if the select found nothing Logger.getOnly().isNotNull(var.Add, "Var " + var.Id + @" set, select or add must result in a string or number value unless when=""exists"" is set."); if (select != null && select != "") var.Set = @"#not-"+when+@"#"; } string exists = XmlFiler.getAttribute(xn, "file-exists"); if (exists != null) var.FileExists = exists; string rest = XmlFiler.getAttribute(xn, "wait"); if (rest != null) var.Rest = Convert.ToInt32(rest); AddInstruction(xn, var, con); return var; }
public void evalFunction() { TestState ts = TestState.getOnly("LT"); ts.Script = "UtilitiesTest.xml"; SelectText ins1 = new SelectText(); ts.AddNamedInstruction("one", ins1); ins1.Path = "window:cleaner/item:dust"; // note: ins1 has no default data set (ie. $one.text = null) Var ins2 = new Var(); ts.AddNamedInstruction("_two", ins2); ins2.Set = " how's this??"; Var ins3 = new Var(); ts.AddNamedInstruction("thr-ee", ins3); ins3.Set = "And $_two; this should be ignored?"; ins2.Execute(); ins3.Execute(); string result = Utilities.evalExpr("$random()"); Assert.IsTrue(result == "0" || result == "1", "default random not 0 or 1"); result = Utilities.evalExpr("$random(,)"); Assert.IsTrue(result == "$random(,)", "random can't just have a comma for an argument"); result = Utilities.evalExpr("$random(,4)"); Assert.IsTrue(result == "$random(,4)", "random has to have a max argument"); result = Utilities.evalExpr("$random(4,)"); Assert.IsTrue(result == "$random(4,)", "random has to have a min argument when there's a comma"); result = Utilities.evalExpr("$random(45,50)"); Assert.IsTrue(result == "$random(45,50)", "min has to be less than max"); result = Utilities.evalExpr("$random(50,45)"); Assert.IsTrue(result == "45" || result == "46" || result == "47" || result == "48" || result == "49" || result == "50", "Random must be between 45 and 50"); result = Utilities.evalExpr("$random(-6,-10)"); Assert.IsTrue(result == "-6" || result == "-7" || result == "-8" || result == "-9" || result == "-10", "Random must be between -10 and -6"); result = Utilities.evalExpr("$random(3,-2)"); Assert.IsTrue(result == "-2" || result == "-1" || result == "0" || result == "1" || result == "2" || result == "3", "Random must be between -2 and 3"); }
public void evalExpr() { TestState ts = TestState.getOnly("LT"); ts.Script = "UtilitiesTest.xml"; SelectText ins1 = new SelectText(); ts.AddNamedInstruction("one",ins1); ins1.Path = "window:cleaner/item:dust"; // note: ins1 has no default data set (ie. $one.text = null) Var ins2 = new Var(); ts.AddNamedInstruction("_two",ins2); ins2.Set = " how's this??"; Var ins3 = new Var(); ts.AddNamedInstruction("thr-ee",ins3); ins3.Set = "And $_two; this should be ignored?"; ins2.Execute(); ins3.Execute(); string result = Utilities.evalExpr("$one"); Assert.AreEqual(null,result); result = Utilities.evalExpr("$one;"); Assert.AreEqual(null,result); result = Utilities.evalExpr("$one "); Assert.AreEqual(" ",result); result = Utilities.evalExpr(" $one"); Assert.AreEqual(" ",result); result = Utilities.evalExpr("$one; "); Assert.AreEqual(" ",result); result = Utilities.evalExpr(";$one; "); Assert.AreEqual("; ",result); result = Utilities.evalExpr(";$one;;"); Assert.AreEqual(";;",result); result = Utilities.evalExpr("$one1"); Assert.AreEqual("$one1",result); result = Utilities.evalExpr("$on;e"); Assert.AreEqual("$on;e",result); result = Utilities.evalExpr("$_two"); Assert.AreEqual(" how's this??",result); result = Utilities.evalExpr("$_two;"); Assert.AreEqual(" how's this??",result); result = Utilities.evalExpr("$_two "); Assert.AreEqual(" how's this?? ",result); result = Utilities.evalExpr(" $_two"); Assert.AreEqual(" how's this??",result); result = Utilities.evalExpr("$_two; "); Assert.AreEqual(" how's this?? ",result); result = Utilities.evalExpr(";$_two; "); Assert.AreEqual("; how's this?? ",result); result = Utilities.evalExpr(";$_two;;"); Assert.AreEqual("; how's this??;",result); result = Utilities.evalExpr("$_two1"); Assert.AreEqual("$_two1",result); result = Utilities.evalExpr("$_tw;o"); Assert.AreEqual("$_tw;o",result); result = Utilities.evalExpr("$one.;"); Assert.AreEqual(null,result); result = Utilities.evalExpr("$one..;"); Assert.AreEqual("[select-text-1 does not have data for '.']",result); result = Utilities.evalExpr("$one.path"); Assert.AreEqual("window:cleaner/item:dust",result); result = Utilities.evalExpr("$one.path;"); Assert.AreEqual("window:cleaner/item:dust",result); result = Utilities.evalExpr("$one.path.;"); Assert.AreEqual("[select-text-1 does not have data for 'path.']",result); result = Utilities.evalExpr("text$one;$_two;$thr-ee"); Assert.AreEqual("text how's this??And how's this?? this should be ignored?",result); result = Utilities.evalExpr("text$one;$_two;$thr-ee"); Assert.AreEqual("text how's this??And how's this?? this should be ignored?",result); result = Utilities.evalExpr("text$one.path;$_two;$thr-ee OK"); Assert.AreEqual("textwindow:cleaner/item:dust how's this??And how's this?? this should be ignored? OK",result); result = Utilities.evalExpr("text $_two $one.path OK"); Assert.AreEqual("text how's this?? window:cleaner/item:dust OK",result); }
/// <summary> /// The search is almost breadth-first: /// The first step in the path has an unknown sibling index and its role is not 'value'. /// If this ah has children of the step type, they are searched until all the /// subsequent steps find matching ah's. This search substitutes its own visitor /// that records the successful path ah's. Upon success, a sibling index is assigned to /// the variable named in the step, the non-terminal subpath ah's are exposed /// to the caller's visitor in turn and this method returns the last ah on the path. /// Subpath steps are searched for depending on their content. /// </summary> /// <param name="path">The path to search. Each step in the path contains an /// Accessibility name and Accessibility role of the matching gui object.</param> /// <param name="visitor">null or the object providing methods that are called when steps are found.</param> /// <returns>A new <see cref="AccessibilityHelper"/> object that wraps the matched /// window.</returns> private AccessibilityHelper SearchPathForIndex(GuiPath path, IPathVisitor visitor) { Logger log = Logger.getOnly(); log.startList("SearchPathForIndex('" + path.toString() + "') starting from ('" + this.Role + "','" + this.Name + "')"); if (ChildCount <= 0) { if (visitor != null) visitor.notFound(path); log.endList(); return null; } AccessibilityHelper ah = null; int nnum = 0; int index = 0; string parentName = null; if (path.Prev != null) parentName = path.Prev.Name; foreach (AccessibilityHelper child in this) { if (child == null) continue; string logStr = "-> Child(" + (++nnum) + ") ('" + child.Role + "','" + child.Name + "')"; if (path.Next != null && path.Next.Role == AccessibleRole.Alert) logStr += "NOT SUPPOSED TO BE VALUE!! val[" + child.Value + "]"; log.listItem(logStr); if (MatchNameAndRole(path.Name, path.Role, child)) { // this is a candidate for matching the path ++index; // the first one is 1 if (path.Next != null) { // This method won't allow for caller's visitors on candidate nodes until proved needed TrackingVisitor tv = new TrackingVisitor(); // tv may need to open sub menus if (tv != null) tv.visitNode(child); // add child to node list to visit later if this is the right path ah = ValueOrChild(path.Next, child, tv); if (ah != null) { // the subpath was matched to the end if (path.VarId != null && path.VarId != "") { // Create and execute a variable to record the index Var var = new Var(); var.Id = path.VarId; var.Set = System.Convert.ToString(index); // 1 based count var.Execute(); // puts the var in the TestState hash // don't set path.Nth = index since it might change if the path is used again } // let the caller's visitor tend to all the path ah's if (visitor != null) { if (tv != null) tv.StepDownPath(visitor); } log.endList(); return ah; } } } else if (parentName != null && child.Role == AccessibleRole.Client && child.Name == parentName) { // try the client instead ah = child.SearchPathForIndex(path, visitor); if (ah != null) { log.endList(); return ah; // found it } } } if (visitor != null) visitor.notFound(path); log.endList(); return null; }
public void PublishVars() { if (m_scriptPath == null) m_scriptPath = getScriptPath(); Var sp = new Var(); sp.Id = "ScriptPath"; sp.Set = m_scriptPath; sp.Execute(); // sets and interprets the value and adds to the hash }
/// <summary> /// Overriden in this class to set context variables available to child instructions. /// $guiPath, $appPath, $guiRole and $guiName /// </summary> protected override void SetContextVariables() { Var guiPath = new Var("guiPath", m_path); Var appPath = new Var("appPath", m_select); Var guiRole = new Var("guiRole", m_role); Var guiName = new Var("guiName", m_name); Var nodeName = new Var("nodeName", m_nodeName); guiPath.Execute(); appPath.Execute(); guiRole.Execute(); guiName.Execute(); nodeName.Execute(); }
/// <summary> /// Read instruction file. /// Interpret script nodes according to tne instruction file. /// Make the class but don't process its child instructions if any. /// Don't execute the instruction. /// </summary> /// <param name="xn">The XML repersentation of the instruction to be checked</param> /// <param name="con">The current context object</param> /// <returns>Returns an unexecuted instruction or null</returns> public static Instruction MakeShell(XmlNode xn, Context con) { Logger.getOnly().isNotNull(xn, "Instruction can't be created from nothing!"); if (m_actPrototypes == null) { Logger.getOnly().fail("Can not create: No instruction prototypes loaded."); return null; } Instruction FluffedInstruction = null; // number the context if it doesn't have one yet to avoid depth-first numbering. if (con != null && con.Number == -1) con.Number = TestState.getOnly().IncInstructionCount; // figure out what to do with this node switch (xn.Name) { case "#comment": // ignore comments, etc.. case "#significant-whitespace": case "#whitespace": case "#text": break; default: // Find the instruction prototype based on node name. InsPrototype prototype = findPrototype(xn.Name, m_actPrototypes); if (prototype != null) { var AtValues = new ArrayList(); ArrayList atts = prototype.Attributes; Logger.getOnly().startList("Instruction " + prototype.Name); XmlAttributeCollection atNodes = xn.Attributes; if (atNodes != null && atts != null) { foreach (XmlAttribute atx in atNodes) { // find each attribute in the prototype string atValue = null; foreach (Attribute at in atts) { if (at.Name == atx.Name) { // its this one atValue = XmlFiler.getAttribute(xn, at.Name); if (atValue != null && at.Name != "log") { // log is dealt with in AddInstruction() var atVar = new Attribute(at.Name, atValue, at.Value); AtValues.Add(atVar); Logger.getOnly().listItem(" " + atx.Name + "=" + atValue); break; } } } if (atValue == null) { // This attribute is not expected: make it a variable // Add it as a var instruction so it gets bound at the right time // Use <var id="atx.Name" set="atValue"/> var var = new Var(); var.Id = atx.Name; var.Set = XmlFiler.getAttribute(xn, atx.Name); // Add the var to the growing list of instructions AddInstruction(xn, var, con); Logger.getOnly().paragraph("Added <var id=\"" + var.Id + "\" set=\"" + var.Set + "\"/>"); } } } Logger.getOnly().endList(); // Create the instruction using prototype.Name, AtValues.Name and AtValues.Value string protoName = XmlNameToCName(prototype.Name); string protoNameQ = prefix + protoName; Assembly assem = Assembly.GetExecutingAssembly(); // All instruction classes must have empty constructors Object protoInstruction = null; try { protoInstruction = assem.CreateInstance(protoNameQ, true, BindingFlags.CreateInstance, null, null, null, null);} catch (Exception e) { Logger.getOnly().fail("Instruction " + protoName + " not created: " + e.Message); } Logger.getOnly().isNotNull(protoInstruction, "Instruction " + protoName + " is DOA"); FluffedInstruction = (Instruction)protoInstruction; foreach (Attribute at in AtValues) { // Call each setter to set the instruction properties. int number = 0; UInt32 unsigned = 0; string primative = "string"; if (at.Type == "msec" || at.Type == "int" || at.Type.Contains("[0-10]")) { try { number = Convert.ToInt32(at.Value); primative = "int"; } catch (FormatException) { } } if (at.Type == "m-sec" || at.Type == "hz") { try { unsigned = Convert.ToUInt32(at.Value, 10); primative = "UInt32"; } catch (FormatException) { } } if (primative == "string" && at.Type.Contains("|")) { string[] enumList = makeEnumList(at.Type); foreach (string value in enumList) { if (value == at.Value) { primative = value; break; } } if (primative == "string") Logger.getOnly().fail("Invalid enum {" + at.Value + "} for " + protoNameQ + "." + at.Name + "(" + at.Type + ")"); } string propName = XmlNameToCName(at.Name); string propNameQ = protoNameQ + "." + XmlNameToCName(at.Name); PropertyInfo pi = null; MethodInfo m = null; try { if (primative == "int") { pi = assem.GetType(protoNameQ).GetProperty(propName, typeof(int)); m = pi.GetSetMethod(); m.Invoke(protoInstruction, new Object[] { number }); } else if (primative == "UInt32") { pi = assem.GetType(protoNameQ).GetProperty(propName, typeof(UInt32)); m = pi.GetSetMethod(); m.Invoke(protoInstruction, new Object[] { unsigned }); } else { Type t = assem.GetType(protoNameQ); pi = t.GetProperty(propName, typeof(string)); m = pi.GetSetMethod(); m.Invoke(protoInstruction, new Object[] { at.Value }); } } catch { Logger.getOnly().fail(" Can't find setter: " + protoNameQ + "." + propName + "(" + at.Type + ") using value {" + at.Value + "}"); } if (at.Name == "id" && protoName != "Var") TestState.getOnly().AddNamedInstruction(at.Value, FluffedInstruction); } // end of process attributes // Call the finishCreation method FluffedInstruction.finishCreation(xn, con); //if (prototype.Name != "if" && // prototype.Name != "then" && prototype.Name != "else") // Add the instruction to the growing list of instructions AddInstruction(xn, FluffedInstruction, con); } else { bool unknown = true; if (m_pasPrototypes != null) { InsPrototype IgnoredPrototype = findPrototype(xn.Name, m_pasPrototypes); if (IgnoredPrototype != null) unknown = false; } if (unknown) Logger.getOnly().fail("Can't make <" + xn.Name + "> instruction"); } break; } return FluffedInstruction; }
public void PublishVars() { Var sp = new Var(); // sp.finishCreation() not needed sp.Id = "ScriptPath"; sp.Set = getScriptPath(); sp.Execute(); // sets and interprets the value and adds to the hash Var ep = new Var(); // ep.finishCreation() not needed ep.Id = "AppPath"; ep.Set = getAppPath(); ep.Execute(); // sets and interprets the value and adds to the hash Var mp = new Var(); // mp.finishCreation() not needed mp.Id = "ModelPath"; mp.Set = getModelPath(); mp.Execute(); // sets and interprets the value and adds to the hash }
/// <summary> /// Read instruction file. /// Interpret script nodes according to tne instruction file. /// Make the class but don't process its child instructions if any. /// Don't execute the instruction. /// </summary> /// <param name="xn">The XML repersentation of the instruction to be checked</param> /// <param name="con">The current context object</param> /// <returns>Returns an unexecuted instruction or null</returns> static public Instruction MakeShell(XmlNode xn, Context con) { Logger.getOnly().isNotNull(xn, "Instruction can't be created from nothing!"); if (m_actPrototypes == null) { Logger.getOnly().fail("Can not create: No instruction prototypes loaded."); return null; } Instruction FluffedInstruction = null; // number the context if it doesn't have one yet to avoid depth-first numbering. if (con != null && con.Number == -1) con.Number = TestState.getOnly().IncInstructionCount; // figure out what to do with this node switch (xn.Name) { case "#comment": // ignore comments, etc.. case "#significant-whitespace": case "#whitespace": case "#text": break; default: // Find the instruction prototype based on node name. InsPrototype prototype = findPrototype(xn.Name, m_actPrototypes); if (prototype != null) { var AtValues = new ArrayList(); ArrayList atts = prototype.Attributes; Logger.getOnly().startList("Instruction " + prototype.Name); XmlAttributeCollection atNodes = xn.Attributes; if (atNodes != null && atts != null) { foreach (XmlAttribute atx in atNodes) { // find each attribute in the prototype string atValue = null; foreach (Attribute at in atts) { if (at.Name == atx.Name) { // its this one atValue = XmlFiler.getAttribute(xn, at.Name); if (atValue != null && at.Name != "log") { // log is dealt with in AddInstruction() var atVar = new Attribute(at.Name, atValue, at.Value); AtValues.Add(atVar); Logger.getOnly().listItem(" " + atx.Name + "=" + atValue); break; } } } if (atValue == null) { // This attribute is not expected: make it a variable // Add it as a var instruction so it gets bound at the right time // Use <var id="atx.Name" set="atValue"/> var var = new Var(); var.Id = atx.Name; var.Set = XmlFiler.getAttribute(xn, atx.Name); // Add the var to the growing list of instructions AddInstruction(xn, var, con); Logger.getOnly().paragraph("Added <var id=\"" + var.Id + "\" set=\"" + var.Set + "\"/>"); } } } Logger.getOnly().endList(); // Create the instruction using prototype.Name, AtValues.Name and AtValues.Value string protoName = XmlNameToCName(prototype.Name); string protoNameQ = prefix + protoName; Assembly assem = Assembly.GetExecutingAssembly(); // All instruction classes must have empty constructors Object protoInstruction = null; try { protoInstruction = assem.CreateInstance(protoNameQ, true, BindingFlags.CreateInstance, null, null, null, null);} catch (Exception e) { Logger.getOnly().fail("Instruction " + protoName + " not created: " + e.Message); } Logger.getOnly().isNotNull(protoInstruction, "Instruction " + protoName + " is DOA"); FluffedInstruction = (Instruction)protoInstruction; foreach (Attribute at in AtValues) { // Call each setter to set the instruction properties. int number = 0; UInt32 unsigned = 0; string primative = "string"; if (at.Type == "msec" || at.Type == "int" || at.Type.Contains("[0-10]")) { try { number = Convert.ToInt32(at.Value); primative = "int"; } catch (FormatException) { } } if (at.Type == "m-sec" || at.Type == "hz") { try { unsigned = Convert.ToUInt32(at.Value, 10); primative = "UInt32"; } catch (FormatException) { } } if (primative == "string" && at.Type.Contains("|")) { string[] enumList = makeEnumList(at.Type); foreach (string value in enumList) { if (value == at.Value) { primative = value; break; } } if (primative == "string") Logger.getOnly().fail("Invalid enum {" + at.Value + "} for " + protoNameQ + "." + at.Name + "(" + at.Type + ")"); } string propName = XmlNameToCName(at.Name); string propNameQ = protoNameQ + "." + XmlNameToCName(at.Name); PropertyInfo pi = null; MethodInfo m = null; try { if (primative == "int") { pi = assem.GetType(protoNameQ).GetProperty(propName, typeof(int)); m = pi.GetSetMethod(); m.Invoke(protoInstruction, new Object[] { number }); } else if (primative == "UInt32") { pi = assem.GetType(protoNameQ).GetProperty(propName, typeof(UInt32)); m = pi.GetSetMethod(); m.Invoke(protoInstruction, new Object[] { unsigned }); } else { Type t = assem.GetType(protoNameQ); pi = t.GetProperty(propName, typeof(string)); m = pi.GetSetMethod(); m.Invoke(protoInstruction, new Object[] { at.Value }); } } catch { Logger.getOnly().fail(" Can't find setter: " + protoNameQ + "." + propName + "(" + at.Type + ") using value {" + at.Value + "}"); } if (at.Name == "id" && protoName != "Var") TestState.getOnly().AddNamedInstruction(at.Value, FluffedInstruction); } // end of process attributes // Call the finishCreation method FluffedInstruction.finishCreation(xn, con); //if (prototype.Name != "if" && // prototype.Name != "then" && prototype.Name != "else") // Add the instruction to the growing list of instructions AddInstruction(xn, FluffedInstruction, con); } else { bool unknown = true; if (m_pasPrototypes != null) { InsPrototype IgnoredPrototype = findPrototype(xn.Name, m_pasPrototypes); if (IgnoredPrototype != null) unknown = false; } if (unknown) Logger.getOnly().fail("Can't make <" + xn.Name + "> instruction"); } break; } return FluffedInstruction; }