Automatic tests with Selenium – part #1

Automatic tests with Selenium – part #1

Testing is often the last thing that Developer thinks about. But with Selenium WebDriver it’s easy and even spectacular while running.

In short

With Selenium you automate the browser. So you can tell it to click wherever and do whatever you can do with your browser. But Selenium will do it for you. Moreover it’ll do it faster and every time in the the same exact way – repeatedly. We have a bunch of APIs to use c#, ruby, python, perl and even javascript. We also have a PHP API created by facebook group. Thanks guys.

Let’s test

I choose python, since it’s native in Ubuntu. First we need to install selenium driver:

pip install selenium

And we can check if it works. Let’s create a python file test1.py

sudo touch test1.py
sudo chmod +x test1.py
sudo vi test1.py

copy and paste this contetns

#!/usr/bin/env python

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('http://google.com');
inputElement = driver.find_element_by_name("q")
inputElement.send_keys("selenium")

then run your first test

./test1.py

As a result we should see the browser window open and Google page with selenium phrase searched.

What we just did

We created a driver for Chrome browser.

driver = webdriver.Chrome()

Next we tell the browser to open url

driver.get('http://google.com');

And then we get the search input by it’s name and enter “selenium” phrase.

inputElement = driver.find_element_by_name("q")
inputElement.send_keys("selenium")

You can read some more about WebDriver.

Ok, let’s go to Automatic tests with Selenium – part #2.

Leave a comment

Your email address will not be published. Required fields are marked *