1

I need to implement e2e automated test of printing to file as emulation of printing process using GUI. I have

  1. Web application running under selenium and nunit. It presses Print button.
  2. Printing application installed on computer. It prints data from web server to user's printer or to file (default XPS Windows printer) for test needs.
  3. Automated e2e test process using dedicated server with installed TFS agent as foreground console application ran "as Administrator".
  4. I'm logged off, test run on CI server by scheduler.

I have to: Handle the "Save File as" window which has to save a printed xps file (or pdf if pdf printer will be used):

  1. Set the file name. I use SendKyes.SendWait(myFileName)
  2. Press OK button. SendKyes.SendWait("{ENTER}")

    private void SaveFileIfRequired(int waitWindowTimeoutSeconds)
    {
        const string saveDialogWindowName = "Save Print Output As";
    
        bool isSaveDialogShown = WasWinFromWindowShown(saveDialogWindowName, waitWindowTimeoutSeconds/3);
        Logger.Debug($"{nameof(isSaveDialogShown)} : {isSaveDialogShown}");
    
        if (isSaveDialogShown)
        {
            string fileNameToSave = @"autmationTestPrintedFile_" + DateTime.Now.Ticks;
            string filePathToSave = Path.Combine(DefaultTestData.GetPathToPrintedLabels(), fileNameToSave).ToLower();
    
            Logger.Trace($"{nameof(filePathToSave)} : {filePathToSave}");
    
            var windowPointer = FindWindow(null, saveDialogWindowName);
            int foreground = SetForegroundWindow(windowPointer);
            // foreground  0 when logged off and 
    
            Logger.Trace($"windowPointer: {windowPointer}, foreground {foreground}");
    
    
            const int closeWindowsDelayMiliseconds = 1000;
    
            //SendKeys.SendWait(@"^+{LEFT}{BACKSPACE}");
            SendKeys.SendWait("^{HOME}"); // Move to start of control
            SendKeys.SendWait("^+{END}"); // Select everything
            SendKeys.SendWait("{DEL}");
            SendKeys.Flush();
            Thread.Sleep(closeWindowsDelayMiliseconds/2);
            SendKeys.SendWait(filePathToSave);
            SendKeys.Flush();
            Thread.Sleep(closeWindowsDelayMiliseconds/2);
            SendKeys.SendWait(@"{ENTER}");
            SendKeys.Flush();
    
    
            VerifyWindowIsClosed(saveDialogWindowName);
        }
    

The problem is next:

If I logged in to dedicated server (via RDP) where test is running, all is fine and the file will be saved, because window will be handled.

If I logged off from server, the SetForeground will return 0 and file willn't be saved, and window will be opened.

I know about UAC limitations for Windows 7+. UAC was disabled at all. We use Windows server 2012. I need some workaround. Maybe infrom UAC about this operation, change Register, maybe some method or library...

Of course I use

<add key="SendKeys" value="SendInput"/>

in settings


update

Looks like it's not a UAC problem, but GUI-free mode problem when log off from RDP machine or minimize rdp window. windows locks desktop and set gui-free mode without active windows

Details: tests via remote desktop

One of solutions - keep RDP connection while testing GUI

Also possible variants to minimize RDP window and even try not to lock while disconnect:

Artem A
  • 2,154
  • 2
  • 23
  • 30
  • Is there any reason why you aren't using [UI Automation](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx)? It was invented, among others, to enable automated testing of GUIs. – IInspectable Dec 23 '16 at 00:20
  • 1. I don't use it because mostly it's a web application. I use selenium and protractor. The only one moment i need interact with GUI is this print file functionality. Also i'm not familiar with that framework, while i'm developer 2. The problem was found at a very last setp. Can UI Automation help to avoid problem with GUI-free mode while log off ? – Artem A Dec 23 '16 at 00:49
  • `1` That's not a reason not to use UI Automation. `2` Since UI Automation doesn't require foreground activation, it is likely a more robust solution, especially under those circumstances. – IInspectable Dec 23 '16 at 00:55
  • Maybe. It will be resonable to use it, if we know for sure, that it doesn't require to be logged in (for CI) as in article about TestComplete framework. Their arguments and examples looks reasonable. 1. I worry, that GUI testing even in Ui automation will not work in non-gui mode of desktop while run from CI server. Am I wrong? 2. Can I use Ui automation from nunit test? – Artem A Dec 23 '16 at 12:08
  • I don't know, if UI Automation works in GUI-less mode. But if anything will, I'd first evaluate whether the de-facto standard for automation does. [UI Automation](https://msdn.microsoft.com/en-us/library/ms747327.aspx) is exposed to the .NET framework, so it should be perfectly reasonable to use it from NUnit tests. – IInspectable Dec 23 '16 at 12:19

0 Answers0