Logo QF-Test

Finding valuable answers
in the Mailing List Archive.

 

Free Trial  Download  Buy

Thomas Max, QF-Test training and support

Use the full-text search on our web site to find helpful tips on the mailing list.

Thomas Max, 
Sr. Software Engineer & Trainer, QFS

2016 up to now | 2015 | 2014 | 2013 | 2012 | 2011 | 2010 | 2009 | 2008 | 2007

(older archive entries before 2007 are not shown here, but included in the onsite-search)

Mailing List - Entries of 2007


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [QF-Test] How to fetch an URL with Python


  • Subject: Re: [QF-Test] How to fetch an URL with Python
  • From: "Robert Lahmer, QFS support" <support@?.de>
  • Date: Wed, 11 Jul 2007 11:42:21 +0200


Hello René,

in case you're working with the Internet Explorer on Windows, the following Server script should help you:


import autowin

browserTitleRe = ".*Microsoft Internet Explorer.*"
childTextRe = "(about|file|http).*"
childClass= "Edit"

url = ""
browserWnd = autowin.findWindow(browserTitleRe, 1)
if browserWnd != None:
children = autowin.findChildWindows(browserWnd, clazz=childClass, name=childTextRe, regexp=1)
   if len(children) > 0:
       url = autowin.getWindowText(children[0])
   else:
       rc.logError("Address window not found.")
else:
   rc.logError("Browser window not found.")

rc.setLocal("url", url)


The script needs the autowin module, so please copy the attached file autowin.py into your qftest/jython folder or replace the file in qftest/qftest-x.y.z/jython/Lib, if it already exists.

If the script fails to fetch the URL from your browser, you maybe should play a little bit with the values of browserTitleRe, childTextRe, and childClass respectively. For example, with the settings from above you won't get a ftp:// address. If necessary, you can also try to set childTextRe or childClass to None.


Best regards,
   Robert


p.s.: Unfortunately autowin is (currently ???) not able to fetch the URL from the Firefox browser. If anybody wants to do this ...




--On Dienstag, Juli 10, 2007 09:57:44 +0200 René Hegeman <Rene.Hegeman@?.nl> wrote:

Hi,

When clicking the help function of our Java swing application, a browser
opens and displays a web page. I would like to use python in a QF-Test
server script to check if the browser is opened, and what page is opened
(by fetching the URL).
Does someone know if and how this is possible with Python?

Greetings,

René Hegeman
Planon B.V.
The Netherlands




--
Robert Lahmer
Quality First Software GmbH                http://www.qfs.de
Tulpenstr. 41                           Tel: +49 8171 919874
DE-82538 Geretsried                     Fax: +49 8171 919876
GF: Gregor Schmid, Karlheinz Kellerer     HRB München 140833
#-------------------------------------------------------------------------------------------------
# autowin.py
# version 1.0
#
# Collection of simple methods for Windows test automation based on Jython and jniwrapper.
#-------------------------------------------------------------------------------------------------

import re
from java.lang import System, Thread
from com.jniwrapper.win32 import Msg
from com.jniwrapper.win32.ui import Wnd

from com.jniwrapper import Str
from com.jniwrapper import Pointer


#-------------------------------------------------------------------------------------------------
# Global variables
#-------------------------------------------------------------------------------------------------
_poll = 500                             # poll interval for wait... methods

#-------------------------------------------------------------------------------------------------
# Public methods
#-------------------------------------------------------------------------------------------------
# {{{ getWindowText

def getWindowText(win):
    """Get the text of a window (does work not only for titles).
    @param   win   The window handle.
    @return  The text of the window."""
    buflen = 1024
    res = Str(buflen)
    dataPtr = Pointer(res)
    handle = Pointer.Void()
    dataPtr.asVoidPointer(handle)
    l = win.sendMessageEx(Msg.WM_GETTEXT, buflen, handle.getValue())
    return res.getValue()
#

# }}}
# {{{ findWindow

def findWindow(name, regexp=0):
    """Find a window on the desktop indentified by its title.
    @param      name    The window title
    @param      regext  True if the given title is a regular expression.
    @return     A handle for the window or None if not found."""
    if regexp:
        reg = re.compile(name)
    wins = Wnd.getAllWindows()
    for w in wins:
        if w.isVisible():
            wname = getWindowText(w)
            if regexp and reg.match(wname):
                return w
            if (not regexp) and name == wname:
                return w
