18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to close PDF window

sumansuman Members
edited November -1 in Sahi - Open Source
Hi,
From my application it will open a new window, in which it will render a PDF file.
When I try to hover on this window sahi controler is not displaying any Window name.

I hope sahi controller will run on browser window whose context type is Text/HTML

Problem:
When I trying to close open window(which contains PDF) using _closeWindow(), it is closing parent window.

could you please anyone help me closing newly open window.

Comments

  • narayannarayan Administrators
    Hi Suman,

    Sahi will work on windows which have html content in it. If you are opening a PDF, it may not be possible to close it, unless you open the pdf in an iframe inside another window. You could try calling an external program to close it though.

    (You can try using AutoIt or some simple VBScript to close it)

    Regards,
    Narayan
  • sumansuman Members
    Hi narayan,

    Could you please elobrate on this, and also explain me how to call VBScript from sahi.

    Thanks in advance.
  • beesmanbeesman Members
    edited August 2010
    closePdf.vbs
    Option Explicit
    
    Dim objIE
    Dim objShell
    
    Set objShell = CreateObject("Shell.Application")
     For Each objIE In objShell.Windows
    '    WScript.Echo objIE.LocationURL
        If InstrRev(LCase(objIE.LocationName), ".pdf") > 0 Then
            objIE.Quit
        End if
     next
    
    Set objIE = Nothing
    Set objShell = Nothing
    

    put closePdf.vbs to %sahi_home%\bin

    _execute("wscript closePdf.vbs", true);
    or
    _execute("cscript closePdf.vbs", true);


    console
    Executing: wscript closePdf.vbs
    or
    Executing: cscript closePdf.vbs

    then IE dead
    OS: XP
    BROWSER: IE8
    SAHI 20100820
  • beesmanbeesman Members
    edited August 2010
    i solved the problem

    closePdf.vbs
    Option Explicit
    Dim objIE
    Dim objShell
    Dim objParm
    Set objParm = Wscript.Arguments
    If objParm.Count = 1 Then
        Set objShell = CreateObject("Shell.Application")
         For Each objIE In objShell.Windows
            If InstrRev(LCase(objIE.LocationName), objParm(0)) > 0 Then
                objIE.Quit
            End if
         next
    End if
    Set objIE = Nothing
    Set objShell = Nothing
    
    

    AppWindow.java
        public static void closeWindowByTitle(String title){
            try {
                executeSystemCommand("wscript closePdf.vbs " + title, true);            
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        private static String executeSystemCommand(final String command, final boolean isSynchronous) throws InterruptedException {
            String cmd = "";
            try {
                cmd = command; // getCommandForOS(command);
                System.out.println("Executing: " + cmd);
                Process process = Runtime.getRuntime().exec(Utils.getCommandTokens(cmd));
                return (isSynchronous) ? getExitStatus(process) : SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return FAILURE;
            }
        }
    
        private static String getExitStatus(final Process process) throws InterruptedException {
            return (NORMAL_TERMINATION == process.waitFor()) ? SUCCESS : FAILURE;
        }
        
        private static final int NORMAL_TERMINATION = 0;
        public static final String SUCCESS = "success";
        public static final String FAILURE = "failure";
    

    Packages.AppWindow.closeWindowByTitle(".pdf");
  • narayannarayan Administrators
    Beesman,

    You are a rock star!

    Cheers!
    Narayan
  • hi suman

    Have you solved the problem?

    http://114.18.133.218/post-801-1.fhtml
Sign In or Register to comment.