[1.0.1] help using App.focus() from inside a class

Asked by rob

im trying to make a simple controller for ie9 that i can use for testing and parsing information from webpages. the idea is that i would use the controller class to control a browser window and call webpage classes that have methods for parsing info from the webpage source html. i have 2 methods in the class, getAdr and getSrc. getAdr uses ctrl-l,ctrl-c to get the text in the address bar. getSrc uses alt-v,c to view source, copies the text and then closes the source window. they both work individually. but if i call getAdr and then getSrc, the focus doesnt change and the browser window gets closed instead of opening the source window. i suspect it has something to do with the way i defined the class and/or methods, because i have functions that do exactly the same thing and they work as expected.
here is the code:

from sikuli import *
import re
from org.sikuli.basics.proxies import Vision
Vision.setParameter("MinTargetSize",6)
mmd = Settings.MoveMouseDelay
Settings.MoveMouseDelay = 0.01

class Gui(object):
    '''base class'''
class GuiElem(object):
    '''base class'''
class Window(Gui):
    '''base class'''
class Browser(Window):
    '''base class for ie9 controller'''
    def getSource(self):
        '''copies source from the active tab to the system clipboard.
        uses keyboard shortcuts internally.'''
        raise NotImplementedError
    def open(self):
        raise NotImplementedError
    def switchTo(self):
        raise NotImplementedError
    def close(self):
        raise NotImplementedError
class Ie9(Browser):
    '''controller for Ie9. internally uses keyboard shortcuts to control the browser.'''
    def __init__(self):
        '''initialize a new Ie9 obj'''
        self.inst = App(' - Windows Internet Explorer')
    def getAdr(self):
        '''stores address bar contents using system clipboard'''
        self.inst.focus()
        type('l',KeyModifier.CTRL)
        type('c',KeyModifier.CTRL)
        self.adrVal = Env.getClipboard()
        text = open(r'adr.txt', 'r+') #for testing
        print >> text, self.adrVal #for testing
        text.close() #for testing
        return self.adrVal
    def getSrc(self):
        '''copy source from the active tab using system clipboard.
        stores the source in a Page obj'''
        self.inst.focus()
        type('v',KeyModifier.ALT)
        type('c')
        wait(0.4)
        type('a',KeyModifier.CTRL)
        type('c',KeyModifier.CTRL)
        wait(0.4)
        type('w',KeyModifier.CTRL)
        self.src = Env.getClipboard()
        text = open(r'src.txt', 'r+') #for testing
        print >> text, self.src #for testing
        text.close() #for testing
        return self.src
if __name__ == '__main__':
    browser = Ie9()
    browser.getAdr()
    browser.getSrc()

any help or suggestions would be greatly appreciated.

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

App.focus() always brings the windows of the app to front, but depending on the window structure (browsers are critical in this section), the window you expect might not have the input focus.

In some cases you simply have to wait a little before firing actions after focus.

If nothing else helps, you have to click the window in an inactive are, to give it the input focus (App: focusedWindow() will return the frontmost window of all active apps)

Can you help with this problem?

Provide an answer of your own, or ask rob for more information if necessary.

To post a message you must log in.