#

# }}}
# {{{ waitForWindow

def waitForWindow(timeout, name, regexp=0):
    """Find for a window on the desktop identified by its title.
    @param      timeout The maximum time to wait (in ms).
    @param      name    The window title
    @param      regext  True if the given title is a regular expression.
    @return     A handle for the window or None if the timeout is exceeded."""
    return _waitFor(timeout, findWindow, name, regexp)
#

# }}}
# {{{ findChildWindow

def findChildWindow(parent, name, regexp=0):
    """Find a child window of a given window, indentified by some text.
    @param      parent  A handle for the parent window.
    @param      name    The identifying text.
    @param      regext  True if the given text is a regular expression.
    @return     A handle for the child window or None if not found."""
    if regexp:
        reg = re.compile(name)
    wins = Wnd.getChildWindows(parent)
    for w in wins:
        if w.isVisible():
            wname = getWindowText(w)
            if regexp and reg.match(wname):
                return w
            if (not regexp) and name == wname:
                return w
#

# }}}
# {{{ findChildWindows

def findChildWindows(parent, name=None, regexp=0, clazz=None):
    """Find child windows of a given window, indentified by text and class name.
    @param      parent  A handle for the parent window.
    @param      name    The identifying text.
    @param      regexp  True if the given text is a regular expression.
    @param      clazz   The name of the child window's class.
    @return     A list of child window handles."""
    children = []
    if regexp:
        reg = re.compile(name)
    wins = Wnd.getChildWindows(parent)
    for w in wins:
        if w.isVisible():
            wname = getWindowText(w)
            cname = w.getWindowClassName()
            match = 1
            if name != None:
                match = match and (regexp and reg.match(wname)) or ((not regexp) and name == wname)
            if clazz != None:
                match = match and (cname == clazz) 
            if match:
                children.append(w)
    return children
#

# }}}
# {{{ waitForChildWindow

def waitForChildWindow(timeout, parent, name, regexp=0):
    """Wait for a child window of a given window, indentified by some text.
    @param      timeout The maximum time to wait (in ms).
    @param      parent  A handle for the parent window.
    @param      name    The identifying text.
    @param      regext  True if the given text is a regular expression.
    @return     A handle for the child window or None if not found."""
    return _waitFor(timeout, findChildWindow, parent, name, regexp)
#

# }}}
# {{{ doClick

def doClick(win, x, y):
    """Simulate a mouse click on a window.
    @param      win     A handle for the window.
    @param      x       Target X coordinate.
    @param      y       Target Y coordinate."""
    win.sendMessage(Msg.WM_LBUTTONDOWN, x, y)
    win.sendMessage(Msg.WM_LBUTTONUP, x, y)
#

# }}}
# {{{ closeWindow

def closeWindow(win):
    """Close a window by sending it the WM_CLOSE message.
    @param      win     A handle for the window."""
    win.sendMessage(Msg.WM_CLOSE, 0, 0)
#

# }}}
# {{{ typeKey

def typeKey(win, keycode):
    """Simulate a key press and release on a window.
    @param      win     A handle for the window.
    @param      keycode The keycode for the key."""
    lparam = 1
    win.sendMessage(Msg.WM_KEYDOWN, keycode, lparam)
    lparam = 1 | 1 << 30 | 1 << 31
    win.sendMessage(Msg.WM_KEYUP, keycode, lparam)
#

# }}}

#-------------------------------------------------------------------------------------------------
# Helper methods
#-------------------------------------------------------------------------------------------------
# {{{ _waitFor

def _waitFor(timeout, proc, *args, **kw):
    now = System.currentTimeMillis()
    until = now + timeout
    print "until:", until
    while 1:
        ret = apply(proc, args, kw)
        print "ret:", ret
        if ret:
            return ret
        rest = until - System.currentTimeMillis()
        print "rest:", rest
        if rest <= 0:
            return None
        # try:
        Thread.sleep(min(rest, _poll))
        # except:
            # # Interrupted
            # return None
#

# }}}

#{{{ local vars

# 
# Local Variables:
# gim-program: "telnet"
# gim-send-limit: 80000
# eval: (setq gim-arguments (list "192.168.2.11" "18180"))
# eval: (local-set-key "\M-R" '(lambda () (interactive) (gim-send-string "rel()\n")))
# End:

#}}}

Videos Downloads Documentation Buy Free Trial