Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,6 @@ ENV/
.ropeproject

.idea/
.pypirc
.pypirc
example/dqn_selenium_ai_weights.h5f

74 changes: 37 additions & 37 deletions example/configuration.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import os

import keras.backend as K
import numpy as np
from package.environment import SeleniumEnvironment
from package.processor import SeleniumObservationProcessor
from srcdir import srcdir
import keras.backend as K
from keras.layers import Dense, Activation, Flatten, Convolution2D, Permute
from keras.layers.convolutional import Conv2D
from keras.models import Sequential
from models import AbstractConfiguration, KickoffModes
from rl.memory import SequentialMemory
from rl.policy import LinearAnnealedPolicy, EpsGreedyQPolicy

from package.environment import SeleniumEnvironment
from package.processor import SeleniumObservationProcessor
from srcdir import srcdir


class ExampleConfiguration(AbstractConfiguration):
mode = KickoffModes.train # type: KickoffModes
mode = KickoffModes.test # type: KickoffModes

use_preset_training = True
render = True # Default true when testing
Expand Down Expand Up @@ -103,6 +101,7 @@ def on_step_reset(self, driver):
def on_environment_creation(self):
# Create file within the docker container to use as the test web page
filepath = srcdir + '/example/test.html'
print(filepath)
self.environment.selenium_docker_wrapper.upload_file_to_container(filepath)
self.starting_url = 'file://' + '/' + os.path.basename(filepath)

Expand All @@ -111,36 +110,37 @@ def get_preset_training_step(self):
return 0

targeted_element_xpath = '//input[contains(@id, "add-to-cart-button")]'
target_element = self.environment.driver.find_element_by_xpath(targeted_element_xpath)

target_element_top = target_element.location["y"]
target_element_bottom = target_element_top + target_element.size["height"]

scroll_amount = self.environment.driver.execute_script("return window.pageYOffset;")
window_size = self.environment.driver.get_window_size()
inner_height = self.environment.driver.execute_script("return window.innerHeight;")

if scroll_amount > target_element_top:
action = self.environment.action_space.mouse_scroll_up
elif scroll_amount + inner_height < target_element_bottom:
action = self.environment.action_space.mouse_scroll_down
else:
target_element_left = target_element.location["x"]
target_element_right = target_element_left + target_element.size["width"]

if self.environment.action_space.mouse_position_x > target_element_right:
action = self.environment.action_space.move_mouse_left
elif self.environment.action_space.mouse_position_x < target_element_left:
action = self.environment.action_space.move_mouse_right
elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \
> target_element_bottom:
action = self.environment.action_space.move_mouse_up
elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \
< target_element_top:
action = self.environment.action_space.move_mouse_down
try:
target_element = self.environment.driver.find_element_by_xpath(targeted_element_xpath)
target_element_top = target_element.location["y"]
target_element_bottom = target_element_top + target_element.size["height"]

scroll_amount = self.environment.driver.execute_script("return window.pageYOffset;")
window_size = self.environment.driver.get_window_size()
inner_height = self.environment.driver.execute_script("return window.innerHeight;")

if scroll_amount > target_element_top:
action = self.environment.action_space.mouse_scroll_up
elif scroll_amount + inner_height < target_element_bottom:
action = self.environment.action_space.mouse_scroll_down
else:
action = self.environment.action_space.mouse_press

target_element_left = target_element.location["x"]
target_element_right = target_element_left + target_element.size["width"]

if self.environment.action_space.mouse_position_x > target_element_right:
action = self.environment.action_space.move_mouse_left
elif self.environment.action_space.mouse_position_x < target_element_left:
action = self.environment.action_space.move_mouse_right
elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \
> target_element_bottom:
action = self.environment.action_space.move_mouse_up
elif self.environment.action_space.mouse_position_y - (window_size["height"] - inner_height) + scroll_amount \
< target_element_top:
action = self.environment.action_space.move_mouse_down
else:
action = self.environment.action_space.mouse_press
except:
return 0
return self.environment.action_space.available_actions.index(action)

