← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #258406]: How to handle a Firefox session on Windows

 

Question #258406 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/258406

RaiMan proposed the following answer:
I set up your environment on my Windows 8 64
- Eclipse Luna
- Pydev 3.9
- SikuliX 1.1.0 in c:\SikuliX (sikulixapi.jar, sikulix.jar, ...)
- Jython 2.7b3 installed in c:\SikuliX\jython\jython27

In the properties of a Pydev project I have these two pointers in the PYTHOPATH external libraries section:
c:\SikuliX\sikulixapi.jar
c:\SikuliX\Lib

having these settings
from sikuli import *
is sufficient to access the Sikuli features and run a script successfully.

For the images to be used for searching I used the Sikuli-IDE, to capture some images in a images.sikuli stored in the project folder.
To access these images in script at the beginning one needs
addImagePath("images.sikuli")

so the folder structure of ma test project looks like this:
-- testpy (folder)
   ... the property files
   -- images.sikuli (folder)
      ... the image files
      images.py
  main.py (the script to run)

I have revised the FF functions:

this is the main.py:

from sikuli import *
addImagePath("images.sikuli")

def ffDefine(ffN="Mozilla Firefox", ffLoc=r"C:\Program Files (x86)\Mozilla Firefox\firefox.exe"):
  global ffProps
  ffProps = {}
  ffProps["name"] = ffN
  ffProps["loc"] = ffLoc
  ffProps["app"] = App(ffN)
  return ffProps["app"]

def ffStart():
  App.open(ffProps["loc"])
  return ffGetWindow()

def ffGetWindow():
  ff = App(ffProps["name"])
  ffWin = None
  for i in range(3):
    if ff.window(0) == None:
      wait(1)
      continue
    for i in range(100):
      ffWin = ff.window(i)
      if ffWin == None:
        break
      if ffWin.w < 200:
        continue
      break
    break
  return ffWin

def ffStop():
  ffName = ffProps["name"]
  if ffGetWindow():
    App.focus(ffName)
    while ffGetWindow():
      App.focus(ffName)
      write("#C.w#w1.") # = type("w", Key.CTRL); wait(1)
  App.close(ffName)

def ffGoto(url, waitFor=None, waitTime=10):
  ffWin = ffGetWindow()
  if ffWin:
    write("#C.l#w500."); paste(url); write("#N.")
    # = type("l", Key.CTRL); wait(0.5); paste(url); type(Key.ENTER)
    if waitFor:
      if not ffWin.exists(waitFor, waitTime):
        return None
      return ffWin.getLastMatch()
  return ffWin

# ***** usage

ffApp = ffDefine() # mandatory: define the FF browser
ffStop()  # stop a current FF session to get a clean base
ffStart()  # start FF
ffGetWindow().highlight(2) # show the window

aURL = "http://www.sikulix.com";
imgURLReady = "nightly.png";
found = ffGoto(aURL, imgURLReady)  # open the given url and wait for the image imgURLReady (default 10 secs)
if not found:
  print "could not open url:", aURL
  exit(1)
found.highlight(2) # show the image on the page

# do something

ffStop()  # optional

I used the new write() function instead of type()

*** undefined variable for wait, paste, write and other non-dotted functions
-- You either have to live with this problem (at runtime the functions are dynamically bound to the default screen object SCREEN (usually Screen(0), so the static code analyzer cannot know it)
-- or you list the function names to be ignored (to be considered as global) in the global Pydev prefs Editor -> Code Analysis -> Undefined
-- or you do not use the undated version, but a dotted one
S0 = Screen(0) (once at the beginning)
... and then
S0.wait(1)
S0.write("#C.l#w500.")
...

You may download a zipped version of my Eclipse Pydev project
https://dl.dropboxusercontent.com/u/42895525/EclipsePydev/EclipsePydevWindowsTestpy.zip

You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.