Need to close 'save as' window in e2e test. SendKey using C#

187 views Asked by At

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:

0

There are 0 answers