commit 11067cf1b101b6e2f7fb497cdba2e5018efab3f9 Author: Michael Mintz Date: Fri Dec 4 16:11:53 2015 -0500 Fresh Copy diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..f55b1c54 --- /dev/null +++ b/.gitignore @@ -0,0 +1,37 @@ +*.py[cod] + +# Packages +*.egg +*.egg-info +dist +build +logs +archived_logs +ghostdriver.log +eggs +parts +bin +var +sdist +develop-eggs +.installed.cfg +lib +lib64 +__pycache__ + +# Installer logs +pip-log.txt +.swp + +# Unit test / coverage reports +.coverage +.tox +nosetests.xml + +# py.test +.cache/* +.pytest_config + +# Developer +.project +.pydevproject diff --git a/Docker_README.md b/Docker_README.md new file mode 100755 index 00000000..bb6be3bd --- /dev/null +++ b/Docker_README.md @@ -0,0 +1,59 @@ +## Docker setup instructions for SeleniumBase + +#### 1. Get the Docker Toolbox from https://www.docker.com/docker-toolbox and install it. + +#### 2. Create your SeleniumBase Docker environment: + + docker-machine create --driver virtualbox seleniumbase + +#### 3. If your Docker environment ever goes down for any reason, you can bring it back up with a restart: + + docker-machine restart seleniumbase + +#### 4. Configure your shell: + + eval "$(docker-machine env seleniumbase)" + +#### 5. Go to the SeleniumBase home directory. (That's where "Dockerfile" is located) + +#### 6. Create your Docker image from your Dockerfile: (Get ready to wait awhile) + + docker build -t seleniumbase . + +#### 7. Run a test inside your Docker: (Once the test completes after a few seconds, you'll automatically exit the Docker shell) + + docker run seleniumbase ./run_docker_test_in_firefox.sh + +#### 8. You can also enter Docker and stay inside the shell: + + docker run -i -t seleniumbase + +#### 9. Now you can run the example test from inside the Docker shell: (This time using PhantomJS) + + ./run_docker_test_in_phantomjs.sh + +#### 10. When you're satisfied, you may exit the Docker shell: + + exit + +#### 11. (Optional) Since Docker images and containers take up a lot of space, you may want to clean up your machine from time to time when they’re not being used: +http://stackoverflow.com/questions/17236796/how-to-remove-old-docker-containers +Here are a few of those cleanup commands: + + docker images | grep "" | awk '{print $3}' | xargs docker rmi + docker rm 'docker ps --no-trunc -aq' + +If you want to completely remove all of your Docker containers and images, use these commands: (If there's nothing to delete, those commands will return an error.) + + docker rm $(docker ps -a -q) + docker rmi $(docker images -q) + +Finally, if you want to wipe out your SeleniumBase Docker virtualbox, use these commands: + + docker-machine kill seleniumbase + docker-machine rm seleniumbase + +#### 12. (Optional) More reading on Docker can be found here: +* https://docs.docker.com +* https://docs.docker.com/mac/started/ +* https://docs.docker.com/installation/mac/ diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 00000000..9620a042 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,105 @@ +# SeleniumBase Docker Image +FROM ubuntu:14.04 + +# Install Python and Basic Python Tools +RUN apt-get update && apt-get install -y python python-pip python-setuptools python-dev python-distribute + +#======================== +# Miscellaneous packages +# Includes minimal runtime used for executing selenium with firefox +#======================== +ENV BUILD_DEPS '\ + build-essential \ + libmysqlclient-dev \ + libpython-dev \ + libyaml-dev \ + libxml2-dev \ + libxslt1-dev \ + libxslt-dev \ + zlib1g-dev \ + ' + +RUN apt-get update -qqy \ + && apt-get -qy --no-install-recommends install \ + locales \ + language-pack-en \ + sudo \ + unzip \ + wget \ + curl \ + vim \ + xvfb \ + libaio1 \ + libxml2 \ + libxslt1.1 \ + mysql-client \ + ${BUILD_DEPS} \ + && rm -rf /var/lib/apt/lists/* + +#============================== +# Locale and encoding settings +#============================== +ENV LANGUAGE en_US.UTF-8 +ENV LANG ${LANGUAGE} +RUN locale-gen ${LANGUAGE} \ + && dpkg-reconfigure --frontend noninteractive locales + +#==================== +# Firefox Latest ESR +#==================== +RUN apt-get update -qqy \ + && apt-get -qy --no-install-recommends install \ + $(apt-cache depends firefox | grep Depends | sed "s/.*ends:\ //" | tr '\n' ' ') \ + && rm -rf /var/lib/apt/lists/* \ + && cd /tmp \ + && wget --no-check-certificate -O firefox-esr.tar.bz2 \ + 'https://download.mozilla.org/?product=firefox-esr-latest&os=linux64&lang=en-US' \ + && tar -xjf firefox-esr.tar.bz2 -C /opt/ \ + && ln -s /opt/firefox/firefox /usr/bin/firefox \ + && rm -f /tmp/firefox-esr.tar.bz2 + +#=================== +# Timezone settings +#=================== +# Full list at http://en.wikipedia.org/wiki/List_of_tz_database_time_zones +# e.g. "US/Pacific" for Los Angeles, California, USA +ENV TZ "America/New_York" +# Apply TimeZone +RUN echo $TZ | tee /etc/timezone \ + && dpkg-reconfigure --frontend noninteractive tzdata + +#======================================== +# Add normal user with passwordless sudo +#======================================== +RUN sudo useradd seluser --shell /bin/bash --create-home \ + && sudo usermod -a -G sudo seluser \ + && echo 'ALL ALL = (ALL) NOPASSWD: ALL' >> /etc/sudoers + +#=================== +# Install PhantomJS +#=================== +RUN cd /usr/local/share && wget https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-x86_64.tar.bz2 +RUN cd /usr/local/share && tar xjf phantomjs-1.9.7-linux-x86_64.tar.bz2 +RUN ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/share/phantomjs +RUN ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/local/bin/phantomjs +RUN ln -s /usr/local/share/phantomjs-1.9.7-linux-x86_64/bin/phantomjs /usr/bin/phantomjs + +#===================== +# Set up SeleniumBase +#===================== +COPY docker/docker_requirements.txt /SeleniumBase/ +COPY docker/docker_setup.py /SeleniumBase/ +COPY seleniumbase /SeleniumBase/seleniumbase/ +COPY examples /SeleniumBase/examples/ +RUN cd /SeleniumBase && ls && sudo pip install -r docker_requirements.txt +RUN cd /SeleniumBase && ls && sudo python docker_setup.py install + +#========================================= +# Create entrypoint and grab example test +#========================================= +COPY docker/docker-entrypoint.sh / +COPY docker/run_docker_test_in_firefox.sh / +COPY docker/run_docker_test_in_phantomjs.sh / +COPY docker/docker_config.cfg /SeleniumBase/examples/ +ENTRYPOINT ["/docker-entrypoint.sh"] +CMD ["/bin/bash"] diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..aaa102c5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Michael Mintz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/README.md b/README.md new file mode 100755 index 00000000..73a18e73 --- /dev/null +++ b/README.md @@ -0,0 +1,602 @@ +# SeleniumBase Automation Framework + +### An open-source solution for reliable testing and business automation. + +Trusted by Boston's most promising companies, including HubSpot, Jana, and Veracode. + +#### Features include: +* Python libraries for quickly making reliable WebDriver scripts that run fast. +* Built-in Nosetest & Pytest plugins for logging test data, results, and screenshots. +* Simple integration with [Jenkins](http://jenkins-ci.org/), [Selenium Grid](http://docs.seleniumhq.org/projects/grid/), [MySQL](http://www.mysql.com/), [Docker](https://www.docker.com/), and [AWS](http://aws.amazon.com/). +* Customizable with command-line options and a global config file: [settings.py](https://github.com/mdmintz/SeleniumBase/blob/master/seleniumbase/config/settings.py). + +*Learn how the pros handle test automation: Check out HubSpot's blog article on [Automated Testing with Selenium](http://dev.hubspot.com/blog/bid/88880/Automated-Integration-Testing-with-Selenium-at-HubSpot), and read [The Classic "QA Team" is Obsolete](http://product.hubspot.com/blog/the-classic-qa-team-is-obsolete) for more.* + + +## Part I: MAC SETUP INSTRUCTIONS +####(WINDOWS users: You'll need to make a few modifications to the setup steps listed here. For starters, you won't be able to use the "brew install" command since that's MAC-only. Instead, download the requirements mentioned directly from the web. I'll provide you with links to save you time. You'll also want to put downloaded files into your [PATH](http://java.com/en/download/help/path.xml).) +####(DOCKER users: If you want to run browser automation with Docker, see the [Docker_README](https://github.com/mdmintz/SeleniumBase/blob/master/Docker_README.md)) + +#### **Step 0:** Get the requirements + +[Python 2.*](https://www.python.org/downloads/) + +If you're a MAC user, that should already come preinstalled on your machine. Although Python 3 exists, you'll want Python 2 (both of these major versions are being improved in parallel). Python 2.7.10 is the one I've been using on my Mac. + +If you're a WINDOWS user, [download the latest 2.* version from here](https://www.python.org/downloads/release/python-2710/). Depending on which version of Python you have installed, you may need to install "pip" if your Python installation didn't come with it. If you don't have it installed, you can [get pip here](https://pip.pypa.io/en/latest/installing/). + +[Homebrew](http://brew.sh/) + [Git](http://git-scm.com/) + +(NOTE: You can download the SeleniumBase repository right from GitHub and skip all the git-related commands. That's probably the fastest way if you want to quickly get a live demo of this tool up and running.) + + ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" + brew install git + brew update + +(WINDOWS users: Skip the Homebrew part and [download Git here](http://git-scm.com/download).) + +[MySQL](http://www.mysql.com/) + +(NOTE: If you're using this test framework from a local development machine and don't plan on writing to a MySQL DB from your local test runs, you can skip this step.) + + brew install MySQL + +That installs the MySQL library so that you can use db commands in your code. To make that useful, you'll want to have a MySQL DB that you can connect to. You'll also want to use the testcaserepository.sql file from the seleniumbase/core folder to add the necessary tables. + +(WINDOWS users: [Download MySQL here](http://dev.mysql.com/downloads/windows/). If you want a visual tool to help make your MySQL life easier, [try MySQL Workbench](http://dev.mysql.com/downloads/workbench/).) + +[virtualenvwrapper](http://virtualenvwrapper.readthedocs.org/en/latest/) + + cd ~/ + pip install virtualenvwrapper + export WORKON_HOME=~/Envs + mkdir -p $WORKON_HOME + source /usr/local/bin/virtualenvwrapper.sh + +To save time from having to source virtualenvwrapper again when you open a new window, you can add the line "source /usr/local/bin/virtualenvwrapper.sh" to a file called ".bash_profile" in your home directory. + + cd ~/ + vi .bash_profile + source /usr/local/bin/virtualenvwrapper.sh + +("vi" is a fast text editor - "i" for insert-text mode, "esc" to get out of insert-text mode, ":wq"+[Enter] to save & exit the file) + +[Chromedriver](https://sites.google.com/a/chromium.org/chromedriver/) and [PhantomJS](http://phantomjs.org/) + + brew install chromedriver phantomjs + +(NOTE: There are web drivers for other web browsers as well. These two will get you started.) + +(WINDOWS users: [Download Chromedriver](https://sites.google.com/a/chromium.org/chromedriver/downloads) and put it in your PATH. Next, [Download PhantomJS](https://bitbucket.org/ariya/phantomjs/downloads) and also put that in your PATH.) + +If you haven't already, you'll want to [Download Firefox](https://www.mozilla.org/en-US/firefox/new/) and either [Download Chrome](https://www.google.com/chrome/browser/desktop/index.html) or [Download Chromium](https://download-chromium.appspot.com/). + + +#### **Step 1:** Download or Clone SeleniumBase to your local machine + +If you're using Git, you can fork the repository on GitHub to create your personal copy. This is important because you'll want to add your own configurations, credentials, settings, etc. Now you can clone your forked copy to your personal computer. You can use a tool such as [SourceTree](http://www.sourcetreeapp.com/) to make things easier by providing you with a simple-to-use user interface for viewing and managing your git commits and status. + +```bash +git clone [LOCATION OF YOUR FORKED SELENIUMBASE GITHUB FOLDER]/seleniumbase.git +cd seleniumbase +``` + +(NOTE: If you decided to download SeleniumBase rather than Git-cloning it, you can skip the above step.) + +#### **Step 2:** Create a virtualenv for seleniumbase + +```bash +mkvirtualenv seleniumbase +``` + +(Virtual environments are important because they allow you to have separate configurations from the rest of your system. This will prevent conflicts if you use other tools that require other configurations and settings.) + +If you ever need to leave your virtual environment, use the following command: + +```bash +deactivate +``` + +To get back into your virtual environment, use the following command: + +```bash +workon seleniumbase +``` + +To see a list of environments that exist on your system, use the following command: + +```bash +lsvirtualenv +``` + +To delete a virtual environment that you no longer need, use the following command: + +```bash +rmvirtualenv [NAME OF VIRTUAL ENV TO REMOVE] +``` + +#### **Step 3:** Install necessary packages from the SeleniumBase folder and compile the test framework + +If you don't desire connecting to a MySQL DB to record the results of your local test runs, run this command: + +```bash +sudo pip install -r requirements.txt +``` +The "-e ." at the end of requirements.txt should automatically trigger setup.py installation from the following command: +```bash +sudo python setup.py install +``` + +If you do desire connecting to a MySQL DB to record the results of your test runs, run this command: (Make sure you already have MySQL installed from either Brew or web-download. If you're a WINDOWS user, you may have problems on the MySQL installation part. To get around this, you can either follow the instructions from the error message given, or you can pip install the previous requirements.txt file.) + +```bash +sudo pip install -r server_requirements.txt +``` + +As mentioned before, the "-e ." in that file should automatically trigger installation of setup.py + +NOTE: + +(If you already have root access on the machine you're using, you might not need to add "sudo" before those commands.) + +In some cames, certain packages will have other dependencies as requirements, and those will get installed automatically. You'll be able to see all installed packages in your virtual environment by using the following command: + +```bash +pip freeze +``` + +By default, some files may be hidden on a MAC, such as .gitignore (which is used to tell Git which files to ignore for staging commits). To view all files from the Finder window, run the following command in a terminal window: + +```bash +defaults write com.apple.finder AppleShowAllFiles -bool true +``` + +(You may need to reopen the MAC Finder window to see changes from that.) + + +#### **Step 4:** Verify that Selenium and Chromedriver were successfully installed by checking inside a python command prompt + +```bash +python +>>> from selenium import webdriver +>>> browser = webdriver.Chrome() +>>> browser.get("http://dev.hubspot.com/blog/the-classic-qa-team-is-obsolete") +>>> browser.close() +>>> exit() +``` + + +#### **Step 5:** Verify that SeleniumBase was successfully installed by running example tests + +You can verify the installation of SeleniumBase by writing a simple script to perform basic actions such as navigating to a web page, clicking, waiting for page elements to appear, typing in text, scraping text on a page, and verifying text. (Copy/paste the following code into a new file called "my_first_test.py"). This may be a good time to read up on css selectors. If you use Chrome, you can right-click on a page and select "Inspect Element" to see the details you need to create such a script. At a quick glance, dots are for class names and pound signs are for IDs. + +```python +from seleniumbase import BaseCase + +class MyTestClass(BaseCase): + + def test_basic(self): + self.open("http://xkcd.com/353/") + self.wait_for_element_visible("div#comic") + self.click('a[rel="license"]') + text = self.wait_for_element_visible('center').text + self.assertTrue("reuse any of my drawings" in text) + self.assertTrue("You can use them freely" in text) + self.open("http://xkcd.com/1481/") + self.click_link_text('Blag') + self.wait_for_text_visible("The blag of the webcomic", "#site-description") + self.update_text_value("input#s", "Robots!\n") + self.wait_for_text_visible("Hooray robots!", "#content") + self.open("http://xkcd.com/1319/") + self.wait_for_text_visible("Automation", "div#ctitle") +``` + +Now try running the script using various web browsers: + +```bash +nosetests my_first_test.py --browser=chrome --with-selenium -s + +nosetests my_first_test.py --browser=phantomjs --with-selenium -s + +nosetests my_first_test.py --browser=firefox --with-selenium -s +``` + +After the test completes, in the console output you'll see a dot on a new line, representing a passing test. (On test failures you'll see an F instead, and on test errors you'll see an E). It looks more like a moving progress bar when you're running a ton of unit tests side by side. This is part of nosetests. After all tests complete (in this case there is only one), you'll see the "Ran 1 test in ..." line, followed by an "OK" if all nosetests passed. +If the example is moving too fast for your eyes to see what's going on, there are 2 things you can do. Add either of the following: + +```python +import time; time.sleep(5) # sleep for 5 seconds (add this after the line you want to pause on) +import ipdb; ipdb.set_trace() # waits for your command. n = next line of current method, c = continue, s = step / next executed line (will jump) +``` + +You may also want to have your test sleep in other situations where you need to have your test wait for something. If you know what you're waiting for, you should be specific by using a command that waits for something specific to happen. + +If you need to debug things on the fly (in case of errors), use this line to run the code: + +```bash +nosetests my_first_test.py --browser=chrome --with-selenium --pdb --pdb-failures -s +``` + +The above code (with --pdb) will leave your browser window open in case there's a failure, which is possible if the web pages from the example change the data that's displayed on the page. (ipdb commands: 'c', 's', 'n' => continue, step, next). + +Here are some other useful nosetest arguments that you may want to append to your run commands: + +```bash +--logging-level=INFO # Hide DEBUG messages, which can be overwhelming. +-x # Stop running the tests after the first failure is reached. +-v # Prints the full test name rather than a dot for each test. +--with-id # If -v is also used, will number the tests for easy counting. +``` + +Due to high demand, pytest support has been added. You can run the above sample script in pytest like this: + +```bash +py.test my_first_test.py --with-selenium --with-testing_base --browser=chrome -s + +py.test my_first_test.py --with-selenium --with-testing_base --browser=phantomjs -s + +py.test my_first_test.py --with-selenium --with-testing_base --browser=firefox -s +``` + +(NOTE: I'm currently adding more pytest plugins to catch up with nosetests. The latest one added is "--with-testing_base", which gives you full logging on test failures for screenshots, page source, and basic test info. Coming soon: The DB and S3 plugins, which are already available with nosetests.) + +#### **Step 6:** Complete the setup + +If you're planning on using the full power of this test framework, there are a few more things you'll want to do: + +* Setup your [Jenkins](http://jenkins-ci.org/) build server for running your tests at regular intervals. (Or you can use any build server you want.) + +* Setup an [Amazon S3](http://aws.amazon.com/s3/) account for saving your log files and screenshots for future viewing. This test framework already has the code you need to connect to it. (Modify the s3_manager.py file from the seleniumbase/core folder with connection details to your instance.) + +* Install [MySQL Workbench](http://dev.mysql.com/downloads/tools/workbench/) to make life easier by giving you a nice GUI tool that you can use to read & write from your DB directly. + +* Setup your Selenium Grid and update your *.cfg file to point there. An example config file called selenium_server_config_example.cfg has been provided for you in the grid folder. The start-selenium-node.bat and start-selenium-server.sh files are for running your grid. In an example situation, your Selenium Grid server might live on a unix box and your Selenium Grid nodes might live on EC2 Windows virtual machines. When your build server runs a Selenium test, it would connect to your Selenium Grid to find out which Grid browser nodes are available to run that test. To simplify things, you can use [Browser Stack](https://www.browserstack.com/automate) as your entire Selenium Grid (and let them do all the fun work of maintaining the grid for you). + +* There are ways of running your tests from Jenkins without having to utilize a remote machine. One way is by using PhantomJS as your browser (it runs headlessly). Another way is by using Xvfb (another headless system). [There's a plugin for Xvfb in Jenkins](https://wiki.jenkins-ci.org/display/JENKINS/Xvfb+Plugin). Here are some more helpful resources I found regarding the use of Xvfb: +1. http://stackoverflow.com/questions/6183276/how-do-i-run-selenium-in-xvfb +2. http://qxf2.com/blog/xvfb-plugin-for-jenkins-selenium/ +3. http://stackoverflow.com/questions/27202131/firefox-started-by-selenium-ignores-the-display-created-by-pyvirtualdisplay + +* If you use [Slack](https://slack.com), you can easily have your Jenkins jobs display results there by using the [Jenkins Slack Plugin](https://github.com/jenkinsci/slack-plugin). Another way to send messages from your tests to Slack is by using [Slack's Incoming Webhooks API](https://api.slack.com/incoming-webhooks). + +* If you use [HipChat](https://www.hipchat.com/), you can easily have your Jenkins jobs display results there by using the [Jenkins HipChat Plugin](https://wiki.jenkins-ci.org/display/JENKINS/HipChat+Plugin). Another way is by using the hipchat_reporting plugin, which is included with this test framework. + +* Be sure to tell SeleniumBase to use these added features when you set them up. That's easy to do. You would be running tests like this: + +```bash +nosetests [YOUR_TEST_FILE].py --browser=chrome --with-selenium --with-testing_base --with-basic_test_info --with-page_source --with-screen_shots --with-db_reporting --with-s3_logging -s +``` + +(When the testing_base plugin is used, if there's a test failure, the basic_test_info plugin records test logs, the page_source plugin records the page source of the last web page seen by the test, and the screen_shots plugin records the image of the last page seen by the test where the failure occurred. Make sure you always include testing_base whenever you include a plugin that logs test data. The db_reporting plugin records the status of all tests as long as you've setup your MySQL DB properly and you've also updated your seleniumbase/core/mysql_conf.py file with your DB credentials.) +To simplify that long run command, you can create a *.cfg file, such as the one provided in the example, and enter your plugins there so that you can run everything by typing: + +```bash +nosetests [YOUR_TEST_FILE].py --config=[MY_CONFIG_FILE].cfg -s +``` + +So much easier on the eyes :) +You can simplify that even more by using a setup.cfg file, such as the one provided for you in the examples folder. If you kick off a test run from within the folder that setup.cfg is location in, that file will automatically be used as your configuration, meaning that you wouldn't have to type out all the plugins that you want to use (or include a config file) everytime you run tests. + +If you tell nosetests to run an entire file, it will run every method in that python file that starts with "test". You can be more specific on what to run by doing something like: + +```bash +nosetests [YOUR_TEST_FILE].py:[SOME_CLASS_NAME].test_[SOME_TEST_NAME] --config=[MY_CONFIG_FILE].cfg -s +``` + +Let's try an example of a test that fails. Copy the following into a file called fail_test.py: +```python +""" test_fail.py """ +from seleniumbase import BaseCase + +class MyTestClass(BaseCase): + + def test_find_army_of_robots_on_xkcd_desert_island(self): + self.driver.get("http://xkcd.com/731/") + self.wait_for_element_visible("div#ARMY_OF_ROBOTS", timeout=3) # This should fail +``` +Now run it: + +```bash +nosetests test_fail.py --browser=chrome --with-selenium --with-testing_base --with-basic_test_info --with-page_source --with-screen_shots -s +``` + +You'll notice that a logs folder was created to hold information about the failing test, and screenshots. Take a look at what you get. Remember, this data can be saved in your MySQL DB and in S3 if you include the necessary plugins in your run command (and if you set up the neccessary connections properly). For future test runs, past test results will get stored in the archived_logs folder. + +Have you made it this far? Congratulations!!! Now you're ready to dive in at full speed! + + +## Part II: Detailed Method Specifications, Examples + +#### Navigating to a Page, Plus Some Other Useful Related Commands + +```python +self.driver.get("https://xkcd.com/378/") # Instant navigation to any web page. + +self.driver.refresh() # refresh/reload the current page. + +where_am_i = self.driver.current_url # this variable changes as the current page changes. + +source = self.driver.page_source # this variable changes as the page source changes. +``` + +**ProTip™:** You may need to use the page_source method along with Python's find() command to parse through the source to find something that Selenium wouldn't be able to. (You may want to brush up on your Python programming skills if you're confused.) +Ex: +```python +source = self.driver.page_source +first_image_open_tag = source.find('') +first_image_close_tag = source.find'', first_image_open_tag) +everything_inside_first_image_tags = source[first_image_open_tag+len(''):first_image_close_tag] +``` + +#### Clicking + +To click an element on the page: + +```python +self.click("div#my_id") +``` + +#### Asserting existance of an element on a page within some number of seconds: + +```python +self.wait_for_element_present("div.my_class", timeout=10) +``` + +#### Asserting visibility of an element on a page within some number of seconds: + +```python +self.wait_for_element_visible("a.my_class", timeout=5) +``` + +You can even combine visibility checking and clicking into one statement like so: + +```python +self.wait_for_element_visible("a.my_class", timeout=5).click() +``` + +#### Asserting visibility of text inside an element on a page within some number of seconds: + +```python +self.wait_for_text_visible("Make it so!", "div#trek div.picard div.quotes", timeout=3) +self.wait_for_text_visible("Tea. Earl Grey. Hot.", "div#trek div.picard div.quotes", timeout=1) +``` + +#### Asserting Anything + +```python +self.assertTrue(myvar1 == something) + +self.assertEqual(var1, var2) +``` + +#### Useful Conditional Statements (with creative examples in action) + +is_element_visible(selector) # is an element visible on a page +```python +import logging +if self.is_element_visible('div#warning'): + logging.debug("Red Alert: Something bad might be happening!") +``` + +is_element_present(selector) # is an element present on a page +```python +if self.is_element_present('div#top_secret img.tracking_cookie'): + self.contact_cookie_monster() # Not a real method unless you define it somewhere +else: + current_url = self.driver.current_url + self.contact_the_nsa(url=current_url, message="Dark Zone Found") # Not a real method unless you define it somewhere +``` +Another example: +```python +def is_there_a_cloaked_klingon_ship_on_this_page(): + if self.is_element_present("div.ships div.klingon"): + return not self.is_element_visible("div.ships div.klingon") + return False +``` + +is_text_visible(text, selector) # is text visible on a page +```python +def get_mirror_universe_captain_picard_superbowl_ad(superbowl_year): + selector = "div.superbowl_%s div.commercials div.transcript div.picard" % superbowl_year + if self.is_text_visible("For the Love of Marketing and Earl Grey Tea!", selector): + return "Picard HubSpot Superbowl Ad 2015" + elif self.is_text_visible("Delivery Drones... Engage", selector): + return "Picard Amazon Superbowl Ad 2015" + elif self.is_text_visible("Bing it on Screen!", selector): + return "Picard Microsoft Superbowl Ad 2015" + elif self.is_text_visible("OK Glass, Make it So!", selector): + return "Picard Google Superbowl Ad 2015" + elif self.is_text_visible("Number One, I've Never Seen Anything Like It.", selector): + return "Picard Tesla Superbowl Ad 2015" + elif self.is_text_visible("""With the first link, the chain is forged. + The first speech censored, the first thought forbidden, + the first freedom denied, chains us all irrevocably.""", selector): + return "Picard Wikimedia Superbowl Ad 2015" + elif self.is_text_visible("Let us make sure history never forgets the name ... Facebook", selector): + return "Picard Facebook Superbowl Ad 2015" + else: + raise Exception("Reports of my assimilation are greatly exaggerated.") +``` + +#### Typing Text + +update_text_value(selector, text) # updates the text from the specified element with the specified value. Exception raised if element missing or field not editable. Example: + +```python +self.update_text_value("input#id_value", "2012") +``` + +You can also use the WebDriver .send_keys() command, but it won't clear the text box first if there's already text inside. +If you want to type in special keys, that's easy too. Here's an example: + +```python +from selenium.webdriver.common.keys import Keys +self.wait_for_element_visible("textarea").send_keys(Keys.SPACE + Keys.BACK_SPACE + '\n') # the backspace should cancel out the space, leaving you with the newline +``` + +#### Switching Tabs + +What if your test opens up a new tab/window and now you have more than one page? No problem. You need to specify which one you currently want Selenium to use. Switching between tabs/windows is easy: +Ex: + +```python +self.driver.switch_to_window(self.driver.window_handles[1]) # this switches to the new tab +``` + +driver.window_handles is a list that will continually get updated when new windows/tabs appear (index numbering is auto-incrementing from 0, which represents the main window) + +**ProTip™:** iFrames follow the same principle as new windows - you need to specify the iFrame if you want to take action on something in there +Ex: + +```python +self.driver.switch_to_frame('ContentManagerTextBody_ifr') +# Now you can act inside the iFrame +# Do something cool (here) +self.driver.switch_to_default_content() # exit the iFrame when you're done +``` + +#### Handle Pop-Up Alerts + +What if your test makes an alert pop up in your browser? No problem. You need to switch to it and either accept it or dismiss it: +Ex: + +```python +self.driver.switch_to_alert().accept() + +self.driver.switch_to_alert().dismiss() +``` + +If you're not sure whether there's an alert before trying to accept or dismiss it, one way to handle that is to wrap your alert-handling code in a try/except block. Other methods such as .text and .send_keys() will also work with alerts. + +#### Executing Custom jQuery Scripts: + +jQuery is a powerful JavaScript library that allows you to perform advanced actions in a web browser. +If the web page you're on already has jQuery loaded, you can start executing jQuery scripts immediately. +You'd know this because the web page would contain something like the following in the HTML: + +```html + +``` + +It's OK if you want to use jQuery on a page that doesn't have it loaded yet. To do so, run the following command first: + +```python +self.activate_jquery() +``` + +Here are some examples of using jQuery in your scripts: +```python +self.execute_script('jQuery, window.scrollTo(0, 600)') # Scrolling the page + +self.execute_script("jQuery('#annoying-widget').hide()") # Hiding elements on a page + +self.execute_script("jQuery('#annoying-button a').remove()") # Removing elements on a page + +self.execute_script("jQuery('%s').mouseover()" % (mouse_over_item)) # Mouse-over elements on a page + +self.execute_script("jQuery('input#the_id').val('my_text')") # Fast text input on a page + +self.execute_script("jQuery('div#dropdown a.link').click()") # Click elements on a page + +self.execute_script("return jQuery('div#amazing')[0].text") # Returns the css "text" of the element given + +self.execute_script("return jQuery('textarea')[2].value") # Returns the css "value" of the 3rd textarea element on the page +``` + +In the following more-complex example, jQuery is used to plant code on a page that Selenium can then touch after that: +```python +self.driver.get(SOME_PAGE_TO_PLAY_WITH) +referral_link = 'Free-Referral Button!' % DESTINATION_URL +self.execute_script("document.body.innerHTML = \"%s\"" % referral_link) +self.click("a.analytics") # Clicks the generated button +``` + +## Part III: More Details + +Nosetests automatically runs any python method that starts with "test" from the file you selected. You can also select specific tests to run from files or classes. For example, the code in the early examples could've been run using "nosetests my_first_test.py:MyTestClass.test_basic ... ...". If you wanted to run all tests in MyTestClass, you can use: "nosetests my_first_test.py:MyTestClass ... ...", which is useful when you have multiple tests in the same file. Don't forget the plugins. Use "-s" if you want better logging in the console output. + +To use the SeleniumBase Test Framework calls, don't forget to include the following import: + +```python +from seleniumbase import BaseCase +``` + +And you'll need to inherit BaseCase in your classes like so: + +```python +class MyTestClass(BaseCase): +``` + +#### Checking Email: +Let's say you have a test that sends an email, and now you want to check that the email was received: + +```python +from seleniumbase.fixtures.email_manager import EmailManager, EmailException +num_email_results = 0 +email_subject = "This is the subject to search for (maybe include a timestamp)" +email_manager = EmailManager("[YOUR SELENIUM GMAIL EMAIL ADDRESS]") # the password for this is elsewhere (in the library) because this is a default email account +try: + html_text = email_manager.search(SUBJECT="%s" % email_subject, timeout=300) + num_email_results = len(html_text) +except EmailException: + num_email_results = 0 +self.assertTrue(num_email_results) # true if not zero +``` + +Now you can parse through the email if you're looking for specific text or want to navigate to a link listed there. + + +#### Database Powers: +Let's say you have a test that needs to access the database. First make sure you already have a table ready. Then try this example: + +```python +from seleniumbase.core.mysql import DatabaseManager +def write_data_to_db(self, theId, theValue, theUrl): + db = DatabaseManager() + query = """INSERT INTO myTable(theId,theValue,theUrl) + VALUES (%(theId)s,%(theValue)s,%(theUrl)s)""" + db.execute_query_and_close(query, {"theId":theId, + "theValue":theValue, + "theUrl":theUrl}) +``` + +Access credentials are stored in your library file for your convenience (you have to add them first). + +The following example below (taken from the Delayed Data Manager) shows how data can be pulled from the database. + +```python +import logging +from seleniumbase.core.mysql import DatabaseManager + +def get_delayed_test_data(self, testcase_address, done=0): + """ Returns a list of rows """ + db = DatabaseManager() + query = """SELECT guid,testcaseAddress,insertedAt,expectedResult,done + FROM delayedTestData + WHERE testcaseAddress=%(testcase_address)s + AND done=%(done)s""" + data = db.fetchall_query_and_close(query, {"testcase_address":testcase_address, "done":done}) + if data: + return data + else: + logging.debug("Could not find any rows in delayedTestData.") + logging.debug("DB Query = " + query % {"testcase_address":testcase_address, "done":done}) + return [] +``` + +Now you know how to pull data from your MySQL DB. + +You may also be wondering when you would use the Delayed Data Manager. Here's one example: If you scheduled an email to go out 12 hours from now and you wanted to check that the email gets received (but you don't want the Selenium test of a Jenkins job to sit idle for 12 hours) you can store the email credentials as a unique time-stamp for the email subject in the DB (along with a time for when it's safe for the email to be searched for) and then a later-running test can do the checking after the right amount of time has passed. + + +Congratulations! If you've made it this far, it means you have a pretty good idea about how to move forward! +Feel free to check out other exciting open source projects on GitHub: +[https://github.com/hubspot](https://github.com/hubspot) + +Happy Automating! + +~ Michael Mintz (https://github.com/mdmintz) + + +### Legal Disclaimer +Automation is a powerful tool. It allows you to take full control of web browsers and do almost anything that a human could do, but faster. It can be used for both good and evil. With great power comes great responsibility. You are fully responsible for how you use this framework and the automation that you create. You may also want to see an expert when it comes to setting up your automation environment if you require assistance. diff --git a/conftest.py b/conftest.py new file mode 100755 index 00000000..8d0a0a39 --- /dev/null +++ b/conftest.py @@ -0,0 +1,87 @@ +""" This is the pytest configuration file """ + +import os +import shutil +import pytest +import time +from seleniumbase.config import settings +from seleniumbase.fixtures import constants + + +def pytest_addoption(parser): + parser.addoption('--browser', action="store", + dest='browser', + choices=constants.Browser.VERSION.keys(), + default=constants.Browser.FIREFOX, + help="""Specifies the web browser to use. Default=FireFox. + If you want to use Chrome, explicitly indicate that. + Example: (--browser=chrome)""") + parser.addoption('--is_pytest', action="store_true", + dest='is_pytest', + default=True, + help="""This is used by the BaseCase class to tell apart + pytest runs from nosetest runs.""") + parser.addoption('--data', dest='data', + default=None, + help='Extra data to pass from the command line.') + parser.addoption('--with-selenium', action="store_true", + dest='with_selenium', + default=False, + help="Use if tests need to be run with a web browser.") + parser.addoption('--with-testing_base', action="store_true", + dest='with_testing_base', + default=False, + help="Use to save logs (screenshots) when tests fail.") + parser.addoption('--log_path', dest='log_path', + default='logs/', + help='Where the log files are saved.') + + +def pytest_configure(config): + with_selenium = config.getoption('with_selenium') + with_testing_base = config.getoption('with_testing_base') + browser = config.getoption('browser') + log_path = config.getoption('log_path') + data = '' + if config.getoption('data') is not None: + data = config.getoption('data') + # Create a temporary config file while tests are running + pytest_config = '.pytest_config' + config_file = open(pytest_config, 'w+') + config_file.write("with_selenium:::%s\n" % with_selenium) + config_file.write("browser:::%s\n" % browser) + config_file.write("data:::%s\n" % data) + config_file.write("with_testing_base:::%s\n" % with_testing_base) + config_file.write("log_path:::%s\n" % log_path) + config_file.close() + + +def pytest_unconfigure(): + pytest_config = '.pytest_config' + if os.path.isfile(pytest_config): + os.remove(pytest_config) + + +def pytest_runtest_setup(): + # Handle Logging + with_testing_base = pytest.config.getoption('with_testing_base') + if with_testing_base: + log_path = pytest.config.getoption('log_path') + if log_path.endswith("/"): + log_path = log_path[:-1] + if not os.path.exists(log_path): + os.makedirs(log_path) + else: + archived_folder = "%s/../archived_logs/" % log_path + if not os.path.exists(archived_folder): + os.makedirs(archived_folder) + archived_logs = "%slogs_%s" % (archived_folder, int(time.time())) + shutil.move(log_path, archived_logs) + os.makedirs(log_path) + if not settings.ARCHIVE_EXISTING_LOGS: + shutil.rmtree(archived_logs) + + +def pytest_runtest_teardown(): + # A placeholder for a method that runs after every test with pytest + pass diff --git a/docker/docker-entrypoint.sh b/docker/docker-entrypoint.sh new file mode 100755 index 00000000..b4756f92 --- /dev/null +++ b/docker/docker-entrypoint.sh @@ -0,0 +1,4 @@ +#!/bin/bash +set -e +echo "***** SeleniumBase Docker Machine *****" +exec "$@" diff --git a/docker/docker_config.cfg b/docker/docker_config.cfg new file mode 100755 index 00000000..48c63cc7 --- /dev/null +++ b/docker/docker_config.cfg @@ -0,0 +1,6 @@ +[nosetests] +with-selenium_docker=1 +with-testing_base=1 +with-basic_test_info=1 +nocapture=1 +logging-level=INFO diff --git a/docker/docker_requirements.txt b/docker/docker_requirements.txt new file mode 100755 index 00000000..b9667ac0 --- /dev/null +++ b/docker/docker_requirements.txt @@ -0,0 +1,14 @@ +selenium==2.48.0 +nose==1.3.7 +pytest==2.8.3 +flake8==2.5.0 +requests==2.7.0 +urllib3==1.10.4 +BeautifulSoup==3.2.1 +unittest2==1.1.0 +chardet==2.3.0 +simplejson==3.7.3 +boto==2.38.0 +pdb==0.1 +ipdb==0.8.1 +pyvirtualdisplay==0.1.5 diff --git a/docker/docker_setup.py b/docker/docker_setup.py new file mode 100755 index 00000000..06206c2b --- /dev/null +++ b/docker/docker_setup.py @@ -0,0 +1,37 @@ +""" +The setup package to install the SeleniumBase Test Framework plugins +on a development machine that DOES NOT intend to write to +a MySQL DB during test runs. +""" + +from setuptools import setup, find_packages # noqa + +setup( + name='seleniumbase', + version='1.1.7', + author='Michael Mintz', + author_email='@mintzworld', + maintainer='Michael Mintz', + description='''The SeleniumBase Test Framework. + (Powered by Python, WebDriver, and more...)''', + license='The MIT License', + packages=['seleniumbase', + 'seleniumbase.core', + 'seleniumbase.plugins', + 'seleniumbase.fixtures', + 'seleniumbase.common', + 'seleniumbase.config'], + entry_points={ + 'nose.plugins': [ + 'base_plugin = seleniumbase.plugins.base_plugin:Base', + 'selenium_docker = ' + 'seleniumbase.plugins.docker_selenium_plugin:SeleniumBrowser', + 'page_source = seleniumbase.plugins.page_source:PageSource', + 'screen_shots = seleniumbase.plugins.screen_shots:ScreenShots', + 'test_info = seleniumbase.plugins.basic_test_info:BasicTestInfo', + 's3_logging = seleniumbase.plugins.s3_logging_plugin:S3Logging', + 'hipchat_reporting = seleniumbase.plugins' + '.hipchat_reporting_plugin:HipchatReporting', + ] + } + ) diff --git a/docker/run_docker_test_in_firefox.sh b/docker/run_docker_test_in_firefox.sh new file mode 100755 index 00000000..a4e93e53 --- /dev/null +++ b/docker/run_docker_test_in_firefox.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e +# Run example test from inside Docker image +echo "Running example SeleniumBase test from Docker with headless Firefox..." +cd /SeleniumBase/examples/ && nosetests my_first_test.py --config=docker_config.cfg --browser=firefox +exec "$@" diff --git a/docker/run_docker_test_in_phantomjs.sh b/docker/run_docker_test_in_phantomjs.sh new file mode 100755 index 00000000..7e903e86 --- /dev/null +++ b/docker/run_docker_test_in_phantomjs.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e +# Run example test from inside Docker image +echo "Running example SeleniumBase test from Docker with PhantomJS..." +cd /SeleniumBase/examples/ && nosetests my_first_test.py --config=docker_config.cfg --browser=phantomjs +exec "$@" diff --git a/examples/ReadMe.txt b/examples/ReadMe.txt new file mode 100755 index 00000000..0cf7b323 --- /dev/null +++ b/examples/ReadMe.txt @@ -0,0 +1,9 @@ +The python tests here are in nosetest format, which means that you CANNOT run them by using “python [NAME OF .PY FILE]” in the command prompt. To make running these files easy, .sh files have been created. Those contain the run commands to properly execute the python tests. + +On a MAC or Unix-based system, you can execute .sh files by using ./[NAME OF .SH FILE] in a command prompt from the folder that the .sh files are located in. On a Windows-based system .bat files work the same way. You can switch the file extensions from .sh to .bat if you need to. One .bat file has been included in this folder. + +You may have trouble opening .cfg files if you want to try viewing/editing them because the file extension may be unrecognized on your system. If so, use the Right-Click “Open With” option, or just drag & drop the file into a text-editing program. + +If you run scripts with logging enabled, you’ll see two folders appear: “logs” and “archived logs”. The “logs” folder will contain log files from the most recent test run, but logs will only be created if the test run is failing. Afterwards, logs from the “logs” folder will get pushed to the “archived_logs” folder. + +You may also see .pyc files appear as you run tests. That’s compiled bytecode, which is a natural result of running Python code. diff --git a/examples/__init__.py b/examples/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/examples/example_config.cfg b/examples/example_config.cfg new file mode 100755 index 00000000..f30d9032 --- /dev/null +++ b/examples/example_config.cfg @@ -0,0 +1,9 @@ +[nosetests] +with-selenium=1 +with-testing_base=1 +with-page_source=1 +with-screen_shots=1 +with-basic_test_info=1 +nocapture=1 +logging-level=INFO +browser=chrome diff --git a/examples/my_first_test.py b/examples/my_first_test.py new file mode 100755 index 00000000..13e21ad2 --- /dev/null +++ b/examples/my_first_test.py @@ -0,0 +1,19 @@ +from seleniumbase import BaseCase + + +class MyTestClass(BaseCase): + + def test_basic(self): + self.open("http://xkcd.com/353/") + self.wait_for_element_visible("div#comic") + self.click('a[rel="license"]') + text = self.wait_for_element_visible('center').text + self.assertTrue("reuse any of my drawings" in text) + self.assertTrue("You can use them freely" in text) + self.open("http://xkcd.com/1481/") + self.click_link_text('Blag') + self.wait_for_text_visible("blag of the webcomic", "#site-description") + self.update_text_value("input#s", "Robots!\n") + self.wait_for_text_visible("Hooray robots!", "#content") + self.open("http://xkcd.com/1319/") + self.wait_for_text_visible("Automation", "div#ctitle") diff --git a/examples/rate_limiting_test.py b/examples/rate_limiting_test.py new file mode 100755 index 00000000..6917f854 --- /dev/null +++ b/examples/rate_limiting_test.py @@ -0,0 +1,14 @@ +from seleniumbase import BaseCase +from seleniumbase.common import decorators + + +class MyTestClass(BaseCase): + + @decorators.rate_limited(4) + def print_item(self, item): + print item + + def test_rate_limited_printing(self): + print "\nRunning rate-limited print test:" + for item in xrange(10): + self.print_item(item) diff --git a/examples/run_my_first_test_in_chrome.sh b/examples/run_my_first_test_in_chrome.sh new file mode 100755 index 00000000..dfd41c2c --- /dev/null +++ b/examples/run_my_first_test_in_chrome.sh @@ -0,0 +1 @@ +nosetests my_first_test.py --browser=chrome --with-selenium --logging-level=INFO -s \ No newline at end of file diff --git a/examples/run_my_first_test_in_firefox.sh b/examples/run_my_first_test_in_firefox.sh new file mode 100755 index 00000000..a8598246 --- /dev/null +++ b/examples/run_my_first_test_in_firefox.sh @@ -0,0 +1 @@ +nosetests my_first_test.py --browser=firefox --with-selenium --logging-level=INFO -s \ No newline at end of file diff --git a/examples/run_my_first_test_in_phantomjs.sh b/examples/run_my_first_test_in_phantomjs.sh new file mode 100755 index 00000000..7af38aef --- /dev/null +++ b/examples/run_my_first_test_in_phantomjs.sh @@ -0,0 +1 @@ +nosetests my_first_test.py --browser=phantomjs --with-selenium --logging-level=INFO -s diff --git a/examples/run_my_first_test_with_config.sh b/examples/run_my_first_test_with_config.sh new file mode 100755 index 00000000..259f6f67 --- /dev/null +++ b/examples/run_my_first_test_with_config.sh @@ -0,0 +1 @@ +nosetests my_first_test.py --config=example_config.cfg \ No newline at end of file diff --git a/examples/run_rate_limiting_test.sh b/examples/run_rate_limiting_test.sh new file mode 100755 index 00000000..323f3224 --- /dev/null +++ b/examples/run_rate_limiting_test.sh @@ -0,0 +1 @@ +nosetests decorator_test.py -s \ No newline at end of file diff --git a/examples/run_test_fail_with_debug.sh b/examples/run_test_fail_with_debug.sh new file mode 100755 index 00000000..56d24f62 --- /dev/null +++ b/examples/run_test_fail_with_debug.sh @@ -0,0 +1 @@ +nosetests test_fail.py --browser=chrome --with-selenium --logging-level=INFO --pdb --pdb-failures -s \ No newline at end of file diff --git a/examples/run_test_fail_with_debug_in_firefox.sh b/examples/run_test_fail_with_debug_in_firefox.sh new file mode 100755 index 00000000..660a8f54 --- /dev/null +++ b/examples/run_test_fail_with_debug_in_firefox.sh @@ -0,0 +1 @@ +nosetests test_fail.py --browser=firefox --with-selenium --logging-level=INFO --pdb --pdb-failures -s \ No newline at end of file diff --git a/examples/run_test_fail_with_logging.sh b/examples/run_test_fail_with_logging.sh new file mode 100755 index 00000000..f92a81b9 --- /dev/null +++ b/examples/run_test_fail_with_logging.sh @@ -0,0 +1 @@ +nosetests test_fail.py --browser=chrome --with-selenium --logging-level=INFO --with-testing_base --with-basic_test_info --with-page_source --with-screen_shots -s \ No newline at end of file diff --git a/examples/run_test_fail_with_logging_in_firefox.sh b/examples/run_test_fail_with_logging_in_firefox.sh new file mode 100755 index 00000000..fc97ca8d --- /dev/null +++ b/examples/run_test_fail_with_logging_in_firefox.sh @@ -0,0 +1 @@ +nosetests test_fail.py --browser=firefox --with-selenium --logging-level=INFO --with-testing_base --with-basic_test_info --with-page_source --with-screen_shots -s \ No newline at end of file diff --git a/examples/setup.cfg b/examples/setup.cfg new file mode 100755 index 00000000..061dbdd1 --- /dev/null +++ b/examples/setup.cfg @@ -0,0 +1,6 @@ +[nosetests] + +; This is the config file for default values used during nosetest runs + +nocapture=1 ; Displays print statements from output. Undo this by using: --nologcapture +logging-level=INFO ; INFO keeps the logs much cleaner than using DEBUG diff --git a/examples/test_fail.py b/examples/test_fail.py new file mode 100755 index 00000000..228009ae --- /dev/null +++ b/examples/test_fail.py @@ -0,0 +1,11 @@ +""" This test was made to fail on purpose to demonstrate the + logging capabilities of the SeleniumBase Test Framework """ + +from seleniumbase import BaseCase + + +class MyTestClass(BaseCase): + + def test_find_army_of_robots_on_xkcd_desert_island(self): + self.driver.get("http://xkcd.com/731/") + self.wait_for_element_visible("div#ARMY_OF_ROBOTS", timeout=0.5) diff --git a/grid_files/Grid_Hub_Server_README.md b/grid_files/Grid_Hub_Server_README.md new file mode 100644 index 00000000..5d10cc28 --- /dev/null +++ b/grid_files/Grid_Hub_Server_README.md @@ -0,0 +1,18 @@ +## Notes on using the Selenium Grid Hub + +The Selenium Grid Hub allows you to distribute tests to run in parallel across multiple machines. Each machine can then run its own allocation of tests in parallel. This allows you to run an entire test suite quickly, which may be important if you have a lot of tests to run. Machines can be personal computers, data centers, or virtual machines in the cloud. You can also create your own virtual machine by using a tool such as Docker (see the [Docker_README](https://github.com/mdmintz/SeleniumBase/blob/master/Docker_README.md)). + +### Running the Selenium Grid Hub + +You may need to download selenium-server-standalone-2.48.2.jar (or the latest version) separately. That file is not present with this repository to save space. You can download that file from here: +* http://docs.seleniumhq.org/download/ +or here: +* http://selenium-release.storage.googleapis.com/index.html?path=2.48/ +Once you have downloaded the jar file, put it in this folder (the "grid_files" folder). + +More detailed info about connecting to the Selenium Grid Hub can be found here: +* https://theintern.github.io/intern/#selenium-grid +and here: +* https://github.com/SeleniumHQ/selenium/wiki/Grid2 +For even more information, look here: +* https://github.com/SeleniumHQ/selenium/wiki diff --git a/grid_files/selenium_server_config_example.cfg b/grid_files/selenium_server_config_example.cfg new file mode 100644 index 00000000..6fb4f5ee --- /dev/null +++ b/grid_files/selenium_server_config_example.cfg @@ -0,0 +1,12 @@ +[nosetests] +with-xunit=1 +with-selenium=1 +server=[IF NOT RUNNING THE TESTS LOCALLY, ENTER_YOUR_SELENIUM_SERVER_HOSTNAME_HERE - MIGHT BE YOUR OWN, OR ON AN EC2 MACHINE, PORT 4444 LIKELY, OR YOU MIGHT BE USING BROWSERSTACK: *.browserstack.com, PORT 80 LIKELY. IF RUNNING LOCALLY REMOVE THIS ENTIRE LINE AND THE LINE WITH THE "PORT"!] +port=4444 +with-testing_base=1 +with-page_source=1 +with-screen_shots=1 +with-s3_logging=1 +with-db_reporting=1 +with-basic_test_info=1 +nocapture=0 diff --git a/grid_files/start-selenium-node.bat b/grid_files/start-selenium-node.bat new file mode 100644 index 00000000..6668d1bf --- /dev/null +++ b/grid_files/start-selenium-node.bat @@ -0,0 +1,2 @@ +cd c:\ +java -jar selenium-server-standalone-2.48.2.jar -role node -hub http://[ENTER URL OF THE GRID HUB SERVER]:4444/grid/register -browser browserName=chrome,maxInstances=5 -browser browserName=firefox,maxInstances=5 -browser browserName="internet explorer",maxInstances=1 \ No newline at end of file diff --git a/grid_files/start-selenium-server.sh b/grid_files/start-selenium-server.sh new file mode 100755 index 00000000..ce62a2b5 --- /dev/null +++ b/grid_files/start-selenium-server.sh @@ -0,0 +1,2 @@ +#!/bin/bash +java -jar selenium-server-standalone-2.48.2.jar -role hub \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100755 index 00000000..0304f628 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,14 @@ +selenium==2.48.0 +nose==1.3.7 +pytest==2.8.3 +flake8==2.5.0 +requests==2.7.0 +urllib3==1.10.4 +BeautifulSoup==3.2.1 +unittest2==1.1.0 +chardet==2.3.0 +simplejson==3.7.3 +boto==2.38.0 +pdb==0.1 +ipdb==0.8.1 +-e . diff --git a/seleniumbase/__init__.py b/seleniumbase/__init__.py new file mode 100755 index 00000000..c733c1ab --- /dev/null +++ b/seleniumbase/__init__.py @@ -0,0 +1 @@ +from seleniumbase.fixtures.base_case import BaseCase # noqa diff --git a/seleniumbase/common/__init__.py b/seleniumbase/common/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/seleniumbase/common/decorators.py b/seleniumbase/common/decorators.py new file mode 100755 index 00000000..8c08deb1 --- /dev/null +++ b/seleniumbase/common/decorators.py @@ -0,0 +1,66 @@ +import logging +import math +import threading +import time +from functools import wraps + + +def retry_on_exception(tries=6, delay=1, backoff=2, max_delay=32): + ''' + Decorator for implementing exponential backoff for retrying on failures. + + tries: Max number of tries to execute the wrapped function before failing. + delay: Delay time in seconds before the FIRST retry. + backoff: Multiplier to extend the initial delay by for each retry. + max_delay: Max time in seconds to wait between retries. + ''' + tries = math.floor(tries) + if tries < 1: + raise ValueError('"tries" must be greater than or equal to 1.') + if delay < 0: + raise ValueError('"delay" must be greater than or equal to 0.') + if backoff < 1: + raise ValueError('"backoff" must be greater than or equal to 1.') + if max_delay < delay: + raise ValueError('"max_delay" must be greater than or equal to delay.') + + def decorated_function_with_retry(func): + @wraps(func) + def function_to_retry(*args, **kwargs): + local_tries, local_delay = tries, delay + while local_tries > 1: + try: + return func(*args, **kwargs) + except Exception, e: + if local_delay > max_delay: + local_delay = max_delay + logging.exception('%s: Retrying in %d seconds...' + % (str(e), local_delay)) + time.sleep(local_delay) + local_tries -= 1 + local_delay *= backoff + return func(*args, **kwargs) + return function_to_retry + return decorated_function_with_retry + + +def rate_limited(max_per_second): + min_interval = 1.0 / float(max_per_second) + + def decorate(func): + last_time_called = [0.0] + rate_lock = threading.Lock() # To support multi-threading + + def rate_limited_function(*args, **kargs): + try: + rate_lock.acquire(True) + elapsed = time.clock() - last_time_called[0] + wait_time_remaining = min_interval - elapsed + if wait_time_remaining > 0: + time.sleep(wait_time_remaining) + last_time_called[0] = time.clock() + finally: + rate_lock.release() + return func(*args, **kargs) + return rate_limited_function + return decorate diff --git a/seleniumbase/config/__init__.py b/seleniumbase/config/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/seleniumbase/config/settings.py b/seleniumbase/config/settings.py new file mode 100755 index 00000000..6ae63fc1 --- /dev/null +++ b/seleniumbase/config/settings.py @@ -0,0 +1,67 @@ +""" +You'll probably want to customize this to your own environment and needs. +""" + + +# #####>>>>>----- REQUIRED/IMPORTANT SETTINGS -----<<<<<##### + +# Default times to wait for page elements to appear before performing actions +SMALL_TIMEOUT = 5 +LARGE_TIMEOUT = 10 +EXTREME_TIMEOUT = 30 + +# If True, existing logs from past test runs will be saved and take up space. +# If False, only the logs from the most recent test run will be saved locally. +# This has no effect on Jenkins/S3/MySQL, which may still be saving test logs. +ARCHIVE_EXISTING_LOGS = False + +# Default names for files saved during test failures when logging is turned on. +# (These files will get saved to the "logs/" folder) +SCREENSHOT_NAME = "screenshot.jpg" +BASIC_INFO_NAME = "basic_test_info.txt" +PAGE_SOURCE_NAME = "page_source.html" + +''' This adds wait_for_ready_state_complete() after various browser actions. + By default, Selenium waits for the 'interactive' state before continuing. + Setting this to True may improve reliability at the cost of speed. + WARNING: Some websites are in a perpetual "interactive" state due to + dynamic content that never fully finishes loading (Use "False" there). ''' +# Called after self.open(url) or self.open_url(url), NOT self.driver.open(url) +WAIT_FOR_RSC_ON_PAGE_LOADS = False +# Called after self.click(selector), NOT element.click() +WAIT_FOR_RSC_ON_CLICKS = False + + +# #####>>>>>----- RECOMMENDED SETTINGS -----<<<<<##### +# ##### (For test logging and database reporting) + +# Amazon S3 Bucket Credentials +# (For saving screenshots and other log files from tests) +S3_LOG_BUCKET = "[ENTER LOG BUCKET FOLDER NAME HERE]" +S3_BUCKET_URL = ("http://[ENTER SUBDOMAIN OF AMAZON BUCKET URL HERE]" + ".s3-[ENTER S3 REGION HERE].amazonaws.com/") +S3_SELENIUM_ACCESS_KEY = "[ENTER YOUR S3 ACCESS KEY FOR SELENIUM HERE]" +S3_SELENIUM_SECRET_KEY = "[ENTER YOUR S3 SECRET KEY FOR SELENIUM HERE]" + +# MySQL DB Credentials +# (For saving data from tests) +DB_HOST = "[TEST DB HOST]" # Ex: "127.0.0.1" +DB_USERNAME = "[TEST DB USERNAME]" # Ex: "root" +DB_PASSWORD = "[TEST DB PASSWORD]" # Ex: "test" +DB_SCHEMA = "[TEST DB SCHEMA]" # Ex: "test" + + +# #####>>>>>----- OPTIONAL SETTINGS -----<<<<<##### +# ##### (For reading emails, notifying people via chat apps, etc.) + +# Default Email Credentials +# (If tests send out emails, you can scan and verify them by using IMAP) +EMAIL_USERNAME = "[TEST ACCOUNT GMAIL USERNAME]@gmail.com" +EMAIL_PASSWORD = "[TEST ACCOUNT GMAIL PASSWORD]" +EMAIL_IMAP_STRING = "imap.gmail.com" +EMAIL_IMAP_PORT = 993 + +# HipChat Reporting Credentials +# (For HipChat notifications if your team uses HipChat) +# (room_id and owner_to_mention get entered during nosetest options) +HIPCHAT_AUTH_TOKEN = "[ENTER YOUR HIPCHAT AUTH TOKEN HERE]" diff --git a/seleniumbase/core/__init__.py b/seleniumbase/core/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/seleniumbase/core/application_manager.py b/seleniumbase/core/application_manager.py new file mode 100755 index 00000000..c6b9112b --- /dev/null +++ b/seleniumbase/core/application_manager.py @@ -0,0 +1,26 @@ +""" +Method for generating application strings used in the Testcase Database. +""" + +import time + + +class ApplicationManager: + """ + This class contains methods to generate application strings. + """ + + @classmethod + def generate_application_string(cls, test): + """ Generate an application string based on some of the given information + that can be pulled from the test object: app_env, start_time. """ + + app_env = 'test' + if hasattr(test, 'env'): + app_env = test.env + elif hasattr(test, 'environment'): + app_env = test.environment + + start_time = int(time.time() * 1000) + + return "%s.%s" % (app_env, start_time) diff --git a/seleniumbase/core/browser_launcher.py b/seleniumbase/core/browser_launcher.py new file mode 100755 index 00000000..dae574de --- /dev/null +++ b/seleniumbase/core/browser_launcher.py @@ -0,0 +1,28 @@ +from selenium import webdriver +from seleniumbase.fixtures import constants + + +def get_driver(browser_name): + ''' + Spins up a new web browser and returns the driver. + Tests that run with pytest spin up the browser from here. + Can also be used to spin up additional browsers for the same test. + ''' + if browser_name == constants.Browser.FIREFOX: + try: + profile = webdriver.FirefoxProfile() + profile.set_preference("reader.parse-on-load.enabled", False) + return webdriver.Firefox(profile) + except: + return webdriver.Firefox() + if browser_name == constants.Browser.INTERNET_EXPLORER: + return webdriver.Ie() + if browser_name == constants.Browser.PHANTOM_JS: + return webdriver.PhantomJS() + if browser_name == constants.Browser.GOOGLE_CHROME: + try: + chrome_options = webdriver.ChromeOptions() + chrome_options.add_argument("--allow-file-access-from-files") + return webdriver.Chrome(chrome_options=chrome_options) + except Exception: + return webdriver.Chrome() diff --git a/seleniumbase/core/log_helper.py b/seleniumbase/core/log_helper.py new file mode 100755 index 00000000..b3c8a537 --- /dev/null +++ b/seleniumbase/core/log_helper.py @@ -0,0 +1,75 @@ +import codecs +import sys +import traceback +from seleniumbase.config import settings + + +def log_screenshot(test_logpath, driver): + screenshot_name = settings.SCREENSHOT_NAME + screenshot_path = "%s/%s" % (test_logpath, screenshot_name) + driver.get_screenshot_as_file(screenshot_path) + + +def log_test_failure_data(test_logpath, driver, browser): + basic_info_name = settings.BASIC_INFO_NAME + basic_file_path = "%s/%s" % (test_logpath, basic_info_name) + log_file = codecs.open(basic_file_path, "w+", "utf-8") + last_page = get_last_page(driver) + data_to_save = [] + data_to_save.append("Last_Page: %s" % last_page) + data_to_save.append("Browser: %s " % browser) + data_to_save.append("Traceback: " + ''.join( + traceback.format_exception(sys.exc_info()[0], + sys.exc_info()[1], + sys.exc_info()[2]))) + log_file.writelines("\r\n".join(data_to_save)) + log_file.close() + + +def log_page_source(test_logpath, driver): + html_file_name = settings.PAGE_SOURCE_NAME + try: + page_source = driver.page_source + except Exception: + # Since we can't get the page source from here, skip saving it + return + html_file_path = "%s/%s" % (test_logpath, html_file_name) + html_file = codecs.open(html_file_path, "w+", "utf-8") + rendered_source = get_html_source_with_base_href(driver, page_source) + html_file.write(rendered_source) + html_file.close() + + +def get_last_page(driver): + try: + last_page = driver.current_url + except Exception: + last_page = '[WARNING! Browser Not Open!]' + if len(last_page) < 5: + last_page = '[WARNING! Browser Not Open!]' + return last_page + + +def get_base_url(full_url): + protocol = full_url.split('://')[0] + simple_url = full_url.split('://')[1] + base_url = simple_url.split('/')[0] + full_base_url = "%s://%s" % (protocol, base_url) + return full_base_url + + +def get_base_href_html(full_url): + ''' The base href line tells the html what the base page really is. + This is important when trying to open the page outside it's home. ''' + base_url = get_base_url(full_url) + return '' % base_url + + +def get_html_source_with_base_href(driver, page_source): + ''' Combines the domain base href with the html source. + This is needed for the page html to render correctly. ''' + last_page = get_last_page(driver) + if '://' in last_page: + base_href_html = get_base_href_html(last_page) + return '%s\n%s' % (base_href_html, page_source) + return '' diff --git a/seleniumbase/core/mysql.py b/seleniumbase/core/mysql.py new file mode 100755 index 00000000..6907d01c --- /dev/null +++ b/seleniumbase/core/mysql.py @@ -0,0 +1,68 @@ +""" +Wrapper for MySQL functions to make life easier +""" + +import time +import mysql_conf as conf + + +class DatabaseManager(): + """ + This class wraps database functions for easy use. + It connects to the testcase database. + """ + + def __init__(self, database_env='test', conf_creds=None): + """ + Gets database information from mysql_conf.py and creates a connection. + """ + import MySQLdb + db_server, db_user, db_pass, db_schema = \ + conf.APP_CREDS[conf.Apps.TESTCASE_REPOSITORY][database_env] + retry_count = 3 + backoff = 3 # Time to wait (in seconds) between retries + count = 0 + while count < retry_count: + try: + self.conn = MySQLdb.connect(host=db_server, + user=db_user, + passwd=db_pass, + db=db_schema) + self.conn.autocommit(True) + self.cursor = self.conn.cursor() + return + except Exception: + time.sleep(backoff) + count = count + 1 + if retry_count == 3: + raise Exception("Unable to connect to Database after 3 retries.") + + def fetchall_query_and_close(self, query, values): + """ + Executes a query, gets all the values and then closes up the connection + """ + self.cursor.execute(query, values) + retval = self.cursor.fetchall() + self.__close_db() + return retval + + def fetchone_query_and_close(self, query, values): + """ + Executes a query, gets the first value, and closes up the connection + """ + self.cursor.execute(query, values) + retval = self.cursor.fetchone() + self.__close_db() + return retval + + def execute_query_and_close(self, query, values): + """ + Executes a query and closes the connection + """ + retval = self.cursor.execute(query, values) + self.__close_db() + return retval + + def __close_db(self): + self.cursor.close() + self.conn.close() diff --git a/seleniumbase/core/mysql_conf.py b/seleniumbase/core/mysql_conf.py new file mode 100755 index 00000000..57fc5cef --- /dev/null +++ b/seleniumbase/core/mysql_conf.py @@ -0,0 +1,25 @@ +""" +This file contains database credentials for the various databases +that the tests need to access +""" + +from seleniumbase.config import settings + +# Environments +TEST = "test" + + +class Apps: + TESTCASE_REPOSITORY = "testcase_repository" + +APP_CREDS = { + + Apps.TESTCASE_REPOSITORY: { + TEST: ( + settings.DB_HOST, + settings.DB_USERNAME, + settings.DB_PASSWORD, + settings.DB_SCHEMA) + }, + +} diff --git a/seleniumbase/core/s3_manager.py b/seleniumbase/core/s3_manager.py new file mode 100755 index 00000000..289c1068 --- /dev/null +++ b/seleniumbase/core/s3_manager.py @@ -0,0 +1,79 @@ +""" +Manager for dealing with uploading/managing files on S3 +""" +from boto.s3.connection import S3Connection +from boto.s3.key import Key +from seleniumbase.config import settings + +already_uploaded_files = [] + + +class S3LoggingBucket(object): + """ + A class to upload our log files from tests to S3, from + whence we can share them. + """ + + def __init__(self, + log_bucket=settings.S3_LOG_BUCKET, + bucket_url=settings.S3_BUCKET_URL, + selenium_access_key=settings.S3_SELENIUM_ACCESS_KEY, + selenium_secret_key=settings.S3_SELENIUM_SECRET_KEY): + + self.conn = S3Connection(selenium_access_key, + selenium_secret_key) + self.bucket = self.conn.get_bucket(log_bucket) + self.bucket_url = bucket_url + + def get_key(self, _name): + """create a new Key instance with the given name""" + return Key(bucket=self.bucket, name=_name) + + def get_bucket(self): + """return the bucket we're using""" + return self.bucket + + def upload_file(self, file_name, file_path): + """upload a given file from the file_path to the bucket + with the new name/path file_name""" + upload_key = Key(bucket=self.bucket, name=file_name) + content_type = "text/plain" + if file_name.endswith(".html"): + content_type = "text/html" + if file_name.endswith(".jpg"): + content_type = "image/jpg" + upload_key.set_contents_from_filename( + file_path, + headers={"Content-Type": content_type}) + upload_key.url = \ + upload_key.generate_url(expires_in=3600).split("?")[0] + try: + upload_key.make_public() + except: + pass + return file_name + + def upload_index_file(self, test_address, timestamp): + """create an index.html file with links to all the log files we + just uploaded""" + global already_uploaded_files + already_uploaded_files = list(set(already_uploaded_files)) + already_uploaded_files.sort() + file_name = "%s/%s/index.html" % (test_address, timestamp) + index = self.get_key(file_name) + index_str = [] + for completed_file in already_uploaded_files: + index_str.append("%s" % (completed_file, completed_file)) + index.set_contents_from_string( + "
".join(index_str), + headers={"Content-Type": "text/html"}) + index.make_public() + return "%s%s" % (self.bucket_url, file_name) + + def save_uploaded_file_names(self, files): + """We keep record of file names that have been uploaded. We upload log + files related to each test after its execution. Once we're done, we + use already_uploaded_files to create an index file""" + global already_uploaded_files + already_uploaded_files.extend(files) diff --git a/seleniumbase/core/selenium_launcher.py b/seleniumbase/core/selenium_launcher.py new file mode 100755 index 00000000..d578c021 --- /dev/null +++ b/seleniumbase/core/selenium_launcher.py @@ -0,0 +1,93 @@ +""" Download and run the selenium server jar file """ + +import subprocess +import os +import socket +import urllib +import time + +SELENIUM_JAR = ("http://selenium-release.storage.googleapis.com" + "/2.48/selenium-server-standalone-2.48.2.jar") +JAR_FILE = "selenium-server-standalone-2.48.2.jar" + + +def download_selenium(): + """ + Downloads the selenium server jar file from its + online location and stores it locally. + """ + try: + local_file = open(JAR_FILE, 'wb') + remote_file = urllib.urlopen(SELENIUM_JAR) + print 'Please wait, downloading Selenium...\n' + local_file.write(remote_file.read()) + local_file.close() + remote_file.close() + except Exception, details: + raise Exception("Error while downloading Selenium Server. Details: %s" + % details) + + +def is_running_locally(host, port): + socket_s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + socket_s.connect((host, port)) + socket_s.close() + return True + except: + return False + + +def is_available_locally(): + return os.path.isfile(JAR_FILE) + + +def start_selenium_server(selenium_jar_location, port, file_path): + + """ + Starts selenium on the specified port + and configures the output and error files. + Throws an exeption if the server does not start. + """ + + process_args = None + process_args = ["java", "-jar", selenium_jar_location, "-port", port] + selenium_exec = subprocess.Popen( + process_args, + stdout=open("%s/log_seleniumOutput.txt" % (file_path), "w"), + stderr=open("%s/log_seleniumError.txt" % (file_path), "w")) + time.sleep(2) + if selenium_exec.poll() == 1: + raise StartSeleniumException("The selenium server did not start." + "Do you already have one runing?") + return selenium_exec + + +def stop_selenium_server(selenium_server_process): + """Kills the selenium server. We are expecting an error 143""" + + try: + selenium_server_process.terminate() + return selenium_server_process.poll() == 143 + except Exception, details: + raise Exception("Cannot kill selenium process, details: " + details) + + +class StartSeleniumException(Exception): + + def __init__(self, value): + self.value = value + + def __str__(self): + return repr(self.value) + + +def execute_selenium(host, port, file_path): + if is_running_locally(host, port): + return + if not is_available_locally(): + download_selenium() + try: + return start_selenium_server(JAR_FILE, port, file_path) + except StartSeleniumException: + print "Selenium Server might already be running. Continuing... " diff --git a/seleniumbase/core/testcase_manager.py b/seleniumbase/core/testcase_manager.py new file mode 100755 index 00000000..633d4e67 --- /dev/null +++ b/seleniumbase/core/testcase_manager.py @@ -0,0 +1,133 @@ +""" +Testcase database related methods +""" + +from seleniumbase.core.mysql import DatabaseManager + + +class TestcaseManager: + """ + Helper for Testcase related DB stuff + """ + + def __init__(self, database_env): + self.database_env = database_env + + def insert_execution_data(self, execution_query_payload): + """ Inserts an execution into the database. + Returns the execution guid. """ + + query = """INSERT INTO execution + (guid, executionStart, totalExecutionTime, username) + VALUES (%(guid)s,%(execution_start_time)s, + %(total_execution_time)s,%(username)s)""" + DatabaseManager(self.database_env).execute_query_and_close( + query, + execution_query_payload.get_params()) + return execution_query_payload.guid + + def update_execution_data(self, execution_guid, execution_time): + """updates an existing execution in the database""" + + query = """UPDATE execution + SET totalExecutionTime=%(execution_time)s + WHERE guid=%(execution_guid)s """ + DatabaseManager(self.database_env).execute_query_and_close( + query, + {"execution_guid": execution_guid, + "execution_time": execution_time}) + + def insert_testcase_data(self, testcase_run_payload): + """inserts all data for the test case, returns the new row guid""" + + query = """INSERT INTO testcaseRunData + (guid, browser, state, execution_guid, env, start_time, + testcaseAddress, runtime, retryCount, message, stackTrace) + VALUES ( + %(guid)s, + %(browser)s, + %(state)s, + %(execution_guid)s, + %(env)s, + %(start_time)s, + %(testcaseAddress)s, + %(runtime)s, + %(retryCount)s, + %(message)s, + %(stackTrace)s) """ + DatabaseManager(self.database_env).execute_query_and_close( + query, testcase_run_payload.get_params()) + + def update_testcase_data(self, testcase_payload): + """updates an existing testcase run in the database""" + + query = """UPDATE testcaseRunData SET + runtime=%(runtime)s, + state=%(state)s, + retryCount=%(retryCount)s, + stackTrace=%(stackTrace)s, + message=%(message)s + WHERE guid=%(guid)s """ + DatabaseManager(self.database_env).execute_query_and_close( + query, testcase_payload.get_params()) + + def update_testcase_log_url(self, testcase_payload): + """updates an existing testcase run's logging URL in the database""" + + query = """UPDATE testcaseRunData + SET logURL=%(logURL)s + WHERE guid=%(guid)s """ + DatabaseManager(self.database_env).execute_query_and_close( + query, testcase_payload.get_params()) + + +class ExecutionQueryPayload: + """ Helper class for containing the execution query data """ + def __init__(self): + self.execution_start_time = None + self.total_execution_time = -1 + self.username = "Default" + self.guid = None + + def get_params(self): + """ Returns a params object for use with the pool """ + return { + "execution_start_time": self.execution_start_time, + "total_execution_time": self.total_execution_time, + "username": self.username, + "guid": self.guid + } + + +class TestcaseDataPayload: + """ Helper class for containing all the testcase query data """ + def __init__(self): + self.guid = None + self.testcaseAddress = None + self.browser = None + self.state = None + self.execution_guid = None + self.env = None + self.start_time = None + self.runtime = None + self.retry_count = 0 + self.stack_trace = None + self.message = None + self.logURL = None + + def get_params(self): + """ Returns a params object for use with the pool """ + return { + "guid": self.guid, + "testcaseAddress": self.testcaseAddress, + "browser": self.browser, + "state": self.state, + "execution_guid": self.execution_guid, + "env": self.env, + "start_time": self.start_time, + "runtime": self.runtime, + "retryCount": self.retry_count, + "stackTrace": self.stack_trace, + "message": self.message, + "logURL": self.logURL + } diff --git a/seleniumbase/core/testcaserepository.sql b/seleniumbase/core/testcaserepository.sql new file mode 100755 index 00000000..e8bb2d61 --- /dev/null +++ b/seleniumbase/core/testcaserepository.sql @@ -0,0 +1,41 @@ +# table delayedTestData +# ----------------------------------- +CREATE TABLE `delayedTestData` ( + `guid` varchar(64) NOT NULL DEFAULT '', + `testcaseAddress` varchar(255) NOT NULL DEFAULT '', + `insertedAt` bigint(20) NOT NULL, + `expectedResult` text, + `done` tinyint(1) DEFAULT '0', + `expiresAt` bigint(20) DEFAULT NULL, + PRIMARY KEY (`guid`), + UNIQUE KEY `uuid` (`guid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +# table execution +# ----------------------------------- +CREATE TABLE `execution` ( + `guid` varchar(64) NOT NULL DEFAULT '', + `totalExecutionTime` int(11), + `username` varchar(255) DEFAULT NULL, + `executionStart` bigint(20) DEFAULT '0', + PRIMARY KEY (`guid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +# table testcaseRunData +# ----------------------------------- +CREATE TABLE `testcaseRunData` ( + `guid` varchar(64) NOT NULL DEFAULT '', + `testcaseAddress` varchar(255) DEFAULT NULL, + `env` varchar(64) DEFAULT NULL, + `start_time` varchar(64) DEFAULT NULL, + `execution_guid` varchar(64) DEFAULT NULL, + `runtime` int(11), + `state` varchar(64) DEFAULT NULL, + `browser` varchar(64) DEFAULT NULL, + `message` text, + `stackTrace` text, + `retryCount` int(11) DEFAULT '0', + `exceptionMap_guid` varchar(64) DEFAULT NULL, + `logURL` text, + PRIMARY KEY (`guid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/seleniumbase/fixtures/__init__.py b/seleniumbase/fixtures/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py new file mode 100755 index 00000000..9fbac178 --- /dev/null +++ b/seleniumbase/fixtures/base_case.py @@ -0,0 +1,238 @@ +""" +These methods improve on and expand existing WebDriver commands. +Improvements include making WebDriver commands more robust and more reliable +by giving page elements enough time to load before taking action on them. +""" + +import json +import logging +import os +import pytest +import sys +import unittest +from seleniumbase.config import settings +from seleniumbase.core import browser_launcher +from seleniumbase.core import log_helper +from selenium.webdriver.remote.webdriver import WebDriver +from selenium.webdriver.common.by import By +import page_actions +import page_utils + + +class BaseCase(unittest.TestCase): + ''' + A base test case that wraps a bunch of methods from tools + for easier access. You can also add your own methods here. + ''' + + def __init__(self, *args, **kwargs): + super(BaseCase, self).__init__(*args, **kwargs) + try: + self.driver = WebDriver() + except Exception: + pass + self.environment = None + + def setUp(self): + """ + pytest-specific code + Be careful if a subclass of BaseCase overrides setUp() + You'll need to add the following line to the subclass setUp() method: + super(SubClassOfBaseCase, self).setUp() + """ + self.is_pytest = None + try: + # This raises an exception if the test is not coming from pytest + self.is_pytest = pytest.config.option.is_pytest + except Exception: + # Not using pytest (probably nosetests) + self.is_pytest = False + if self.is_pytest: + self.with_selenium = pytest.config.option.with_selenium + self.with_testing_base = pytest.config.option.with_testing_base + self.log_path = pytest.config.option.log_path + self.browser = pytest.config.option.browser + self.data = pytest.config.option.data + if self.with_selenium: + self.driver = browser_launcher.get_driver(self.browser) + + def tearDown(self): + """ + pytest-specific code + Be careful if a subclass of BaseCase overrides setUp() + You'll need to add the following line to the subclass's tearDown(): + super(SubClassOfBaseCase, self).tearDown() + """ + if self.is_pytest: + if self.with_selenium: + # Save a screenshot if logging is on when an exception occurs + if self.with_testing_base and (sys.exc_info()[1] is not None): + test_id = "%s.%s.%s" % (self.__class__.__module__, + self.__class__.__name__, + self._testMethodName) + test_logpath = self.log_path + "/" + test_id + if not os.path.exists(test_logpath): + os.makedirs(test_logpath) + # Handle screenshot logging + log_helper.log_screenshot(test_logpath, self.driver) + # Handle basic test info logging + log_helper.log_test_failure_data( + test_logpath, self.driver, self.browser) + # Handle page source logging + log_helper.log_page_source(test_logpath, self.driver) + + # Finally close the browser + self.driver.quit() + + def open(self, url): + self.driver.get(url) + if settings.WAIT_FOR_RSC_ON_PAGE_LOADS: + self.wait_for_ready_state_complete() + + def open_url(self, url): + """ In case people are mixing up self.open() with open(), + use this alternative. """ + self.open(url) + + def click(self, selector, by=By.CSS_SELECTOR, + timeout=settings.SMALL_TIMEOUT): + element = page_actions.wait_for_element_visible( + self.driver, selector, by, timeout=timeout) + element.click() + if settings.WAIT_FOR_RSC_ON_CLICKS: + self.wait_for_ready_state_complete() + + def click_link_text(self, link_text, timeout=settings.SMALL_TIMEOUT): + element = self.wait_for_link_text_visible(link_text, timeout=timeout) + element.click() + if settings.WAIT_FOR_RSC_ON_CLICKS: + self.wait_for_ready_state_complete() + + def update_text_value(self, selector, new_value, + timeout=settings.SMALL_TIMEOUT, retry=False): + """ This method updates a selector's text value with a new value + @Params + selector - the selector with the value to update + new_value - the new value for setting the text field + timeout - how long to wait for the selector to be visible + retry - if True, use jquery if the selenium text update fails + """ + element = self.wait_for_element_visible(selector, timeout=timeout) + element.clear() + element.send_keys(new_value) + if (retry and element.get_attribute('value') != new_value + and not new_value.endswith('\n')): + logging.debug('update_text_value is falling back to jQuery!') + selector = self.jq_format(selector) + self.set_value(selector, new_value) + + def is_element_present(self, selector, by=By.CSS_SELECTOR): + return page_actions.is_element_present(self.driver, selector, by) + + def is_element_visible(self, selector, by=By.CSS_SELECTOR): + return page_actions.is_element_visible(self.driver, selector, by) + + def is_link_text_visible(self, link_text): + return page_actions.is_element_visible(self.driver, link_text, + by=By.LINK_TEXT) + + def is_text_visible(self, text, selector, by=By.CSS_SELECTOR): + return page_actions.is_text_visible(self.driver, text, selector, by) + + def find_visible_elements(self, selector, by=By.CSS_SELECTOR): + return page_actions.find_visible_elements(self.driver, selector, by) + + def execute_script(self, script): + return self.driver.execute_script(script) + + def set_window_size(self, width, height): + return self.driver.set_window_size(width, height) + + def maximize_window(self): + return self.driver.maximize_window() + + def activate_jquery(self): + """ (It's not on by default on all website pages.) """ + self.driver.execute_script( + '''var script = document.createElement("script"); ''' + '''script.src = "https://ajax.googleapis.com/ajax/libs/jquery/1/''' + '''jquery.min.js"; document.getElementsByTagName("head")[0]''' + '''.appendChild(script);''') + + def scroll_to(self, selector): + self.wait_for_element_visible(selector, timeout=settings.SMALL_TIMEOUT) + self.driver.execute_script( + "jQuery('%s')[0].scrollIntoView()" % selector) + + def scroll_click(self, selector): + self.scroll_to(selector) + self.click(selector) + + def jquery_click(self, selector): + self.driver.execute_script("jQuery('%s').click()" % selector) + + def jq_format(self, code): + return page_utils.jq_format(code) + + def set_value(self, selector, value): + val = json.dumps(value) + self.driver.execute_script("jQuery('%s').val(%s)" % (selector, val)) + + def jquery_update_text_value(self, selector, new_value, + timeout=settings.SMALL_TIMEOUT): + element = self.wait_for_element_visible(selector, timeout=timeout) + self.driver.execute_script("""jQuery('%s').val('%s')""" + % (selector, self.jq_format(new_value))) + if new_value.endswith('\n'): + element.send_keys('\n') + + def hover_on_element(self, selector): + return page_actions.hover_on_element(self.driver, selector) + + def hover_and_click(self, hover_selector, click_selector, + click_by=By.CSS_SELECTOR, + timeout=settings.SMALL_TIMEOUT): + return page_actions.hover_and_click(self.driver, hover_selector, + click_selector, click_by, timeout) + + def wait_for_element_present(self, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_element_present( + self.driver, selector, by, timeout) + + def wait_for_element_visible(self, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_element_visible( + self.driver, selector, by, timeout) + + def wait_for_text_visible(self, text, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_text_visible( + self.driver, text, selector, by, timeout) + + def wait_for_link_text_visible(self, link_text, + timeout=settings.LARGE_TIMEOUT): + return self.wait_for_element_visible( + link_text, by=By.LINK_TEXT, timeout=timeout) + + def wait_for_element_absent(self, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_element_absent( + self.driver, selector, by, timeout) + + def wait_for_element_not_visible(self, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_element_not_visible( + self.driver, selector, by, timeout) + + def wait_for_ready_state_complete(self, timeout=settings.EXTREME_TIMEOUT): + return page_actions.wait_for_ready_state_complete(self.driver, timeout) + + def wait_for_and_accept_alert(self, timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_and_accept_alert(self.driver, timeout) + + def wait_for_and_dismiss_alert(self, timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_and_dismiss_alert(self.driver, timeout) + + def wait_for_and_switch_to_alert(self, timeout=settings.LARGE_TIMEOUT): + return page_actions.wait_for_and_switch_to_alert(self.driver, timeout) diff --git a/seleniumbase/fixtures/constants.py b/seleniumbase/fixtures/constants.py new file mode 100755 index 00000000..a09c45b9 --- /dev/null +++ b/seleniumbase/fixtures/constants.py @@ -0,0 +1,47 @@ +""" +This class containts some frequently-used constants +""" + + +class Environment: + QA = "qa" + STAGING = "staging" + PRODUCTION = "production" + MASTER = "master" + LOCAL = "local" + TEST = "test" + + +class Browser: + FIREFOX = "firefox" + INTERNET_EXPLORER = "ie" + SAFARI = "safari" + GOOGLE_CHROME = "chrome" + PHANTOM_JS = "phantomjs" + HTML_UNIT = "htmlunit" + + VERSION = { + "firefox": None, + "ie": None, + "chrome": None, + "phantomjs": None, + "htmlunit": None + } + + LATEST = { + "firefox": None, + "ie": None, + "chrome": None, + "phantomjs": None, + "htmlunit": None + } + + +class State: + NOTRUN = "NotRun" + ERROR = "Error" + FAILURE = "Fail" + PASS = "Pass" + SKIP = "Skip" + BLOCKED = "Blocked" + DEPRECATED = "Deprecated" diff --git a/seleniumbase/fixtures/delayed_data_manager.py b/seleniumbase/fixtures/delayed_data_manager.py new file mode 100755 index 00000000..be995c2c --- /dev/null +++ b/seleniumbase/fixtures/delayed_data_manager.py @@ -0,0 +1,141 @@ +import json +import logging +import time +import uuid +from seleniumbase.core.mysql import DatabaseManager + +DEFAULT_EXPIRATION = 1000 * 60 * 60 * 48 + + +class DelayedTestStorage: + """ The database-calling methods of the Delayed Test Framework """ + + @classmethod + def get_delayed_test_data(self, testcase_address, done=0): + """ This method queries the delayedTestData table in the DB and + then returns a list of rows with the matching parameters. + :param testcase_address: The ID (address) of the test case. + :param done: (0 for test not done or 1 for test done) + :returns: A list of rows found with the matching testcase_address. + """ + db = DatabaseManager() + query = """SELECT guid,testcaseAddress,insertedAt,expectedResult,done + FROM delayedTestData + WHERE testcaseAddress=%(testcase_address)s + AND done=%(done)s""" + data = db.fetchall_query_and_close( + query, {"testcase_address": testcase_address, + "done": done}) + if data: + return data + else: + logging.debug("Could not find any rows in delayedTestData.") + logging.debug("DB Query = " + query % + {"testcase_address": testcase_address, "done": done}) + return [] + + @classmethod + def insert_delayed_test_data(self, guid_, testcase_address, + expected_result, done=0, + expires_at=DEFAULT_EXPIRATION): + """ This method inserts rows into the delayedTestData table + in the DB based on the given parameters where + inserted_at (Date format) is automatically set in this method. + :param guid_: The guid that is provided by the test case. + (Format: str(uuid.uuid4())) + :param testcase_address: The ID (address) of the test case. + :param expected_result: The result string of persistent data + that will be stored in the DB. + :param done: (0 for test not done or 1 for test done) + :returns: True (when no exceptions or errors occur) + """ + inserted_at = int(time.time() * 1000) + + db = DatabaseManager() + query = """INSERT INTO delayedTestData( + guid,testcaseAddress,insertedAt, + expectedResult,done,expiresAt) + VALUES (%(guid)s,%(testcaseAddress)s,%(inserted_at)s, + %(expected_result)s,%(done)s,%(expires_at)s)""" + + db.execute_query_and_close( + query, {"guid": guid_, + "testcaseAddress": testcase_address, + "inserted_at": inserted_at, + "expected_result": expected_result, + "done": done, + "expires_at": inserted_at + expires_at}) + return True + + @classmethod + def set_delayed_test_to_done(self, guid_): + """ This method updates the delayedTestData table in the DB + to set the test with the selected guid to done. + :param guid_: The guid that is provided by the test case. + (Format: str(uuid.uuid4())) + :returns: True (when no exceptions or errors occur) + """ + db = DatabaseManager() + query = """UPDATE delayedTestData + SET done=TRUE + WHERE guid=%(guid)s + AND done=FALSE""" + db.execute_query_and_close(query, {"guid": guid_}) + return True + + +class DelayedTestAssistant: + """ Some methods for assisting tests (that don't call the DB directly) """ + + @classmethod + def get_delayed_results(self, test_id, seconds, set_done=True): + """ + This method gets the delayed_test_data and sets the applicable rows + in the DB to done. + The results is a list of dicts where each list item contains + item[0] = guid + item[1] = testcaseAddress + item[2] = seconds from epoch + item[3] = expected results dict encoded in json + :param test_id: the self.id() of the test + :param seconds: the wait period until the data can be checked + :returns: the results for a specific test where enough time has passed + """ + delayed_test_data = DelayedTestStorage.get_delayed_test_data( + testcase_address=test_id) + now = int(time.time() * 1000) + results_to_check = [] + if delayed_test_data is None: + return results_to_check + for item in delayed_test_data: + if item[2] < now - (seconds * 1000): + results_to_check.append(item) + if set_done: + DelayedTestStorage.set_delayed_test_to_done(item[0]) + return results_to_check + + @classmethod + def store_delayed_data(self, test_id, expected_result_dict, + expires_at=DEFAULT_EXPIRATION): + """ + Loads the dictionary of information into the delayed test database + :param test_id: the self.id() of the test + :param expected_result_dict: a dictionary of what's to be checked later + """ + expected_result_json = json.JSONEncoder().encode(expected_result_dict) + DelayedTestStorage.insert_delayed_test_data(str(uuid.uuid4()), + test_id, + expected_result_json, + 0, + expires_at) + + @classmethod + def set_test_done(self, test_guid): + """ This method calls set_delayed_test_to_done to set a + row in the db to done. + :param test_guid: The guid that is provided by the test. + (Format: str(uuid.uuid4())) + :returns: True (when no exceptions or errors occur) + """ + DelayedTestStorage.set_delayed_test_to_done(test_guid) + return True diff --git a/seleniumbase/fixtures/email_manager.py b/seleniumbase/fixtures/email_manager.py new file mode 100755 index 00000000..720766f4 --- /dev/null +++ b/seleniumbase/fixtures/email_manager.py @@ -0,0 +1,514 @@ +""" +EmailManager - a helper class to login, search for, and delete emails. +""" + +import email +import htmlentitydefs +import imaplib +import quopri +import re +import time +import types +from seleniumbase.config import settings + + +class EmailManager: + """ A helper class to interface with an Email account. These imap methods + can search for and fetch messages without needing a browser. + + Example: + + em = EmailManager() + result = em.check_for_recipient( + "[GMAIL.USER]+[SOME CODE OR TIMESTAMP KEY]@gmail.com") + """ + + HTML = "text/html" + PLAIN = "text/plain" + TIMEOUT = 1800 + + def __init__(self, uname=settings.EMAIL_USERNAME, + pwd=settings.EMAIL_PASSWORD, + imap_string=settings.EMAIL_IMAP_STRING, + port=settings.EMAIL_IMAP_PORT): + self.uname = uname + self.pwd = pwd + self.imap_string = imap_string + self.port = port + + def imap_connect(self): + """ + Connect to the IMAP mailbox. + """ + self.mailbox = imaplib.IMAP4_SSL(self.imap_string, self.port) + self.mailbox.login(self.uname, self.pwd) + self.mailbox.select() + + def imap_disconnect(self): + """ + Disconnect from the IMAP mailbox. + """ + self.mailbox.close() + self.mailbox.logout() + + def __imap_search(self, ** criteria_dict): + """ Searches for query in the given IMAP criteria and returns + the message numbers that match as a list of strings. + + Criteria without values (eg DELETED) should be keyword args + with KEY=True, or else not passed. Criteria with values should + be keyword args of the form KEY="VALUE" where KEY is a valid + IMAP key. + + IMAP default is to AND all criteria together. We don't support + other logic quite yet. + + All valid keys: ALL, ANSWERED, BCC , BEFORE , + BODY , CC , DELETED, DRAFT, FLAGGED, FROM + , HEADER (UNTESTED), KEYWORD + , LARGER , NEW, NOT , OLD, ON , + OR (UNTESTED), RECENT, SEEN, + SENTBEFORE , SENTON , SENTSINCE , SINCE , + SMALLER , SUBJECT , TEXT , TO , + UID , UNANSWERED, UNDELETED, UNDRAFT, UNFLAGGED, + UNKEYWORD , UNSEEN. + + For details on keys and their values, see + http://tools.ietf.org/html/rfc3501#section-6.4.4 + + :param criteria_dict: dictionary of search criteria keywords + :raises: EmailException if something in IMAP breaks + :returns: List of message numbers as strings matched by given criteria + """ + self.imap_connect() + + criteria = [] + for key in criteria_dict: + if criteria_dict[key] is True: + criteria.append('(%s)' % key) + else: + criteria.append('(%s "%s")' % (key, criteria_dict[key])) + + # If any of these criteria are not valid IMAP keys, IMAP will tell us. + status, msg_nums = self.mailbox.search('UTF-8', * criteria) + self.imap_disconnect() + + if 0 == len(msg_nums): + msg_nums = [] + + if 'OK' in status: + return self.__parse_imap_search_result(msg_nums) + else: + raise EmailException("IMAP status is " + str(status)) + + def remove_formatting(self, html): + """ + Clean out any whitespace + @Params + html - String of html to remove whitespace from + @Returns + Cleaned string + """ + return ' '.join(html.split()) + + def __parse_imap_search_result(self, result): + """ + This takes the result of imap_search and returns SANE results + @Params + result - result from an imap_search call + @Returns + List of IMAP search results + """ + if isinstance(result, types.ListType): + # Above is same as "type(result) == types.ListType" + if len(result) == 1: + return self.__parse_imap_search_result(result[0]) + else: + return result + elif isinstance(result, types.StringType): + # Above is same as "type(result) == types.StringType" + return result.split() + else: + # Fail silently assuming tests will fail if emails are not found + return [] + + def fetch_html(self, msg_nums): + """ + Given a message number that we found with imap_search, + get the text/html content. + @Params + msg_nums - message number to get html message for + @Returns + HTML content of message matched by message number + """ + if not msg_nums: + raise Exception("Invalid Message Number!") + + return self.__imap_fetch_content_type(msg_nums, self.HTML) + + def fetch_plaintext(self, msg_nums): + """ + Given a message number that we found with imap_search, + get the text/plain content. + @Params + msg_nums - message number to get message for + @Returns + Plaintext content of message matched by message number + """ + if not msg_nums: + raise Exception("Invalid Message Number!") + + return self.__imap_fetch_content_type(msg_nums, self.PLAIN) + + def __imap_fetch_content_type(self, msg_nums, content_type): + """ + Given a message number that we found with imap_search, fetch the + whole source, dump that into an email object, and pick out the part + that matches the content type specified. Return that, if we got + multiple emails, return dict of all the parts. + @Params + msg_nums - message number to search for + content_type - content type of email message to return + @Returns + Specified content type string or dict of all content types of matched + email. + """ + + if not msg_nums: + raise Exception("Invalid Message Number!") + if not content_type: + raise Exception("Need a content type!") + + contents = {} + self.imap_connect() + for num in msg_nums: + status, data = self.mailbox.fetch(num, "(RFC822)") + for response_part in data: + if isinstance(response_part, tuple): + msg = email.message_from_string(response_part[1]) + for part in msg.walk(): + if str(part.get_content_type()) == content_type: + content = str(part.get_payload(decode=True)) + contents[int(num)] = content + self.imap_disconnect() + return contents + + def fetch_html_by_subject(self, email_name): + """ + Get the html of an email, searching by subject. + @Params + email_name - the subject to search for + @Returns + HTML content of the matched email + """ + if not email_name: + raise EmailException("Subject cannot be null") + + results = self.__imap_search(SUBJECT=email_name) + sources = self.fetch_html(results) + + return sources + + def fetch_plaintext_by_subject(self, email_name): + """ + Get the plain text of an email, searching by subject. + @Params + email_name - the subject to search for + @Returns + Plaintext content of the matched email + """ + if not email_name: + raise EmailException("Subject cannot be null") + + results = self.__imap_search(SUBJECT=email_name) + sources = self.fetch_plaintext(results) + + return sources + + def search_for_recipient(self, email, timeout=None, content_type=None): + """ + Get content of emails, sent to a specific email address. + @Params + email - the recipient email address to search for + timeout - seconds to try beore timing out + content_type - type of email string to return + @Returns + Content of the matched email in the given content type + """ + return self.search(timeout=timeout, + content_type=content_type, TO=email) + + def search_for_subject(self, subject, timeout=None, content_type=None): + """ + Get content of emails, sent to a specific email address. + @Params + email - the recipient email address to search for + timeout - seconds to try beore timing out + content_type - type of email string to return + @Returns + Content of the matched email in the given content type + """ + return self.search(timeout=timeout, + content_type=content_type, SUBJECT=subject) + + def search_for_count(self, ** args): + """ + A search that keeps searching up until timeout for a + specific number of matches to a search. If timeout is not + specified we use the default. If count= is not specified we + will fail. Return values are the same as search(), except for count=0, + where we will return an empty list. Use this if you need to wait for a + number of emails other than 1. + + @Params + args - dict of arguments to use in search: + count - number of emails to search for + timeout - seconds to try search before timing out + @Returns + List of message numbers matched by search + """ + if "timeout" not in args.keys(): + timeout = self.TIMEOUT + elif args["timeout"]: + timeout = args["timeout"] + args["timeout"] = timeout / 15 + + if "count" not in args.keys(): + raise EmailException("Count param not defined!") + else: + count = int(args["count"]) + del args["count"] + + results = None + timer = timeout + count = 0 + while count < timer: + try: + results = self.search(** args) + except EmailException: + if count == 0: + return [] + + if results and len(results) == count: + return results + else: + time.sleep(15) + count += 15 + if count >= timer: + raise EmailException("Failed to match criteria %s in %s minutes" % + (args, timeout / 60)) + + def __check_msg_for_headers(self, msg, ** email_headers): + """ + Checks an Email.Message object for the headers in email_headers. + + Following are acceptable header names: ['Delivered-To', + 'Received', 'Return-Path', 'Received-SPF', + 'Authentication-Results', 'DKIM-Signature', + 'DomainKey-Signature', 'From', 'To', 'Message-ID', + 'Subject', 'MIME-Version', 'Content-Type', 'Date', + 'X-Sendgrid-EID', 'Sender']. + + @Params + msg - the Email.message object to check + email_headers - list of headers to check against + @Returns + Boolean whether all the headers were found + """ + all_headers_found = False + email_headers['Delivered-To'] = email_headers['To'] + email_headers.pop('To') + all_headers_found = all(k in msg.keys() for k in email_headers) + + return all_headers_found + + def fetch_message(self, msgnum): + """ + Given a message number, return the Email.Message object. + @Params + msgnum - message number to find + @Returns + Email.Message object for the given message number + """ + self.imap_connect() + status, data = self.mailbox.fetch(msgnum, "(RFC822)") + self.imap_disconnect() + + for response_part in data: + if isinstance(response_part, tuple): + return email.message_from_string(response_part[1]) + + def get_content_type(self, msg, content_type="HTML"): + """ + Given an Email.Message object, gets the content-type payload + as specified by @content_type. This is the actual body of the + email. + @Params + msg - Email.Message object to get message content for + content_type - Type of content to get from the email + @Return + String content of the email in the given type + """ + if "HTML" in content_type.upper(): + content_type = self.HTML + elif "PLAIN" in content_type.upper(): + content_type = self.PLAIN + + for part in msg.walk(): + if str(part.get_content_type()) == content_type: + return str(part.get_payload(decode=True)) + + def search(self, ** args): + """ + Checks email inbox every 15 seconds that match the criteria + up until timeout. + + Search criteria should be keyword args eg + TO="selenium@gmail.com". See __imap_search docstring for list + of valid criteria. If content_type is not defined, will return + a list of msg numbers. + + Options: + - fetch: will return a dict of Message objects, keyed on msgnum, + which can be used to look at headers and other parts of the complete + message. (http://docs.python.org/library/email.message.html) + - timeout: will replace the default module timeout with the + value in SECONDS. + - content_type: should be either "PLAIN" or + "HTML". If defined returns the source of the matched messages + as a dict of msgnum:content. If not defined we return a list + of msg nums. + """ + + if "content_type" not in args.keys(): + content_type = None + elif "HTML" in args["content_type"]: + content_type = self.HTML + del args["content_type"] + elif "PLAIN" in args["content_type"]: + content_type = self.PLAIN + del args["content_type"] + elif args["content_type"]: + content_type = args['content_type'] + del args["content_type"] + + if "timeout" not in args.keys(): + timeout = self.TIMEOUT + elif "timeout" in args: + timeout = args["timeout"] + del args["timeout"] + + fetch = False + if "fetch" in args.keys(): + fetch = True + del args["fetch"] + + results = None + timer = timeout + count = 0 + while count < timer: + results = self.__imap_search(** args) + if len(results) > 0: + if fetch: + msgs = {} + for msgnum in results: + msgs[msgnum] = self.fetch_message(msgnum) + return msgs + elif not content_type: + return results + else: + return self.__imap_fetch_content_type(results, + content_type) + else: + time.sleep(15) + count += 15 + if count >= timer: + raise EmailException( + "Failed to find message for criteria %s in %s minutes" % + (args, timeout / 60)) + + def remove_whitespace(self, html): + """ + Clean whitespace from html + @Params + html - html source to remove whitespace from + @Returns + String html without whitespace + """ + # Does python have a better way to do exactly this? + clean_html = html + for char in ("\r", "\n", "\t"): + clean_html = clean_html.replace(char, "") + return clean_html + + def remove_control_chars(self, html): + """ + Clean control characters from html + @Params + html - html source to remove control characters from + @Returns + String html without control characters + """ + return self.remove_whitespace(html) + + def replace_entities(self, html): + """ + Replace htmlentities with unicode characters + @Params + html - html source to replace entities in + @Returns + String html with entities replaced + """ + def fixup(text): + """replace the htmlentities in some text""" + text = text.group(0) + if text[:2] == "&#": + # character reference + try: + if text[:3] == "&#x": + return unichr(int(text[3:-1], 16)) + else: + return unichr(int(text[2:-1])) + except ValueError: + pass + else: + # named entity + try: + text = unichr(htmlentitydefs.name2codepoint[text[1:-1]]) + except KeyError: + pass + return text # leave as is + return re.sub("&#?\w+;", fixup, html) + + def decode_quoted_printable(self, html): + """ + Decoding from Quoted-printable, or QP encoding, that uses ASCII 7bit + chars to encode 8 bit chars, resulting in =3D to represent '='. Python + supports UTF-8 so we decode. Also removes line breaks with '= at the + end.' + @Params + html - html source to decode + @Returns + String decoded HTML source + """ + return self.replace_entities(quopri.decodestring(html)) + + def html_bleach(self, html): + """ + Cleanup and get rid of all extraneous stuff for better comparison + later. Turns formatted into into a single line string. + @Params + html - HTML source to clean up + @Returns + String cleaned up HTML source + """ + return self.decode_quoted_printable(html) + + +class EmailException(Exception): + """Raised when we have an Email-related problem.""" + def __init__(self, value): + self.parameter = value + + def __str__(self): + return repr(self.parameter) diff --git a/seleniumbase/fixtures/errors.py b/seleniumbase/fixtures/errors.py new file mode 100755 index 00000000..d3ff3c2b --- /dev/null +++ b/seleniumbase/fixtures/errors.py @@ -0,0 +1,20 @@ +""" +This module contains test-state related exceptions. +Raising one of these in a test will cause the +test-state to be logged appropriately. +""" + + +class BlockedTest(Exception): + """Raise this to mark a test as Blocked""" + pass + + +class SkipTest(Exception): + """Raise this to mark a test as Skipped.""" + pass + + +class DeprecatedTest(Exception): + """Raise this to mark a test as Deprecated.""" + pass diff --git a/seleniumbase/fixtures/page_actions.py b/seleniumbase/fixtures/page_actions.py new file mode 100755 index 00000000..4ee93d7f --- /dev/null +++ b/seleniumbase/fixtures/page_actions.py @@ -0,0 +1,341 @@ +""" +This module contains a set of methods that can be used for page loads and +for waiting for elements to appear on a page. + +These methods improve on and expand existing WebDriver commands. +Improvements include making WebDriver commands more robust and more reliable +by giving page elements enough time to load before taking action on them. + +The default option for searching for elements is by CSS Selector. +This can be changed by overriding the "By" parameter. +Options are: +By.CSS_SELECTOR +By.CLASS_NAME +By.ID +By.NAME +By.LINK_TEXT +By.XPATH +By.TAG_NAME +By.PARTIAL_LINK_TEXT +""" + +import time +from seleniumbase.config import settings +from selenium.webdriver.common.by import By +from selenium.webdriver.remote.errorhandler import ElementNotVisibleException +from selenium.webdriver.remote.errorhandler import NoSuchElementException +from selenium.webdriver.remote.errorhandler import NoAlertPresentException + + +def is_element_present(driver, selector, by=By.CSS_SELECTOR): + """ + Searches for the specified element by the given selector. Returns whether + the element object if the element is present on the page. + @Params + driver - the webdriver object (required) + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + @Returns + Boolean (is element present) + """ + try: + driver.find_element(by=by, value=selector) + return True + except Exception: + return False + + +def is_element_visible(driver, selector, by=By.CSS_SELECTOR): + """ + Searches for the specified element by the given selector. Returns whether + the element object if the element is present and visible on the page. + @Params + driver - the webdriver object (required) + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + @Returns + Boolean (is element visible) + """ + try: + element = driver.find_element(by=by, value=selector) + return element.is_displayed() + except Exception: + return False + + +def is_text_visible(driver, text, selector, by=By.CSS_SELECTOR): + """ + Searches for the specified element by the given selector. Returns whether + the element object if the element is present and visible on the page and + contains the given text. + @Params + driver - the webdriver object (required) + text - the text string to search for + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + @Returns + Boolean (is text visible) + """ + try: + element = driver.find_element(by=by, value=selector) + return element.is_displayed() and text in element.text + except Exception: + return False + + +def hover_on_element(driver, selector): + """ + Fires the hover event for the specified element by the given selector. + @Params + driver - the webdriver object (required) + selector - the locator (css selector) that is used (required) + """ + driver.execute_script("jQuery('%s').mouseover()" % selector) + + +def hover_and_click(driver, hover_selector, click_selector, + click_by=By.CSS_SELECTOR, timeout=settings.SMALL_TIMEOUT): + """ + Fires the hover event for a specified element by a given selector, then + clicks on another element specified. Useful for dropdown hover based menus. + @Params + driver - the webdriver object (required) + hover_selector - the css selector to hover over (required) + click_selector - the css selector to click on (required) + click_by - the method to search by (Default: By.CSS_SELECTOR) + timeout - number of seconds to wait for click element to appear after hover + """ + driver.execute_script("jQuery('%s').mouseover()" % (hover_selector)) + for x in range(int(timeout * 10)): + try: + element = driver.find_element(by=click_by, + value="%s" % click_selector).click() + return element + except Exception: + time.sleep(0.1) + raise NoSuchElementException( + "Element %s was not present after %s seconds!" % + (click_selector, timeout)) + + +def wait_for_element_present(driver, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + """ + Searches for the specified element by the given selector. Returns the + element object if the element is present on the page. The element can be + invisible. Raises an exception if the element does not appear in the + specified timeout. + @Params + driver - the webdriver object + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + timeout - the time to wait for elements in seconds + @Returns + A web element object + """ + + element = None + for x in range(int(timeout * 10)): + try: + element = driver.find_element(by=by, value=selector) + return element + except Exception: + time.sleep(0.1) + if not element: + raise NoSuchElementException( + "Element %s was not present in %s seconds!" % (selector, timeout)) + + +def wait_for_element_visible(driver, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + """ + Searches for the specified element by the given selector. Returns the + element object if the element is present and visible on the page. + Raises an exception if the element does not appear in the + specified timeout. + @Params + driver - the webdriver object (required) + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + timeout - the time to wait for elements in seconds + + @Returns + A web element object + """ + + element = None + for x in range(int(timeout * 10)): + try: + element = driver.find_element(by=by, value=selector) + if element.is_displayed(): + return element + else: + element = None + raise Exception() + except Exception: + time.sleep(0.1) + if not element: + raise ElementNotVisibleException( + "Element %s was not visible in %s seconds!" % (selector, timeout)) + + +def wait_for_text_visible(driver, text, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + """ + Searches for the specified element by the given selector. Returns the + element object if the text is present in the element and visible + on the page. Raises an exception if the text or element do not appear + in the specified timeout. + @Params + driver - the webdriver object (required) + text - the text that is being searched for in the element (required) + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + timeout - the time to wait for elements in seconds + @Returns + A web element object that contains the text searched for + """ + + element = None + for x in range(int(timeout * 10)): + try: + element = driver.find_element(by=by, value=selector) + if element.is_displayed(): + if text in element.text: + return element + else: + element = None + raise Exception() + except Exception: + time.sleep(0.1) + if not element: + raise ElementNotVisibleException( + "Expected text [%s] for [%s] was not visible after %s seconds!" % + (text, selector, timeout)) + + +def wait_for_element_absent(driver, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + """ + Searches for the specified element by the given selector. + Raises an exception if the element is still present after the + specified timeout. + @Params + driver - the webdriver object + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + timeout - the time to wait for elements in seconds + """ + + for x in range(int(timeout * 10)): + try: + driver.find_element(by=by, value=selector) + time.sleep(0.1) + except Exception: + return + raise Exception("Element %s was still present after %s seconds!" % + (selector, timeout)) + + +def wait_for_element_not_visible(driver, selector, by=By.CSS_SELECTOR, + timeout=settings.LARGE_TIMEOUT): + """ + Searches for the specified element by the given selector. + Raises an exception if the element is still visible after the + specified timeout. + @Params + driver - the webdriver object (required) + selector - the locator that is used (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + timeout - the time to wait for the element in seconds + """ + + for x in range(int(timeout * 10)): + try: + element = driver.find_element(by=by, value=selector) + if element.is_displayed(): + time.sleep(0.1) + else: + return + except Exception: + return + raise Exception( + "Element %s was still visible after %s seconds!" % (selector, timeout)) + + +def find_visible_elements(driver, selector, by=By.CSS_SELECTOR): + """ + Finds all WebElements that match a selector and are visible. + Similar to webdriver.find_elements. + @Params + driver - the webdriver object (required) + selector - the locator that is used to search the DOM (required) + by - the method to search for the locator (Default: By.CSS_SELECTOR) + """ + elements = driver.find_elements(by=by, value=selector) + return [element for element in elements if element.is_displayed()] + + +def wait_for_ready_state_complete(driver, timeout=settings.EXTREME_TIMEOUT): + """ + The DOM (Document Object Model) has a property called "readyState". + When the value of this becomes "complete", page resources are considered + fully loaded (although AJAX and other loads might still be happening). + This method will wait until document.readyState == "complete". + """ + + for x in range(int(timeout * 10)): + ready_state = driver.execute_script("return document.readyState") + if ready_state == u'complete': + return True + else: + time.sleep(0.1) + raise Exception( + "Page elements never fully loaded after %s seconds!" % timeout) + + +def wait_for_and_accept_alert(driver, timeout=settings.LARGE_TIMEOUT): + """ + Wait for and accept an alert. Returns the text from the alert. + @Params + driver - the webdriver object (required) + timeout - the time to wait for the alert in seconds + """ + alert = wait_for_and_switch_to_alert(driver, timeout) + alert_text = alert.text + alert.accept() + return alert_text + + +def wait_for_and_dismiss_alert(driver, timeout=settings.LARGE_TIMEOUT): + """ + Wait for and dismiss an alert. Returns the text from the alert. + @Params + driver - the webdriver object (required) + timeout - the time to wait for the alert in seconds + """ + alert = wait_for_and_switch_to_alert(driver, timeout) + alert_text = alert.text + alert.dismiss() + return alert_text + + +def wait_for_and_switch_to_alert(driver, timeout=settings.LARGE_TIMEOUT): + """ + Wait for a browser alert to appear, and switch to it. This should be usable + as a drop-in replacement for driver.switch_to_alert() when the alert box + may not exist yet. + @Params + driver - the webdriver object (required) + timeout - the time to wait for the alert in seconds + """ + + for x in range(int(timeout * 10)): + try: + alert = driver.switch_to_alert() + # Raises exception if no alert present + dummy_variable = alert.text # noqa + return alert + except NoAlertPresentException: + time.sleep(0.1) + raise Exception("Alert was not present after %s seconds!" % timeout) diff --git a/seleniumbase/fixtures/page_utils.py b/seleniumbase/fixtures/page_utils.py new file mode 100755 index 00000000..ec9ec870 --- /dev/null +++ b/seleniumbase/fixtures/page_utils.py @@ -0,0 +1,16 @@ +""" +This module contains useful utility methods. +""" + + +def jq_format(code): + """ + Use before throwing raw code such as 'div[tab="advanced"]' into jQuery. + Selectors with quotes inside of quotes would otherwise break jQuery. + This is similar to "json.dumps(value)", but with one less layer of quotes. + """ + code = code.replace('\\', '\\\\').replace('\t', '\\t').replace('\n', '\\n') + code = code.replace('\"', '\\\"').replace('\'', '\\\'') + code = code.replace('\v', '\\v').replace('\a', '\\a').replace('\f', '\\f') + code = code.replace('\b', '\\b').replace('\u', '\\u').replace('\r', '\\r') + return code diff --git a/seleniumbase/fixtures/tools.py b/seleniumbase/fixtures/tools.py new file mode 100755 index 00000000..53d8d403 --- /dev/null +++ b/seleniumbase/fixtures/tools.py @@ -0,0 +1,8 @@ +""" +This module imports all the commonly used fixtures in one place so that +every test doesn't need to import a number of different fixtures. +""" + +from seleniumbase.fixtures.page_actions import * # noqa +from seleniumbase.fixtures.page_utils import * # noqa +from seleniumbase.fixtures.errors import * # noqa diff --git a/seleniumbase/plugins/__init__.py b/seleniumbase/plugins/__init__.py new file mode 100755 index 00000000..e69de29b diff --git a/seleniumbase/plugins/base_plugin.py b/seleniumbase/plugins/base_plugin.py new file mode 100755 index 00000000..823fbc60 --- /dev/null +++ b/seleniumbase/plugins/base_plugin.py @@ -0,0 +1,107 @@ +""" +This plugin is for saving logs and setting a test environment. +Vars include "env" and "log_path". +You can have tests behave differently based on the environment. +You can access the values of these variables from the tests. +""" + +import os +import shutil +import time +from nose.plugins import Plugin +from nose.exc import SkipTest +from seleniumbase.config import settings +from seleniumbase.fixtures import constants, errors + + +class Base(Plugin): + """ + The base_plugin includes the following variables: + self.options.env -- the environment for the tests to use (--env=ENV) + self.options.data -- any extra data to pass to the tests (--data=DATA) + self.options.log_path -- the directory in which the log files + are saved (--log_path=LOG_PATH) + """ + name = 'testing_base' # Usage: --with-testing_base + + def options(self, parser, env): + super(Base, self).options(parser, env=env) + parser.add_option( + '--env', action='store', + dest='environment', + choices=( + constants.Environment.QA, + constants.Environment.STAGING, + constants.Environment.PRODUCTION, + constants.Environment.MASTER, + constants.Environment.LOCAL, + constants.Environment.TEST), + default=constants.Environment.TEST, + help="The environment to run the tests in.") + parser.add_option( + '--data', dest='data', + default=None, + help='Extra data to pass from the command line.') + parser.add_option( + '--log_path', dest='log_path', + default='logs/', + help='Where the log files are saved.') + + def configure(self, options, conf): + super(Base, self).configure(options, conf) + if not self.enabled: + return + self.options = options + log_path = options.log_path + if log_path.endswith("/"): + log_path = log_path[:-1] + if not os.path.exists(log_path): + os.makedirs(log_path) + else: + archived_folder = "%s/../archived_logs/" % log_path + if not os.path.exists(archived_folder): + os.makedirs(archived_folder) + archived_logs = "%slogs_%s" % (archived_folder, int(time.time())) + shutil.move(log_path, archived_logs) + os.makedirs(log_path) + if not settings.ARCHIVE_EXISTING_LOGS: + shutil.rmtree(archived_logs) + + def beforeTest(self, test): + test_logpath = self.options.log_path + "/" + test.id() + if not os.path.exists(test_logpath): + os.makedirs(test_logpath) + test.test.environment = self.options.environment + test.test.data = self.options.data + test.test.args = self.options + + def addError(self, test, err, capt=None): + """ + Since Skip, Blocked, and Deprecated are all technically errors, but not + error states, we want to make sure that they don't show up in + the nose output as errors. + """ + if (err[0] == errors.BlockedTest or + err[0] == errors.SkipTest or + err[0] == errors.DeprecatedTest): + print err[1].__str__().split('''-------------------- >> ''' + '''begin captured logging''' + ''' << --------------------''', 1)[0] + + def handleError(self, test, err, capt=None): + """ + If the database plugin is not present, we have to handle capturing + "errors" that shouldn't be reported as such in base. + """ + if not hasattr(test.test, "testcase_guid"): + if err[0] == errors.BlockedTest: + raise SkipTest(err[1]) + return True + + elif err[0] == errors.DeprecatedTest: + raise SkipTest(err[1]) + return True + + elif err[0] == errors.SkipTest: + raise SkipTest(err[1]) + return True diff --git a/seleniumbase/plugins/basic_test_info.py b/seleniumbase/plugins/basic_test_info.py new file mode 100755 index 00000000..569a4f25 --- /dev/null +++ b/seleniumbase/plugins/basic_test_info.py @@ -0,0 +1,64 @@ +""" +The plugin for saving basic test info to the logs for Selenium tests. +The created file will be saved in the default logs folder (in .../logs) +Data to be saved includes: +* Last page url +* Browser +* Server +* Error +* Traceback +""" + +import os +import codecs +import traceback +from nose.plugins import Plugin +from seleniumbase.config import settings + + +class BasicTestInfo(Plugin): + """ + This plugin will capture basic info when a test fails or + raises an error. It will store that basic test info in + the default logs or in the file specified by the user. + """ + name = "basic_test_info" # Usage: --with-basic_test_info + + logfile_name = settings.BASIC_INFO_NAME + + def options(self, parser, env): + super(BasicTestInfo, self).options(parser, env=env) + + def configure(self, options, conf): + super(BasicTestInfo, self).configure(options, conf) + if not self.enabled: + return + self.options = options + + def addError(self, test, err, capt=None): + test_logpath = self.options.log_path + "/" + test.id() + if not os.path.exists(test_logpath): + os.makedirs(test_logpath) + file_name = "%s/%s" % (test_logpath, self.logfile_name) + basic_info_file = codecs.open(file_name, "w+", "utf-8") + self.__log_test_error_data(basic_info_file, test, err, "Error") + basic_info_file.close() + + def addFailure(self, test, err, capt=None, tbinfo=None): + test_logpath = self.options.log_path + "/" + test.id() + if not os.path.exists(test_logpath): + os.makedirs(test_logpath) + file_name = "%s/%s" % (test_logpath, self.logfile_name) + basic_info_file = codecs.open(file_name, "w+", "utf-8") + self.__log_test_error_data(basic_info_file, test, err, "Error") + basic_info_file.close() + + def __log_test_error_data(self, log_file, test, err, type): + data_to_save = [] + data_to_save.append("Last_Page: %s" % test.driver.current_url) + data_to_save.append("Browser: %s " % self.options.browser) + data_to_save.append("Server: %s " % self.options.servername) + data_to_save.append("%s: %s" % (type, err[0])) + data_to_save.append("Traceback: " + ''.join( + traceback.format_exception(*err))) + log_file.writelines("\r\n".join(data_to_save)) diff --git a/seleniumbase/plugins/db_reporting_plugin.py b/seleniumbase/plugins/db_reporting_plugin.py new file mode 100755 index 00000000..b25ecdfb --- /dev/null +++ b/seleniumbase/plugins/db_reporting_plugin.py @@ -0,0 +1,141 @@ +""" +This is the Database test reporting plugin for +recording all test run data in the database. +""" + +import getpass +import time +import uuid +from optparse import SUPPRESS_HELP +from nose.plugins import Plugin +from nose.exc import SkipTest +from seleniumbase.core.application_manager import ApplicationManager +from seleniumbase.core.testcase_manager import ExecutionQueryPayload +from seleniumbase.core.testcase_manager import TestcaseDataPayload +from seleniumbase.core.testcase_manager import TestcaseManager +from seleniumbase.fixtures import constants +from seleniumbase.fixtures import errors + + +class DBReporting(Plugin): + """ + The plugin for reporting test results in the database. + """ + name = 'db_reporting' # Usage: --with-db_reporting + + def __init__(self): + """initialize some variables""" + Plugin.__init__(self) + self.execution_guid = str(uuid.uuid4()) + self.testcase_guid = None + self.execution_start_time = 0 + self.case_start_time = 0 + self.application = None + self.testcase_manager = None + self.error_handled = False + + def options(self, parser, env): + super(DBReporting, self).options(parser, env=env) + parser.add_option('--database_environment', action='store', + dest='database_env', + choices=('prod', 'qa', 'test'), + default='test', + help=SUPPRESS_HELP) + + def configure(self, options, conf): + """get the options""" + super(DBReporting, self).configure(options, conf) + self.options = options + self.testcase_manager = TestcaseManager(self.options.database_env) + + def begin(self): + """At the start of the run, we want to record the + execution information to the database.""" + exec_payload = ExecutionQueryPayload() + exec_payload.execution_start_time = int(time.time() * 1000) + self.execution_start_time = exec_payload.execution_start_time + exec_payload.guid = self.execution_guid + exec_payload.username = getpass.getuser() + self.testcase_manager.insert_execution_data(exec_payload) + + def startTest(self, test): + """at the start of the test, set the test case details""" + data_payload = TestcaseDataPayload() + self.testcase_guid = str(uuid.uuid4()) + data_payload.guid = self.testcase_guid + data_payload.execution_guid = self.execution_guid + if hasattr(test, "browser"): + data_payload.browser = test.browser + else: + data_payload.browser = "N/A" + data_payload.testcaseAddress = test.id() + application = ApplicationManager.generate_application_string(test) + data_payload.env = application.split('.')[0] + data_payload.start_time = application.split('.')[1] + data_payload.state = constants.State.NOTRUN + self.testcase_manager.insert_testcase_data(data_payload) + self.case_start_time = int(time.time() * 1000) + # Make the testcase guid available to other plugins + test.testcase_guid = self.testcase_guid + + def finalize(self, result): + """At the end of the run, we want to + update that row with the execution time.""" + runtime = int(time.time() * 1000) - self.execution_start_time + self.testcase_manager.update_execution_data(self.execution_guid, + runtime) + + def addSuccess(self, test, capt): + """ + After sucess of a test, we want to record the testcase run information. + """ + self.__insert_test_result(constants.State.PASS, test) + + def addError(self, test, err, capt=None): + """ + After error of a test, we want to record the testcase run information. + """ + self.__insert_test_result(constants.State.ERROR, test, err) + + def handleError(self, test, err, capt=None): + """ + After error of a test, we want to record the testcase run information. + "Error" also encompasses any states other than Pass or Fail, so we + check for those first. + """ + if err[0] == errors.BlockedTest: + self.__insert_test_result(constants.State.BLOCKED, test, err) + self.error_handled = True + raise SkipTest(err[1]) + return True + + elif err[0] == errors.DeprecatedTest: + self.__insert_test_result(constants.State.DEPRECATED, test, err) + self.error_handled = True + raise SkipTest(err[1]) + return True + + elif err[0] == errors.SkipTest: + self.__insert_test_result(constants.State.SKIP, test, err) + self.error_handled = True + raise SkipTest(err[1]) + return True + + def addFailure(self, test, err, capt=None, tbinfo=None): + """ + After failure of a test, we want to record testcase run information. + """ + self.__insert_test_result(constants.State.FAILURE, test, err) + + def __insert_test_result(self, state, test, err=None): + data_payload = TestcaseDataPayload() + data_payload.runtime = int(time.time() * 1000) - self.case_start_time + data_payload.guid = self.testcase_guid + data_payload.execution_guid = self.execution_guid + data_payload.state = state + if err is not None: + data_payload.message = err[1].__str__().split( + '''-------------------- >> ''' + '''begin captured logging''' + ''' << --------------------''', 1)[0] + self.testcase_manager.update_testcase_data(data_payload) diff --git a/seleniumbase/plugins/docker_selenium_plugin.py b/seleniumbase/plugins/docker_selenium_plugin.py new file mode 100755 index 00000000..a567538a --- /dev/null +++ b/seleniumbase/plugins/docker_selenium_plugin.py @@ -0,0 +1,81 @@ +""" +This is the Docker version of the Selenium plugin. +""" + +import os +from nose.plugins import Plugin +from pyvirtualdisplay import Display +from selenium import webdriver +from seleniumbase.fixtures import constants + + +class SeleniumBrowser(Plugin): + """ + The plugin for Selenium tests. Takes in key arguments and then + creates a WebDriver object. All arguments are passed to the tests. + + The following variables are made to the tests: + self.options.browser -- the browser to use (--browser) + self.options.server -- the server used by the test (--server) + self.options.port -- the port used by thest (--port) + """ + name = 'selenium_docker' # Usage: --with-selenium_docker + + def options(self, parser, env): + super(SeleniumBrowser, self).options(parser, env=env) + + parser.add_option('--browser', action='store', + dest='browser', + choices=constants.Browser.VERSION.keys(), + default=constants.Browser.FIREFOX, + help="""Specifies the browser. Default: FireFox. + If you want to use Chrome, indicate that.""") + parser.add_option('--browser_version', action='store', + dest='browser_version', + default="latest", + help="""The browser version to use. Explicitly select + a version number or use "latest".""") + parser.add_option('--server', action='store', dest='servername', + default='localhost', + help="""Designates the server used by the test. + Default: localhost.""") + parser.add_option('--port', action='store', dest='port', + default='4444', + help="""Designates the port used by the test. + Default: 4444.""") + + def configure(self, options, conf): + super(SeleniumBrowser, self).configure(options, conf) + self.display = Display(visible=0, size=(1200, 800)) + self.display.start() + self.driver = self.__select_browser() + self.options = options + + def beforeTest(self, test): + """ Running Selenium locally will be handled differently + from how Selenium is run remotely, such as from Jenkins. """ + + try: + self.driver = self.__select_browser() + test.test.driver = self.driver + test.test.browser = "firefox" + except Exception as err: + print "Error starting/connecting to Selenium:" + print err + os.kill(os.getpid(), 9) + return self.driver + + def afterTest(self, test): + try: + self.driver.quit() + self.display.stop() + except: + print "No driver to quit." + + def __select_browser(self): + try: + profile = webdriver.FirefoxProfile() + profile.set_preference("reader.parse-on-load.enabled", False) + return webdriver.Firefox(profile) + except: + return webdriver.Firefox() diff --git a/seleniumbase/plugins/hipchat_reporting_plugin.py b/seleniumbase/plugins/hipchat_reporting_plugin.py new file mode 100755 index 00000000..62d2fb1b --- /dev/null +++ b/seleniumbase/plugins/hipchat_reporting_plugin.py @@ -0,0 +1,128 @@ +""" This plugin allows you to receive test notifications through HipChat. +HipChat @ mentions will only occur during normal +business hours. (You can change this) +By default, only failure notifications will be sent. +""" + +import os +import requests +import logging +import datetime +from nose.plugins import Plugin +from seleniumbase.config import settings + + +HIPCHAT_URL = 'https://api.hipchat.com/v1/rooms/message' +HIPCHAT_AUTH_TOKEN = settings.HIPCHAT_AUTH_TOKEN + + +class HipchatReporting(Plugin): + ''' + Usage: --with-hipchat_reporting --hipchat_room_id=[HIPCHAT ROOM ID] + --hipchat_owner_to_mention=[HIPCHAT @NAME] + ''' + name = 'hipchat_reporting' + + def __init__(self): + super(HipchatReporting, self).__init__() + self.hipchat_room_id = None + self.hipchat_owner_to_mention = None + self.hipchat_notify_on_success = False + self.build_url = os.environ.get('BUILD_URL') + self.successes = [] + self.failures = [] + self.errors = [] + + def options(self, parser, env): + super(HipchatReporting, self).options(parser, env=env) + parser.add_option( + '--hipchat_room_id', action='store', + dest='hipchat_room_id', + help='The hipchat room ID notifications will be sent to.', + default=None) + parser.add_option( + '--hipchat_owner_to_mention', action='store', + dest='hipchat_owner_to_mention', + help='The hipchat username to @mention in notifications.', + default=None) + parser.add_option( + '--hipchat_notify_on_success', action='store_true', + default=False, + dest='hipchat_notify_on_success', + help='''Flag for including success notifications. + If not specified, only notifies on errors/failures + by default.''') + + def configure(self, options, conf): + super(HipchatReporting, self).configure(options, conf) + if not self.enabled: + return + if not options.hipchat_room_id: + raise Exception('''A hipchat room ID to notify must be specified + when using the hipchat reporting plugin.''') + else: + self.hipchat_room_id = options.hipchat_room_id + self.hipchat_owner_to_mention = (options.hipchat_owner_to_mention + or None) + self.hipchat_notify_on_success = options.hipchat_notify_on_success + + def addSuccess(self, test, capt): + self.successes.append(test.id()) + + def addError(self, test, err, capt=None): + self.errors.append("ERROR: " + test.id()) + + def addFailure(self, test, err, capt=None, tbinfo=None): + self.failures.append("FAILED: " + test.id()) + + def finalize(self, result): + message = '' + success = True + if not result.wasSuccessful(): + success = False + if (self.hipchat_owner_to_mention and + self._is_during_business_hours()): + message += "@" + self.hipchat_owner_to_mention + '\n' + + if self.failures: + message += "\n".join(self.failures) + if self.errors: + message += '\n' + if self.errors: + message += "\n".join(self.errors) + + if self.build_url: + message += '\n' + self.build_url + + elif self.hipchat_notify_on_success and self.successes: + message = "SUCCESS! The following tests ran successfully:\n+ " + message += "\n+ ".join(self.successes) + + if message: + self._send_hipchat_notification(message, success=success) + + def _is_during_business_hours(self): + now = datetime.datetime.now() + # Mon - Fri, 9am-6pm + return now.weekday() <= 4 and now.hour >= 9 and now.hour <= 18 + + def _send_hipchat_notification(self, message, success=True, + sender='Selenium'): + response = requests.post(HIPCHAT_URL, params={ + 'auth_token': HIPCHAT_AUTH_TOKEN, + 'room_id': self.hipchat_room_id, + 'from': sender, + 'message': message, + 'message_format': 'text', + 'color': 'green' if success else 'red', + 'notify': '0', + 'format': 'json' + }) + + if response.status_code == 200: + logging.debug("Notification sent to room %s", self.hipchat_room_id) + return True + else: + logging.error("Failed to send notification to room %s", + self.hipchat_room_id) + return False diff --git a/seleniumbase/plugins/page_source.py b/seleniumbase/plugins/page_source.py new file mode 100755 index 00000000..292eb59a --- /dev/null +++ b/seleniumbase/plugins/page_source.py @@ -0,0 +1,60 @@ +""" +The plugin for capturing and storing the page source on errors and failures. +""" + +import os +import codecs +from nose.plugins import Plugin +from seleniumbase.config import settings +from seleniumbase.core import log_helper + + +class PageSource(Plugin): + """ + This plugin will capture the page source when a test fails + or raises an error. It will store the page source in the + logs file specified, along with default test information. + """ + name = "page_source" # Usage: --with-page_source + logfile_name = settings.PAGE_SOURCE_NAME + + def options(self, parser, env): + super(PageSource, self).options(parser, env=env) + + def configure(self, options, conf): + super(PageSource, self).configure(options, conf) + if not self.enabled: + return + self.options = options + + def addError(self, test, err, capt=None): + try: + page_source = test.driver.page_source + except Exception: + # Since we can't get the page source from here, skip saving it + return + test_logpath = self.options.log_path + "/" + test.id() + if not os.path.exists(test_logpath): + os.makedirs(test_logpath) + html_file_name = "%s/%s" % (test_logpath, self.logfile_name) + html_file = codecs.open(html_file_name, "w+", "utf-8") + rendered_source = log_helper.get_html_source_with_base_href( + test.driver, page_source) + html_file.write(rendered_source) + html_file.close() + + def addFailure(self, test, err, capt=None, tbinfo=None): + try: + page_source = test.driver.page_source + except Exception: + # Since we can't get the page source from here, skip saving it + return + test_logpath = self.options.log_path + "/" + test.id() + if not os.path.exists(test_logpath): + os.makedirs(test_logpath) + html_file_name = "%s/%s" % (test_logpath, self.logfile_name) + html_file = codecs.open(html_file_name, "w+", "utf-8") + rendered_source = log_helper.get_html_source_with_base_href( + test.driver, page_source) + html_file.write(rendered_source) + html_file.close() diff --git a/seleniumbase/plugins/s3_logging_plugin.py b/seleniumbase/plugins/s3_logging_plugin.py new file mode 100755 index 00000000..8b6022db --- /dev/null +++ b/seleniumbase/plugins/s3_logging_plugin.py @@ -0,0 +1,51 @@ +""" +The S3 Logging Plugin to upload all logs to the S3 bucket specifed. +""" + +import uuid +import logging +import os +from seleniumbase.core.s3_manager import S3LoggingBucket +from nose.plugins import Plugin + + +class S3Logging(Plugin): + """ + The plugin for uploading test logs to the S3 bucket specified. + """ + name = 's3_logging' # Usage: --with-s3_logging + + def configure(self, options, conf): + """ Get the options. """ + super(S3Logging, self).configure(options, conf) + self.options = options + + def afterTest(self, test): + """ After each testcase, upload logs to the S3 bucket. """ + s3_bucket = S3LoggingBucket() + guid = str(uuid.uuid4().hex) + path = "%s/%s" % (self.options.log_path, + test.test.id()) + uploaded_files = [] + for logfile in os.listdir(path): + logfile_name = "%s/%s/%s" % (guid, + test.test.id(), + logfile.split(path)[-1]) + s3_bucket.upload_file(logfile_name, + "%s/%s" % (path, logfile)) + uploaded_files.append(logfile_name) + s3_bucket.save_uploaded_file_names(uploaded_files) + index_file = s3_bucket.upload_index_file(test.id(), guid) + print "Log files uploaded: %s" % index_file + logging.error("Log files uploaded: %s" % index_file) + + # If the database plugin is running, attach a link + # to the logs index database row + if hasattr(test.test, "testcase_guid"): + from seleniumbase.core.testcase_manager \ + import TestcaseDataPayload, TestcaseManager + self.testcase_manager = TestcaseManager(self.options.database_env) + data_payload = TestcaseDataPayload() + data_payload.guid = test.test.testcase_guid + data_payload.logURL = index_file + self.testcase_manager.update_testcase_log_url(data_payload) diff --git a/seleniumbase/plugins/screen_shots.py b/seleniumbase/plugins/screen_shots.py new file mode 100755 index 00000000..7074305e --- /dev/null +++ b/seleniumbase/plugins/screen_shots.py @@ -0,0 +1,57 @@ +""" +Contains the screenshot plugin for the selenium tests. +""" + +import os +from nose.plugins import Plugin +from seleniumbase.config import settings + + +class ScreenShots(Plugin): + """ + This plugin will take a screenshot when either a test fails + or raises an error. It will store that screenshot either in + the default logs file or in another file of the user's specification. + """ + + name = "screen_shots" + logfile_name = settings.SCREENSHOT_NAME + # Browser windows aren't always maximized. This may display more details. + logfile_name_2 = "full_screenshot.jpg" + + def options(self, parser, env): + super(ScreenShots, self).options(parser, env=env) + + def configure(self, options, conf): + super(ScreenShots, self).configure(options, conf) + if not self.enabled: + return + self.options = options + + def add_screenshot(self, test, err, capt=None, tbinfo=None): + test_logpath = self.options.log_path + "/" + test.id() + if not os.path.exists(test_logpath): + os.makedirs(test_logpath) + screenshot_file = "%s/%s" % (test_logpath, self.logfile_name) + test.driver.get_screenshot_as_file(screenshot_file) + '''try: + # Let humans see any errors on screen before closing the window + test.driver.maximize_window() + import time + time.sleep(0.2) # Make sure the screen is ready + except Exception: + pass + # Second screenshot at fullscreen might not be necessary + # import base64 + screen_b64 = test.driver.get_screenshot_as_base64() + screen = base64.decodestring(screen_b64) + screenshot_file_2 = "%s/%s" % (test_logpath, self.logfile_name_2) + f1 = open(screenshot_file_2, 'w+') + f1.write(screen) + f1.close()''' + + def addError(self, test, err, capt=None): + self.add_screenshot(test, err, capt=capt) + + def addFailure(self, test, err, capt=None, tbinfo=None): + self.add_screenshot(test, err, capt=capt, tbinfo=tbinfo) diff --git a/seleniumbase/plugins/selenium_plugin.py b/seleniumbase/plugins/selenium_plugin.py new file mode 100755 index 00000000..9917e30c --- /dev/null +++ b/seleniumbase/plugins/selenium_plugin.py @@ -0,0 +1,141 @@ +""" +This plugin gives the power of Selenium to nosetests +by providing a WebDriver object for the tests to use. +""" + +import time +import os +from nose.plugins import Plugin +from selenium import webdriver +from seleniumbase.core import selenium_launcher +from seleniumbase.core import browser_launcher +from seleniumbase.fixtures import constants + + +class SeleniumBrowser(Plugin): + """ + The plugin for Selenium tests. Takes in key arguments and then + creates a WebDriver object. All arguments are passed to the tests. + + The following variables are made to the tests: + self.options.browser -- the browser to use (--browser) + self.options.server -- the server used by the test (--server) + self.options.port -- the port used by thest (--port) + """ + name = 'selenium' # Usage: --with-selenium + + def options(self, parser, env): + super(SeleniumBrowser, self).options(parser, env=env) + + parser.add_option('--browser', action='store', + dest='browser', + choices=constants.Browser.VERSION.keys(), + default=constants.Browser.FIREFOX, + help="""Specifies the browser. Default: FireFox. + If you want to use Chrome, indicate that.""") + parser.add_option('--browser_version', action='store', + dest='browser_version', + default="latest", + help="""The browser version to use. Explicitly select + a version number or use "latest".""") + parser.add_option('--server', action='store', dest='servername', + default='localhost', + help="""Designates the server used by the test. + Default: localhost.""") + parser.add_option('--port', action='store', dest='port', + default='4444', + help="""Designates the port used by the test. + Default: 4444.""") + + def configure(self, options, conf): + super(SeleniumBrowser, self).configure(options, conf) + if not self.enabled: + return + + # Determine the browser version to use, and configure settings + self.browser_settings = { + "browserName": options.browser, + 'name': self.conf.testNames[0], + 'build': os.getenv('BUILD_TAG'), + 'project': os.getenv('JOB_NAME') + } + + if options.browser == constants.Browser.INTERNET_EXPLORER: + self.browser_settings["platform"] = "WINDOWS" + self.browser_settings["browserName"] = "internet explorer" + + if options.browser_version == 'latest': + version = constants.Browser.LATEST[options.browser] + if version is not None: + self.browser_settings["version"] = version + else: + version_options = constants.Browser.VERSION[options.browser] + if (version_options is not None and + options.browser_version in version_options): + self.browser_settings["version"] = options.browser_version + + self.options = options + + if (self.options.servername == "localhost" and + self.options.browser == constants.Browser.HTML_UNIT): + selenium_launcher.execute_selenium(self.options.servername, + self.options.port, + self.options.log_path) + + def beforeTest(self, test): + """ Running Selenium locally will be handled differently + from how Selenium is run remotely, such as from Jenkins. """ + + if self.options.servername == "localhost": + try: + self.driver = self.__select_browser(self.options.browser) + test.test.driver = self.driver + if "version" in self.browser_settings.keys(): + version = self.browser_settings["version"] + else: + version = "" + test.test.browser = "%s%s" % (self.options.browser, version) + except Exception as err: + print "Error starting/connecting to Selenium:" + print err + os.kill(os.getpid(), 9) + else: + connected = False + for i in range(1, 4): + try: + self.driver = self.__select_browser(self.options.browser) + test.test.driver = self.driver + if "version" in self.browser_settings.keys(): + version = self.browser_settings["version"] + else: + version = "" + test.test.browser = "%s%s" % ( + self.options.browser, version) + connected = True + break + except Exception as err: + print "Attempt #%s to connect to Selenium failed" % i + if i < 3: + print "Retrying in 15 seconds..." + time.sleep(15) + if not connected: + print "Error starting/connecting to Selenium:" + print err + print "\n\n\n" + os.kill(os.getpid(), 9) + + def afterTest(self, test): + try: + self.driver.quit() + except: + print "No driver to quit." + + def __select_browser(self, browser_name): + if (self.options.servername != "localhost" or + self.options.browser == constants.Browser.HTML_UNIT): + return webdriver.Remote("http://%s:%s/wd/hub" % ( + self.options.servername, + self.options.port), + self.browser_settings) + else: + return browser_launcher.get_driver(browser_name) diff --git a/server_requirements.txt b/server_requirements.txt new file mode 100755 index 00000000..e0d27b5c --- /dev/null +++ b/server_requirements.txt @@ -0,0 +1,15 @@ +selenium==2.48.0 +nose==1.3.7 +pytest==2.8.3 +flake8==2.5.0 +requests==2.7.0 +urllib3==1.10.4 +BeautifulSoup==3.2.1 +unittest2==1.1.0 +chardet==2.3.0 +simplejson==3.7.3 +boto==2.38.0 +MySQL-python==1.2.5 +pdb==0.1 +ipdb==0.8.1 +-e . diff --git a/setup.cfg b/setup.cfg new file mode 100755 index 00000000..061dbdd1 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,6 @@ +[nosetests] + +; This is the config file for default values used during nosetest runs + +nocapture=1 ; Displays print statements from output. Undo this by using: --nologcapture +logging-level=INFO ; INFO keeps the logs much cleaner than using DEBUG diff --git a/setup.py b/setup.py new file mode 100755 index 00000000..657ae860 --- /dev/null +++ b/setup.py @@ -0,0 +1,36 @@ +""" +The setup package to install the SeleniumBase Test Framework plugins +""" + +from setuptools import setup, find_packages # noqa + +setup( + name='seleniumbase', + version='1.1.7', + author='Michael Mintz', + author_email='@mintzworld', + maintainer='Michael Mintz', + description='''The SeleniumBase Test Framework. + (Powered by Python, WebDriver, and more...)''', + license='The MIT License', + packages=['seleniumbase', + 'seleniumbase.core', + 'seleniumbase.plugins', + 'seleniumbase.fixtures', + 'seleniumbase.common', + 'seleniumbase.config'], + entry_points={ + 'nose.plugins': [ + 'base_plugin = seleniumbase.plugins.base_plugin:Base', + 'selenium = seleniumbase.plugins.selenium_plugin:SeleniumBrowser', + 'page_source = seleniumbase.plugins.page_source:PageSource', + 'screen_shots = seleniumbase.plugins.screen_shots:ScreenShots', + 'test_info = seleniumbase.plugins.basic_test_info:BasicTestInfo', + 'db_reporting = ' + 'seleniumbase.plugins.db_reporting_plugin:DBReporting', + 's3_logging = seleniumbase.plugins.s3_logging_plugin:S3Logging', + 'hipchat_reporting = seleniumbase.plugins' + '.hipchat_reporting_plugin:HipchatReporting', + ] + } + )