Skip to content

Commit e3c9f34

Browse files
committed
Address issue 4
Changes to support Python 3 with Python 2 backwards compatibility for server and sample code per issue 4
1 parent 40c902e commit e3c9f34

File tree

5 files changed

+63
-47
lines changed

5 files changed

+63
-47
lines changed

autoitdriverserver_python/server.py

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@
2020
# specific language governing permissions and limitations
2121
# under the License.
2222

23+
from __future__ import print_function
2324
from bottle import Bottle, request, response, redirect
2425
from bottle import run, static_file
25-
import ConfigParser
2626
import json
2727
import socket
2828
import sys
2929
import platform
3030
import os
3131
import subprocess
32+
import tempfile
3233
import base64
3334
import urllib
3435
#import autoit
3536
import win32com.client
3637
from time import time
3738
from time import sleep
39+
try:
40+
import configparser
41+
except ImportError:
42+
import ConfigParser as configparser
3843

3944
app = Bottle()
4045

@@ -68,7 +73,7 @@ def create_session():
6873

6974
#process desired capabilities
7075
request_data = request.body.read()
71-
dc = json.loads(request_data).get('desiredCapabilities')
76+
dc = json.loads(request_data.decode()).get('desiredCapabilities')
7277
if dc is not None:
7378
app.caretCoordMode = dc.get('caretCoordMode') if dc.get('caretCoordMode') is not None else app.caretCoordMode
7479
app.expandEnvStrings = dc.get('expandEnvStrings') if dc.get('expandEnvStrings') is not None else app.expandEnvStrings
@@ -162,24 +167,24 @@ def delete_session(session_id=''):
162167
def execute_script(session_id=''):
163168
request_data = request.body.read()
164169
try:
165-
script = json.loads(request_data).get('script')
166-
args = json.loads(request_data).get('args')
170+
script = json.loads(request_data.decode()).get('script')
171+
args = json.loads(request_data.decode()).get('args')
167172

168173
if config.get("AutoIt Options",'AutoItScriptExecuteScriptAsCompiledBinary') == "False":
169-
if platform.machine() == "AMD64": # or "x86_64"?
174+
if platform.machine() == "AMD64":
170175
if config.get("AutoIt Options",'AutoIt64BitOSOnInstallUse32Bit') == "True":
171176
au3Runner = config.get("AutoIt Options",'AutoIt64BitOS32BitExecutablePath')
172177
else:
173178
au3Runner = config.get("AutoIt Options",'AutoIt64BitOS64BitExecutablePath')
174-
else: # platform.machine() == "i386" or "x86"
179+
else: # platform.machine() == "i386"
175180
au3Runner = config.get("AutoIt Options",'AutoIt32BitExecutablePath')
176181
script_call = "%s %s" % (au3Runner,script)
177182
else:
178183
script_call = script
179184
if args is not None:
180185
for arg in args:
181186
script_call = "%s %s" % (script_call,arg)
182-
print "script2exec: %s" % script_call
187+
print("script2exec: ",script_call)
183188
os.system(script_call)
184189
except:
185190
response.status = 400
@@ -213,7 +218,7 @@ def mouse_click(session_id=''):
213218
if request_data == None or request_data == '' or request_data == "{}":
214219
button = 0
215220
else:
216-
button = json.loads(request_data).get('button')
221+
button = json.loads(request_data.decode()).get('button')
217222
try:
218223
if button == 1:
219224
btn_type = "middle"
@@ -255,7 +260,7 @@ def mouse_up(session_id=''):
255260
if request_data == None or request_data == '' or request_data == "{}":
256261
button = 0
257262
else:
258-
button = json.loads(request_data).get('button')
263+
button = json.loads(request_data.decode()).get('button')
259264
try:
260265
if button == 1:
261266
btn_type = "middle"
@@ -280,7 +285,7 @@ def mouse_down(session_id=''):
280285
if request_data == None or request_data == '' or request_data == "{}":
281286
button = 0
282287
else:
283-
button = json.loads(request_data).get('button')
288+
button = json.loads(request_data.decode()).get('button')
284289
try:
285290
if button == 1:
286291
btn_type = "middle"
@@ -307,9 +312,9 @@ def move_to(session_id=''):
307312
xoffset = None
308313
yoffset = None
309314
else:
310-
element_id = json.loads(request_data).get('element')
311-
xoffset = json.loads(request_data).get('xoffset')
312-
yoffset = json.loads(request_data).get('yoffset')
315+
element_id = json.loads(request_data.decode()).get('element')
316+
xoffset = json.loads(request_data.decode()).get('xoffset')
317+
yoffset = json.loads(request_data.decode()).get('yoffset')
313318
try:
314319
if element_id == None and (xoffset != None or yoffset != None):
315320
#src = autoit.mouse_get_pos()
@@ -353,7 +358,7 @@ def move_to(session_id=''):
353358
def set_value(session_id='', element_id=''):
354359
request_data = request.body.read()
355360
try:
356-
value_to_set = json.loads(request_data).get('value')
361+
value_to_set = json.loads(request_data.decode()).get('value')
357362
value_to_set = ''.join(value_to_set)
358363
control_id = decode_value_from_wire(element_id)
359364
#result = autoit.control_set_text("[active]",control_id,value_to_set)
@@ -396,7 +401,7 @@ def find_element(session_id=''):
396401