def determine_reward(self, driver, action_index):
Expand All @@ -156,4 +156,4 @@ def determine_reward(self, driver, action_index):
reward = 0
done = False

return done, reward
return done, reward
6 changes: 5 additions & 1 deletion example/run_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import os.path
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../package'))
from example.configuration import ExampleConfiguration
from package.kickoff import kickoff


if __name__ == '__main__':
configuration = ExampleConfiguration()
kickoff(configuration=configuration)
kickoff(configuration=configuration)
68 changes: 0 additions & 68 deletions example/test.html
Original file line number Diff line number Diff line change
@@ -1,71 +1,3 @@




































































<!doctype html><html class="a-no-js a-touch a-mobile" data-19ax5a9jf="mongoose">
<head>
<script type="text/javascript">var ue_t0=ue_t0||+new Date();</script>
Expand Down
7 changes: 4 additions & 3 deletions package/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,16 @@ def _connect_to_remote_instance(self, chrome_options, selenium_url):
desired_capabilities=chrome_options.to_capabilities())

def clean(self):
print 'Cleaning up Selenium driver and docker container'
print('Cleaning up Selenium driver and docker container')

self.driver and self.driver.quit()
self.container and self.docker_client.remove_container(self.container_id, force=True)

def create_archive(self, filepath):
print(filepath)
pw_tarstream = BytesIO()
pw_tar = tarfile.TarFile(fileobj=pw_tarstream, mode='w')
file_data = open(filepath, 'r').read()
file_data = open(filepath, 'rb').read()
tarinfo = tarfile.TarInfo(name=os.path.basename(filepath))
tarinfo.size = len(file_data)
tarinfo.mtime = time.time()
Expand All @@ -84,4 +85,4 @@ def create_archive(self, filepath):

def upload_file_to_container(self, filepath):
with self.create_archive(filepath) as archive:
return self.docker_client.put_archive(container=self.container_id, path='/', data=archive)
return self.docker_client.put_archive(container=self.container_id, path='/', data=archive)
4 changes: 2 additions & 2 deletions package/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def make(self):
vnc_address = re.sub(str(container_port), str(container_vnc_port), container_url)
vnc_address = re.sub('http', 'vnc', vnc_address)

print 'You can view the VNC instance at ' + vnc_address, 'Password "secret"'
print('You can view the VNC instance at ' + vnc_address, 'Password "secret"')

self.vnc_env = VNCEnv()
self.vnc_env._configure(remotes=str(vnc_address), vnc_kwargs={'password': 'secret'})
Expand Down Expand Up @@ -70,4 +70,4 @@ def step(self, action_index):
done, reward = self.configuration.determine_reward(self.driver, action_index)
observation_array = self.vnc_env._step(action)

return observation_array, reward, done, {}
return observation_array, reward, done, {}
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
numpy
go-vncdriver
attrs==17.2.0
autobahn==17.5.1
Automat==0.6.0
Expand All @@ -10,7 +12,6 @@ docker-py==1.10.3
docker-pycreds==0.2.1
fastzbarlight==0.0.14
funcsigs==1.0.2
go-vncdriver==0.4.19
gym==0.7.4
h5py==2.7.0
idna==2.5
Expand All @@ -19,7 +20,6 @@ ipaddress==1.0.18
Keras==2.0.4
keras-rl==0.3.0
mock==2.0.0
numpy==1.13.0rc2
olefile==0.44
pbr==3.0.1
Pillow==4.1.1
Expand All @@ -39,4 +39,4 @@ universe==0.21.3
urllib3==1.21.1
websocket-client==0.40.0
Werkzeug==0.12.2
zope.interface==4.4.1
zope.interface==4.4.1
2 changes: 1 addition & 1 deletion srcdir.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import os
srcdir = os.path.dirname(__file__)
srcdir = os.path.dirname(os.path.abspath(__file__))