Script attempts to run then immediately stops, but provides no error msg

Asked by Ron Turrentine

When I click "Run" or "Run in slow motion", the SikuliX IDE attempts to run my script, but then it immediately stops. However it provides no error output - in fact, it provides no feedback whatsoever in the "Message" window.

This has been occurring all morning. I copied my script, then re-entered it line by line by hand. When I attempted to run it, it threw an error on a line where I had a typo. I corrected the typo, saved it, but now it's doing the "I'll give you no feedback whatsoever" thing again.

Could there perhaps be an issue with indentation? I have gone over the code many times, and it *looks* OK to me (my wife would roll her eyes, she knows how well I look at stuff). I know that I must be missing something. I wish the IDE would give me some sort of feedback on why it won't run, but it is not being helpful at all.

I should note that I have also reinstalled SikuliX just to be safe. Unfortunately that did not help.

The code is below. Any help/input is quite appreciated!

Thank you in advance,

Ron

#####################
import os
import shutil
import datetime

### Beginning Class PDA ###
class PDA(object):

    def __init__(self):
  self.BasePath = "c:\\screenshots"
  self.Browser = "Chrome"
  self.TestSite = "Test"
  self.TestType = "Default"
  self.SSCount = 0
  self.FolderDate = str(datetime.date.strftime(datetime.datetime.date(datetime.datetime.now()),'%m-%d-%y'))
  self.FullPath = ""
  self.SiteURL = "https://www.google.com/"

    def TakeScreenShot(self, path):
        #increment screenshot count
        self.SSCount += 1

        # cast the count to string
        strcount = str(self.SSCount)

        #format out filename using the SS count
        nextname = self.FormatSSName(strcount)

        # get our main full screen coords defined
        screen = (SCREEN.getX(), SCREEN.getY(), SCREEN.getW(), SCREEN.getH())

        # capture the defined screen
        img = capture(*screen)

        #put the pieces of our new filename into a list
        strjoin = (path, nextname)

        # concatenate the path with the filename (putting a backslash between them)
        newFile = "\\".join(strjoin)

        # save the screenshot
        shutil.copy(img, newFile)

    def FormatSSName(self, strcount):

        strPad = ""

        # Determine length of our SS Count, then prepend the appropriuate # of zeroes to its number and append a .png
        if len(strcount) == 1:
            strPad = "0000" + strcount
            strPad = strPad + ".png"
        elif len(strcount) == 2:
            strPad = "000" + strcount
            strPad = strPad + ".png"
        elif len(strcount) == 3:
            strPad = "00" + strcount
            strPad = strPad + ".png"
        elif len(strcount) == 4:
            strPad = "0" + strcount
            strPad = strPad + ".png"
        else:
            strPad = strcount + ".png"

        return strPad

    def SetupBrowser(self):

        #extract browser filename from PDA.Browser property
        browsername = os.path.basename(self.Browser)

        #Close all instances of the aforementioned browser
        closeApp(browsername)

        wait(5)

        #Define our browser object
        pdaBrowser = App(self.Browser)

        wait(2)

        #Open the browser
        pdaBrowser.open()

        wait(5)

        #Focus on the browser
        pdaBrowser.focus()

        wait(2)

        #Access the address bar
        type("l", KeyModifier.CTRL)

        wait(2)

        #Enter Site URL and navigate to it
        type(self.SiteURL + Key.ENTER)

### Ending Class PDA ###

### SET ENVIRONMENT ###
# Define PDA object
myPDA = PDA()

# define path, browser type, folder names and navigation URL
myPDA.BasePath = "C:\\screenshots\\sikulitest"
myPDA.Browser = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
myPDA.TestSite = "TestSite"
myPDA.TestType = "Default"
myPDA.SiteURL = "https://www.google.com"

backslash = "\\"
pathinfo = (myPDA.BasePath, myPDA.TestSite, myPDA.FolderDate, myPDA.TestType)

#Concatenate full path string
myPDA.FullPath = backslash.join(pathinfo)

#Store path in dir obj
myPath = os.path.dirname(myPDA.FullPath)

#Check if path exists. If not, create it
if not os.path.exists(myPath):
    os.makedirs(myPath)

#Setup our browser
myPDA.SetupBrowser

#Take a screenshot
myPDA.TakeScreenShot(myPath)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Ron Turrentine
Solved:
Last query:
Last reply:
Revision history for this message
Ron Turrentine (oldtimerocker) said :
#1

I made some big changes to my script and got it working after I stripped out the class and changed the class properties to global variables.

This is the working script, however I would love to get it working with the functions as methods within a class:

#######################

import os
import shutil
import datetime

def takescreenshot():

    # import global variables
    global sscount
    global mypath
    global formatssname

    # increment screenshot count
    sscount += 1

    # format out filename using the SS count
    nextname = formatssname()

    # get our main full screen coords defined
    screen = (SCREEN.getX(), SCREEN.getY(), SCREEN.getW(), SCREEN.getH())

    # capture the defined screen
    img = capture(*screen)

    # put the pieces of our new filename into a list
    strjoin = (mypath, nextname)

    # concatenate the path with the filename (putting a backslash between them)
    newFile = "\\".join(strjoin)

    # save the screenshot
    shutil.copy(img, newFile)