397402
def _find_element(session_id, context, many=False):
398403
try:
399-
json_request_data = json.loads(request.body.read())
404+
json_request_data = json.loads(request.body.read().decode())
400405
locator_strategy = json_request_data.get('using')
401406
value = json_request_data.get('value')
402407

@@ -430,7 +435,7 @@ def _find_element(session_id, context, many=False):
430435
def keys(session_id=''):
431436
try:
432437
request_data = request.body.read()
433-
wired_keys = json.loads(request_data).get('value')
438+
wired_keys = json.loads(request_data.decode()).get('value')
434439
keys = "".join(wired_keys)
435440
#autoit.send(keys)
436441
app.oAutoItX.Send(keys)
@@ -609,7 +614,7 @@ def get_current_window_handle(session_id=''):
609614
def select_window(session_id=''):
610615
request_data = request.body.read()
611616
try:
612-
win_name_or_handle = json.loads(request_data).get('name')
617+
win_name_or_handle = json.loads(request_data.decode()).get('name')
613618
#try:
614619
#autoit.win_activate_by_handle(win_name_or_handle)
615620
#except:
@@ -642,8 +647,8 @@ def close_window(session_id=''):
642647
def resize_window(session_id='', window_handle=''):
643648
try:
644649
request_data = request.body.read()
645-
width = json.loads(request_data).get('width')
646-
height = json.loads(request_data).get('height')
650+
width = json.loads(request_data.decode()).get('width')
651+
height = json.loads(request_data.decode()).get('height')
647652

648653
if window_handle == "current":
649654
window = "[active]"
@@ -703,8 +708,8 @@ def get_window_size(session_id='', window_handle=''):
703708
def move_window(session_id='', window_handle=''):
704709
try:
705710
request_data = request.body.read()
706-
x = json.loads(request_data).get('x')
707-
y = json.loads(request_data).get('y')
711+
x = json.loads(request_data.decode()).get('x')
712+
y = json.loads(request_data.decode()).get('y')
708713

709714
if window_handle == "current":
710715
window = "[active]"
@@ -774,7 +779,7 @@ def max_window(session_id='', window_handle=''):
774779
def run_autoit_app(session_id=''):
775780
try:
776781
request_data = request.body.read()
777-
app2run = json.loads(request_data).get('url')
782+
app2run = json.loads(request_data.decode()).get('url')
778783
#autoit.run(app2run)
779784
app.oAutoItX.Run(app2run)
780785
#if autoit._has_error():
@@ -793,11 +798,12 @@ def run_autoit_app(session_id=''):
793798
def upload_file(session_id=''):
794799
try:
795800
request_data = request.body.read()
796-
b64data = json.loads(request_data).get('file')
801+
b64data = json.loads(request_data.decode()).get('file')
797802
byteContent = base64.b64decode(b64data)
798-
path = os.tempnam()
799-
with open(path, 'wb') as f:
803+
path = ""
804+
with tempfile.NamedTemporaryFile(delete=False) as f:
800805
f.write(byteContent)
806+
path = f.name
801807
extracted_files = unzip(path,os.path.dirname(path))
802808
except:
803809
response.status = 400
@@ -827,7 +833,7 @@ def unzip(source_filename, dest_dir):
827833
path = os.path.join(path, word)
828834
zf.extract(member, path)
829835
unzipped_file = os.path.join(dest_dir,member.filename)
830-
print "Unzipped a file: %s" % unzipped_file
836+
print("Unzipped a file: ",unzipped_file)
831837
files_in_zip.append(unzipped_file)
832838
return files_in_zip
833839

@@ -837,10 +843,16 @@ def unsupported_command(error):
837843
return 'Unrecognized command, or AutoItDriverServer does not support/implement this: %s %s' % (request.method, request.path)
838844

839845
def encode_value_4_wire(value):
840-
return urllib.pathname2url(base64.b64encode(value))
846+
try:
847+
return urllib.parse.quote(base64.b64encode(value.encode("utf-8")))
848+
except:
849+
return urllib.quote(base64.b64encode(value.encode("utf-8")))
841850

842851
def decode_value_from_wire(value):
843-
return base64.b64decode(urllib.url2pathname(value))
852+
try:
853+
return base64.b64decode(urllib.parse.unquote(value)).decode("utf-8")
854+
except:
855+
return base64.b64decode(urllib.unquote(value)).decode("utf-8")
844856

845857
if __name__ == '__main__':
846858
import argparse
@@ -863,7 +875,7 @@ def decode_value_from_wire(value):
863875
options_file = args.autoit_options_file
864876
else:
865877
options_file = os.path.join(os.path.curdir,'autoit_options.cfg')
866-
config = ConfigParser.RawConfigParser()
878+
config = configparser.RawConfigParser()
867879
config.read(options_file)
868880
app.caretCoordMode = config.get("AutoIt Options",'CaretCoordMode')
869881
app.expandEnvStrings = config.get("AutoIt Options",'ExpandEnvStrings')

