Skip to content

Selenium on Coder.com

dmosora edited this page Feb 18, 2019 · 3 revisions

Introduction

Coder.com has a unique environment, since it's running in a container, uses a root user by default, and has a bit of a minimal Ubuntu installation. Selenium and the Chrome WebDriver need a bit of coaxing to run behavioral tests. At the end of this article, there are some debugging steps and information to help use other browsers.

Running Selenium

From a new Coder.com container, run these commands in the command line:

  1. Mount Python (Cmd/Ctrl + Shift + P -> Type "mount" -> Enter -> Select Python) https://coder.com/docs/languages
    • If you have the command line open, you need to re-open it (hit the "trash can" icon in the top right and then reopen with CMD/Ctrl + ` )
  2. Install necessary Python packages: pip install behave selenium webdriver_manager pyvirtualdisplay
    • behave - Has the executable and Python functions to write code-backed behavioral tests
    • selenium - Allows loading a webdriver and writing code to navigate through a browser page
    • webdriver_manager - Streamlines the webdriver installation process
    • pyvirtualdisplay - Allows you to set up a virtual screen from the command line for the browser to use (helpful if the page changes for mobile screen sizes)
  3. Install XVFB (operating-system level display virtualization) to run a headless browser: sudo apt-get install xvfb
  4. Error message when running behave at this point says selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary So, install Chrome and its dependencies
apt-get update && apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb

dpkg -i google-chrome*.deb
apt-get install -f
  1. Profit! You can now run behave from the directory with your features folder in it. Behave will pick up the features and run the associated steps.

  2. To play with the browser object and see what you can do, use ipython (pip install ipython):

root@coder:/coder/mnt/longest_path/TDD-Playground/behavioral_tests# ipython
Python 3.7.0 (default, Jul  5 2018, 21:35:04)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from features.steps.amazon_steps import setup_chrome

In [2]: browser = setup_chrome()

In [3]: browser.get("https://www.amazon.com")

In [4]: browser.title
Out[4]: 'Amazon.com: Online Shopping for Electronics, Apparel, Computers, Books, DVDs & more'

In [5]: dir(browser)
Out[5]:
[...]

dir gets you all of the things that browser can do. You can use these to help navigate the Selenium documentation or source code.

Debugging

Reset Coder.com container - This is a good step to start from a blank slate in case something went awry. This will not affect your project code, only installations (pip, apt-get, etc.) and configuration (Linux users, linux file permissions, etc.)!

WARNING -- This WILL remove your ssh keypair. Back it up first so you can recreate it in ~/.ssh/id_rsa later without having to set up a new value in Github.

Setup Instructions Source - I based most of what to do off of this blog, but the instructions above have been pared down to only the things needed to work on Coder.com

Webdriver Manager - Python package that makes managing webdrivers easier. You can look here for running under other browsers like Firefox, Edge, etc.

Clone this wiki locally