def formatssname():

    # import global variable
    global sscount

    strPad = ""

    strcount = str(sscount)

    # Determine length of our SS Count, then prepend the appropriuate # of zeroes to its number and append a .png
    if len(strcount) == 1:
        strPad = "0000" + strcount
    elif len(strcount) == 2:
        strPad = "000" + strcount
    elif len(strcount) == 3:
        strPad = "00" + strcount
    elif len(strcount) == 4:
        strPad = "0" + strcount

    strPad = strPad + '.png'

    return strPad

def setupbrowser():

    # import global variables
    global browser
    global siteurl

    # extract browser filename from PDA.browser property
    browsername = os.path.basename(browser)

    # Close all instances of the aforementioned browser
    closeApp(browsername)

    wait(5)

    # Define our browser object
    pdabrowser = App(browser)

    wait(2)

    # Open the browser
    pdabrowser.open()

    wait(5)

     #Focus on the browser
    pdabrowser.focus()

    wait(2)

    # Access the address bar
    type("l", KeyModifier.CTRL)

    wait(2)

    # Enter Site URL and navigate to it
    type(siteurl + Key.ENTER)

def createfolders():

    # import global variables
    global fullpath
    global mypath

    # Concatenate full path string
    fullpath = backslash.join(pathinfo)

    # Store path in dir obj
    mypath = os.path.dirname(fullpath)

    # Check if path exists. If not, create it
    if not os.path.exists(mypath):
        os.makedirs(mypath)

# Setup global variables and environment
basepath = "c:\\screenshots\\doesthiswork"
browser = "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
testsite = "Test"
testtype = "Default"
sscount = 0
folderdate = str(datetime.date.strftime(datetime.datetime.date(datetime.datetime.now()),'%m-%d-%y'))
fullpath = ""
siteurl = "https://www.google.com/"
backslash = "\\"
pathinfo = (basepath, testsite, folderdate, testtype)
mypath = ""

# Create our folders
createfolders()

# Setup our browser
setupbrowser()

# take screenshot
takescreenshot()

Revision history for this message
RaiMan (raimund-hocke) said :
#2

You should either work with an IDE, that allows real debugging (e.g. PyCharm) or add some print statements (e.g. at the end of def()'s reporting the result of the function.

You might also use the SikuliX debug options.

Currently I can see nothing in your script, that should report anything in the message area.

If there would be a syntax or indentation error, this would definitely be reported.

Simple questions:
- did the browser open?
- is a screenshot stored?

general recommendations:
- use the latest 1.1.3 nightly
- add some checks to your workflow, to assure things are going as you expect (e.g. wait for the Google image after having issued the load url)

One more thing: there is os.path.join, that simply allows to build paths from given parts. String concat should be avoided for that especially on Windows becaus of the double-backslash-trap.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

sorry, did not notice your comment #1.

I will later check about the class problem.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

using a class - this simply works:

class testClass():
  def __init__(self):
    self.var1 = "var1"
    print "init done"

  def aMethod(self, parm1):
    print "aMethod:", parm1
    self.var1 = parm1

test = testClass()
print "test.var1:", test.var1
test.aMethod("testing")
print "test.var1 (changed?):", test.var1

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#5

Hey RaiMan,

Thanks so much for you helpful input. In answer to your questions:

1.) The browser did not open
2.) No screenshots were taken
3.) I did have some helpful "print" commands at various points in my script, but I had stripped them out before uploading it here.
    a.) Nothing was printed in the Message area.
    b.) In fact, there was no output at all. The Message area was completely empty.
4.) I will definitely look into your os.path.join method. That sounds like the perfect solution.
    a.)The string concatenation was "not pretty", which is why I went with a list of strings along with the string.join method (it worked better than the "+" operator for more than 2 strings).
5.) I don't see anything in your class example that is different from what I was doing except for the fact that you did not have "object" between the parentheses (like so: class PDA(object):). Is that a problem?

I noticed that in my _init_ function that I pasted in my original post that the indentation was lost on the property lines. Perhaps there was something amiss with that? The other functions seemed to keep the indentation that they had from the IDE when I copied it.

Thanks again for your helpfulness!

Ron

Revision history for this message
RaiMan (raimund-hocke) said :
#6

--- (object) is no longer needed with newer Python levels 2.5+

Using latest SikuliX?

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#7

Raiman,

Thanks for the "object" info, did not realize that!

Regarding the version, I'm on 1.1.2. I notice that you mentioned 1.1.3 "nightly", but when I visited your downloads page (https://launchpad.net/sikuli/+download) - which is where I downloaded the IDE - I could not find a link to obtain 1.1.3.

Can you please share that URL here?

Thanks again!

Ron

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#8

PS: I'm on Windows 7 professional

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#9

Raiman,

I found your github page (https://github.com/RaiMan/SikuliX1/releases), but I'm not sure how to use the source code. I'm not a java developer. Is there a JAR installer available to install 1.1.3 on Windows?

Thanks!

Ron

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#10

I apologize for the multiple posts, but I have found the installer download page for the Nightly Build:

https://raiman.github.io/SikuliX1/nightly.html

I am running the setup now. I will let you know how it goes.

Thanks,

Ron

Revision history for this message
Ron Turrentine (oldtimerocker) said :
#11

I got 1.1.3 installed and working. Thanks Raiman.
And my script ran within it and is giving me error & info feedback in the Message area.

I appreciate your help!

Best Regards,

Ron

Revision history for this message
RaiMan (raimund-hocke) said :
#12

Thanks for feedback.
Keep going ;-)