sample-code/SeleniumIntegrationWithAutoItDriver.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
from __future__ import print_function
12
from selenium import webdriver
23
from selenium.webdriver import ActionChains
34
import time
45

56
wd = webdriver.Firefox()
67
ad = webdriver.Remote( command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities={'browserName':'AutoIt'})
7-
print "Desired Capabilities returned by server:\n"
8-
print ad.desired_capabilities
9-
print ""
8+
print("Desired Capabilities returned by server:\n")
9+
print(ad.desired_capabilities)
10+
print("")
1011

1112
action1 = ActionChains(ad)
1213

@@ -16,7 +17,7 @@
1617
# check state that img is "unauthenticated" at start
1718
img_src = wd.find_element_by_id("downloadImg").get_attribute("src")
1819
if not img_src.endswith("/images/spacer.gif"):
19-
print "HTTP demo test fail because test site not started with correct default unauthenticated state."
20+
print("HTTP demo test fail because test site not started with correct default unauthenticated state.")
2021

2122
# now test authentication
2223
wd.find_element_by_id("displayImage").click() # trigger the popup
@@ -28,7 +29,7 @@
2829
# now check img is authenticated or changed
2930
img_src = wd.find_element_by_id("downloadImg").get_attribute("src")
3031
if img_src.endswith("/images/spacer.gif"):
31-
print "HTTP demo failed, image didn't authenticate/change after logging in."
32+
print("HTTP demo failed, image didn't authenticate/change after logging in.")
3233

3334
### file upload demo, also adapted from sample code of the test/target site ###
3435
wd.get("http://www.toolsqa.com/automation-practice-form")

sample-code/calculator.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from __future__ import print_function
12
from selenium import webdriver
23
from selenium.webdriver import ActionChains
34
import time
45

56
driver = webdriver.Remote( command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities={'browserName':'AutoIt'})
6-
print "Desired Capabilities returned by server:\n"
7-
print driver.desired_capabilities
8-
print ""
7+
print("Desired Capabilities returned by server:\n")
8+
print(driver.desired_capabilities)
9+
print("")
910

1011
# demo adapted from
1112
# http://www.joecolantonio.com/2014/07/02/selenium-autoit-how-to-automate-non-browser-based-functionality/
@@ -21,7 +22,7 @@
2122
driver.find_element_by_id("121").click() # =
2223
time.sleep(1)
2324
if driver.find_element_by_id("150").text != "6":
24-
print "3 + 3 did not produce 6 as expected."
25+
print("3 + 3 did not produce 6 as expected.")
2526
driver.find_element_by_id("81").click() # Clear "C" button
2627
time.sleep(1)
2728

@@ -32,19 +33,19 @@
3233
action1.send_keys("2*2=").perform()
3334
time.sleep(1)
3435
if driver.find_element_by_id("150").text != "4":
35-
print "2 x 2 did not produce 4 as expected."
36+
print("2 x 2 did not produce 4 as expected.")
3637
driver.find_element_by_id("81").click() # Clear "C" button
3738
time.sleep(1)
3839
action2.send_keys("4*4=").perform()
3940
time.sleep(1)
4041
if driver.find_element_by_id("150").text != "16":
41-
print "4 x 4 did not produce 16 as expected."
42+
print("4 x 4 did not produce 16 as expected.")
4243
driver.find_element_by_id("81").click() # Clear "C" button
4344
time.sleep(1)
4445
action3.send_keys("8*8=").perform()
4546
time.sleep(1)
4647
if driver.find_element_by_id("150").text != "64":
47-
print "8 x 8 did not produce 64 as expected."
48+
print("8 x 8 did not produce 64 as expected.")
4849
driver.find_element_by_id("81").click() # Clear "C" button
4950
time.sleep(1)
5051
driver.close()

sample-code/notepad.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
from __future__ import print_function
12
from selenium import webdriver
23
from selenium.webdriver import ActionChains
34
import time
45

56
driver = webdriver.Remote( command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities={'browserName':'AutoIt'})
6-
print "Desired Capabilities returned by server:\n"
7-
print driver.desired_capabilities
8-
print ""
7+
print("Desired Capabilities returned by server:\n")
8+
print(driver.desired_capabilities)
9+
print("")
910

1011
# demo adapted from AutoItX VBScript example that comes with AutoIt installation
1112
driver.get("notepad.exe")

sample-code/run_au3_script_demo.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
from __future__ import print_function
12
from selenium import webdriver
23
import os
34

45
driver = webdriver.Remote( command_executor='http://127.0.0.1:4723/wd/hub', desired_capabilities={'browserName':'AutoIt'})
5-
print "Desired Capabilities returned by server:\n"
6-
print driver.desired_capabilities
7-
print ""
6+
print("Desired Capabilities returned by server:\n")
7+
print(driver.desired_capabilities)
8+
print("")
89

910
# execute an AutoIt script file (rather than call specific AutoItX API commands)
1011
# supply path to AutoIt script file followed by optional arguments

0 commit comments

Comments
 (0)