How to provide user input to sikuli script from a file

Asked by Sathyamoorthy on 2012-07-25

Hi,

I have to pass several inputs to sikuli script, like username, IP address, Machine name, OS name. So its another painful activity to type each value in input window.
So I want to pass these values from a file. Please anyone give me steps to do that

Thanks in Advance

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Sathyamoorthy
Solved:
2012-09-06
Last query:
2012-09-06
Last reply:
2012-07-26
RaiMan (raimund-hocke) said : #1

the basics:

--- make a comma separated text file like

user1,192.168.0.1,themachine,Windows 7
user2,192.168.0.2,themachine2,Windows 7

and so on

--- read the file and get the records/fields

fname = "absolute path to textfile"
f = open(fname)

for line in f.readlines()
    (user, ip, machine, system) = line.strip().split(",")
    print "current:" user, ip, machine, system
    # put your code here

f.close()

pyCoder (validcode-ye) said : #2

http://www.jython.org/docs/library/configparser.html

#inside application

config = ConfigParser.ConfigParser()
config.read('config.ini')
username = config.get('settings','username')
ipAddress = config.get('settings','ipAddress')

inside 'config.ini'

[settings]
username = Bob Marley
ipAddress = 190.943.1

RaiMan (raimund-hocke) said : #3

@ pyCoder
good finding - thanks. I will make it a faq.

But you missed the needed
import ConfigParser
at the beginning.

BTW: for the given case this is not the best solution, since he wants to input more than one option set with the same structure but different content.
This is more a case for a CSV solution, which might be supported by the Python CSV module.

pyCoder (validcode-ye) said : #4

refined.. your right RaiMan, it's depends on what op needs... if it's like a hundered entries csv method might be better. i took it as a config type. basically if you have thousands of entries use the csv module. but it's achevable as shown below.

CSV http://www.jython.org/docs/library/csv.html

Configparser http://www.jython.org/docs/library/configparser.html

#inside application, configparser example

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('config.ini')
username1 = config.get('user1','username')
username2 = config.get('user2','username')

#inside 'config.ini'

[user1]
user = Guido Van Rossum
ip = 192.168.0.1
machineName= Guido's Machine
system = Ubuntu

[user2]
user = Bob Marley
ip = 192.168.0.1
machineName= Bob's Machine
system = windows 7

pyCoder (validcode-ye) said : #5

Edit:
username1 = config.get('user1','username')

should be

username1 = config.get('user1','user')

i hate how this sight doesn't allow edits.

RaiMan (raimund-hocke) said : #6

--- i hate how this sight doesn't allow edits.
Do not ask RaiMan how he hates it ;-)

pyCoder (validcode-ye) said : #7

lmao *site ugh.....

Thanks for the answers .. If I have to pass values to for loop also how to do that .

Problem solved

Shafiq Khan (srk8887) said : #10

def UserName():
    config = ConfigParser.ConfigParser()
    myFilePath = os.path.join(getBundlePath(),'settings.ini')
    print "My config file is located at: " + myFilePath

    with open(myFilePath,'r') as configfile:
        config.readfp(configfile)
        return config.get('user1','user')

print UserName()

Now, on to encrypting password on config file...

panadol007 (panadol007) said : #11

None of above solutions help in my MAC.
But the simple solution is to give the config file a full path, then it works:

#inside application:
import ConfigParser

config = ConfigParser.ConfigParser()
config.read('/Users/markkhor/Desktop/Development/Sikuli/Testing/SimpleTest/config.ini')
username1 = config.get('user1','user')

print username1

#inside config.ini
[user1]
user = Guido Van Rossum
ip = 192.168.0.1
machineName= Guido's Machine
system = Ubuntu