Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubeddafali committed Sep 5, 2020
1 parent 786751d commit 01d52c8
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 31 deletions.
23 changes: 13 additions & 10 deletions Dockerfile.chrome
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
FROM ubuntu:16.04

ENV URL="https://www.google.com"
ENV URL="https://github.com/ayoubeddafali/sind"
ENV LOGIN="LOGIN"
ENV PASSWORD="PASSWORD"
ENV SCREENSHOTS_DIR="/home/agent/screenshots/"
ENV DOWNLOADS_DIR="/home/agent/downloads/"
ENV ENVIRONMENT="prod"
ENV DEBUG=False
ENV DISPLAY=:99

# Install Chrome
Expand All @@ -24,8 +28,7 @@ RUN apt-get update && apt-get install -y \
&& echo "deb [arch=amd64] https://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list \
&& apt-get update && apt-get install -y google-chrome-stable

RUN apt-get update \
&& apt-get install -y --no-install-recommends \
RUN apt-get install -y --no-install-recommends \
vim \
ca-certificates \
curl \
Expand All @@ -41,20 +44,20 @@ RUN apt-get update \
&& rm -rf /var/lib/apt/lists/*


RUN pip install --upgrade pip
RUN pip install wheel
RUN pip install python-xlib
RUN pip install selenium
RUN pip install pyautogui
RUN pip install pyscreenshot
RUN pip install --upgrade pip \
&& pip install wheel \
&& pip install python-xlib \
&& pip install selenium \
&& pip install pyautogui \
&& pip install pyscreenshot

WORKDIR /home/agent

COPY . .

RUN pip install -r requirements.txt

RUN mkdir -p /home/agent/screenshots && chmod +x start.sh
RUN mkdir -p ${SCREENSHOTS_DIR} && chmod +x start.sh

ENTRYPOINT [ "/home/agent/start.sh" ]
CMD ["python", "main.py"]
Expand Down
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: default

default: test

install:
pipenv install --dev --skip-lock

test:
PYTHONPATH=src/ pytest

shell:
pipenv shell --three

run:
python main.py

image:
docker build -t ${IMAGE_TAG} -f Dockerfile.chrome .
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<img src="./images/logo.png" width="300" height="300" />
</div>

<br>

> A minimal framework to automate web Actions/Plans, and run them in a containerized fashion.
Expand Down Expand Up @@ -40,25 +41,41 @@ executionPlan.run(URL)

While on development phase, you might need to run the plan locally and see your selenium script.

Make sure to have the following points marked :
Make sure to have the following points marked.

1. Install some tools :

```bash
$ sudo apt-get install -y xvfb xserver-xephyr
$ sudo apt-get install scrot -y
```

2. Setup environment & dependencies

```bash
$ pip install pipenv
$ make shell
$ make install
```

3. Run your app :

```
$ make run
```

### Run your tests

```bash
$ make test
```

> TODO
### Production

Once you've finished writing your scenario, you will then start by building a docker image :

> TODO
```bash

$ IMAGE_TAG=custom_image:1.0 make image
```

### Available Tools :
Expand Down
Empty file removed downloads/.gitkeep
Empty file.
11 changes: 8 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@
PWD = os.getenv("PWD")
DEBUG = os.getenv("DEBUG", True)
DOWNLOADS_DIR = os.getenv("DOWNLOADS_DIR", "{}/downloads/".format(PWD))
SCREENSHOTS_DIR = os.getenv("SCREENSHOTS_DIR", "{}/screenshots/".format(PWD))
CHROME_EXEC_PATH = os.getenv("CHROME_DRIVER", "{}/drivers/chromedriver".format(PWD))
ENVIRONMENT = os.getenv("ENVIRONMENT", "prod")

if DEBUG :
print(" ----- Runtime Variables ---- ")
print("PWD: {}".format(PWD))
print("DOWNLOADS_DIR: {}".format(DOWNLOADS_DIR))
print("SCREENSHOTS_DIR: {}".format(SCREENSHOTS_DIR))
print("CHROME_DRIVER: {}".format(CHROME_EXEC_PATH))
print(" ----- END / Runtime Variables ---- ")

display = SmartDisplay(visible=0, size=(1080, 1920))
display = SmartDisplay(visible=1, size=(1080, 1920))
display.start()

options = webdriver.ChromeOptions()
Expand All @@ -39,23 +42,25 @@
# Clean & Create DOWNLOADS_DIR on each run
try:
shutil.rmtree(DOWNLOADS_DIR)
shutil.rmtree(SCREENSHOTS_DIR)
except OSError as er:
pass
try:
os.mkdir(DOWNLOADS_DIR)
os.mkdir(SCREENSHOTS_DIR)
except OSError as er:
if er.errno != os.errno.EEXIST:
raise
pass

# Examples of variables you can define
URL = os.getenv("URL","https://google.com")
URL = os.getenv("URL","https://github.com/ayoubeddafali/sind")
LOGIN = os.getenv("LOGIN", "LOGIN")
PASSWORD = os.getenv("PASSWORD", "PASSWORD")

driver = webdriver.Chrome(executable_path=CHROME_EXEC_PATH, chrome_options=options)
# driver.set_window_size(1080, 1920)

executionPlan = ExecutionPlan(browser=driver, display=display, login=LOGIN, password=PASSWORD)
executionPlan = ExecutionPlan(browser=driver, display=display, login=LOGIN, password=PASSWORD, downloads_dir=DOWNLOADS_DIR, screenshots_dir=SCREENSHOTS_DIR)
executionPlan.run(URL)

19 changes: 6 additions & 13 deletions plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,20 @@
pyautogui.FAILSAFE = False

class ExecutionPlan:
def __init__(self, browser, display, login, password):
def __init__(self, browser, display, login, password, downloads_dir, screenshots_dir):
self.browser = browser
self.display = display
self.login = login
self.password = password
self.downloads_dir = downloads_dir
self.screenshots_dir = screenshots_dir

def run(self, base_url, users):
def run(self, base_url):
self.browser.get(base_url)
try:
# This is just an example scenario
pyautogui.screenshot('/home/agent/screenshots/1.png')
usernameInput = self.browser.find_element_by_id("usernamefld")
usernameInput.clear()
usernameInput.send_keys(self.login)
pyautogui.screenshot('/home/agent/screenshots/1.png')
passowrdInput = self.browser.find_element_by_id("passwordfld")
passowrdInput.clear()
passowrdInput.send_keys(self.password)
pyautogui.screenshot('/home/agent/screenshots/3.png')
loginBtn = self.browser.find_element_by_name("login")
loginBtn.click()
pyautogui.screenshot("{}/1.png".format(self.screenshots_dir))
sleep(5)

except TimeoutException as timeout:
print(timeout)
Expand Down
Binary file added screenshots/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01d52c8

Please sign in to comment.