/// <summary> /// Pre-test setup: /// Create and initialize FaultSession /// Set environment variables /// </summary> public override void Before(MethodInfo methodUnderTest) { string processorArch = DetectProccessorArchitecture(); ComRegistrar.Register(@".\FaultInjectionEngine\" + processorArch + @"\FaultInjectionEngine.dll"); FaultSession session = new FaultSession(FaultInjectionTestData.FaultRules); ProcessStartInfo startInfo = session.GetProcessStartInfo("notepad.exe"); SetLocalEnvironmentVariables(startInfo); }
public void FaultAppendText() { //This is a work-around, Xunit moves the reference dll's at run time so we need to call register //from the test, instead of TestApiCore. string processorArch = DetectProccessorArchitecture(); ComRegistrar.Register(@".\FaultInjectionEngine\" + processorArch + @"\FaultInjectionEngine.dll"); string sampleAppPath = "SampleApp.exe"; //Create the FaultRule and FaultSession FaultRule rule = new FaultRule( "SampleApp.Window1.Append(string, string)", BuiltInConditions.TriggerOnEveryCall, BuiltInFaults.ReturnValueFault("")); FaultSession session = new FaultSession(rule); //Start the app ProcessStartInfo psi = session.GetProcessStartInfo(sampleAppPath); OutOfProcessApplication testApp = new OutOfProcessApplication( new OutOfProcessApplicationSettings { ProcessStartInfo = psi, ApplicationImplementationFactory = new UIAutomationOutOfProcessApplicationFactory() }); testApp.Start(); try { testApp.WaitForMainWindow(TimeSpan.FromSeconds(15)); // Discover various elements in the UI AutomationElement mainWindowElement = (AutomationElement)testApp.MainWindow; AutomationElement inputTextBox = AutomationUtilities.FindElementsById(mainWindowElement, "inputTextBox")[0]; AutomationElement outputTextBox = AutomationUtilities.FindElementsById(mainWindowElement, "outputTextBox")[0]; AutomationElement appendButton = AutomationUtilities.FindElementsById(mainWindowElement, "appendButton")[0]; // Click on the input text box and simulate typing string inputText = "TestTest"; string expectedText = ""; //expected text should be nothing since we intercept the Append method WindowPattern winPattern = mainWindowElement.GetCurrentPattern(WindowPattern.Pattern) as WindowPattern; Helpers.MoveToAndClick(inputTextBox); winPattern.WaitForInputIdle(inputWaitTime); Microsoft.Test.Input.Keyboard.Type(inputText); winPattern.WaitForInputIdle(inputWaitTime); // Now click the button Helpers.MoveToAndClick(appendButton); winPattern.WaitForInputIdle(inputWaitTime); // Now, get the text of the outputTextBox and compare it against what's expected // Fail the test if expected != actual TextPattern textPattern = outputTextBox.GetCurrentPattern(TextPattern.Pattern) as TextPattern; string actualText = textPattern.DocumentRange.GetText(-1); // Report the test result Assert.Equal <string>(expectedText, actualText); } finally { // Close the tested application testApp.Close(); } }