Posted by Derek@TheDailyLinux »
Add Comment »
The goal of this guide is to show you how to install and run the example Python script for cssutils from start to finsih. This was created while running Ubuntu, but can be applied and modified to any distribution. Open up a terminal and let’s get to it!
Step 1: Install easy_install For Python
The easy_install binary is a Python package manager that makes it easy to install cssutils. The following steps will get this installed:
sudo apt-get install curl
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
Step 2: Install cssutils Python Package
Now we’ll install the cssutils package for Python using easy_install.
sudo easy_install cssutils
Step 3: Prepare cssutils Example
Next, you can copy/paste the following example into your favorite text editor and save it (I saved it as cssutils_test.py).
import cssutils
css = u'''/* a comment with umlaut � */
@namespace html "http://www.w3.org/1999/xhtml";
@variables { BG: #fff }
html|a { color:red; background: var(BG) }'''
sheet = cssutils.parseString(css)
for rule in sheet:
if rule.type == rule.STYLE_RULE:
# find property
for property in rule.style:
if property.name == 'color':
property.value = 'green'
property.priority = 'IMPORTANT'
break
# or simply:
rule.style['margin'] = '01.0eM' # or: ('1em', 'important')
sheet.encoding = 'ascii'
sheet.namespaces['xhtml'] = 'http://www.w3.org/1999/xhtml'
sheet.namespaces['atom'] = 'http://www.w3.org/2005/Atom'
sheet.add('atom|title {color: #000000 !important}')
sheet.add('@import "sheets/import.css";')
# cssutils.ser.prefs.resolveVariables == True since 0.9.7b2
print sheet.cssText
Step 4: Run cssutils Example
Finally, we can run the example.
python cssutils_test.py
Conclusions
I’m hoping this guide met the goal of teaching you how to install cssutils and run an example Python script to demonstrate some capabilities of cssutils. Feedback is welcomed and appreciated.
Posted by Derek@TheDailyLinux »
Add Comment »
Selenium is a suite of tools to automate web browsers across many platforms in which you can write your tests in your preferred language. This guide intends on showing you how to install and run a Selenium test on a Linux box. Specifically, this guide was written using a default Fedora 15 installation and using Python as the preferred language. It can easily be applied to other distributions or other preferred languages. Now, open a terminal window and let’s get to it!
Step 1: Install the Selenium Server Package and Python Setup Tools
First, we need to install the selenium-server and python-setuptools packages. The first is obvious: we need the server. The second is for easy_install so we can install the Selenium Python package. Open the terminal and paste the following command.
su -c 'yum install -y selenium-server python-setuptools'
Step 2: Install the Selenium Server Package
Now, we need to install the Python selenium package.
su -c 'easy_install selenium'
Step 3: Startup Selenium Server
Next is starting the selenium server. This is done simply by running the following command as a normal user. I suggest opening in a new terminal window or add “&” to the end to push it to the background so you can continue working in the current terminal.
selenium-server
Step 4: Create a Selenium Test
You can then create a test from scratch or by using the Selenium IDE via Firefox plugin that you can download here: http://seleniumhq.org/download/. Read more about it here: http://seleniumhq.org/projects/ide/.
A note about using the Firefox plugin while running Linux (at the time of this writing). You must enable the experimental features before you can export the created test to Python (or any) code. To do this, open the preferences of the plugin and then make the following changes:
Options -> Options.... -> Enable Experimental Features
Options -> Format -> Python 2
Now, you should be able to copy/paste the output into your favorite editor and save it as a python script.
To get you going, here’s a working script that you can copy/paste into your favorite text editor:
from selenium import selenium
import unittest, time, re
class Google(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://www.google.com/")
self.selenium.start()
def test_google(self):
sel = self.selenium
sel.open("/#hl=en&xhr=t&q=Selenium+IDE+Download&cp=14&pf=p&sclient=psy&biw=1920&bih=866&source=hp&aq=0&aqi=&aql=&oq=Selenium+IDE+D&pbx=1&bav=on.2,or.r_gc.r_pw.&fp=fce33a84b0764b22")
sel.type("lst-ib", "Selenium IDE Download")
# Uncomment to close the server when finished with test.
# def tearDown(self):
# self.selenium.stop()
# self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
Step 5: Run a Selenium Test
Now, we’ll run the test. After running the following command, you should see Firefox open, navigate to http://www.google.com, and perform a search.
python google_test.py
Conclusions
If you’re wondering where to go next or looking for other useful tools, check out the following page from the NetBeans webpage: http://netbeans.org/kb/docs/php/phpunit.html.