logo logo

Using Selenium with Python Tutorial (Part 2)

In the first part of using Selenium with Python tutorial, we’ve learned how to setup Python, Python’s commands through the Shell and how to implement Python’s plugin Eclipse IDE.

In this post, we’ll learn how to open a new project in Eclipse, install Selenium, how to start writing Selenium code in IDE and I’ll demonstrate a test case.

Installing  Selenium on Python

The first step to start using Selenium with Python in your testing,  is opening a new Python project in Eclipse. After you’ve opened Eclipse: File>New, if it’s your first PyDev Project, click “other”.

From there on, select ‘PyDev project’:

Selenium with Python: Opening a project in Eclipse

From there on, select ‘PyDev project’:

Selenium with Python: Opening a project in Eclipse

Click ‘Next’ and select the following values:

Selenium with Python: Opening a project in Eclipse

Click ‘Finish’ and confirm the following message:

Selenium with Python: Open Associated Perspective?

Now our project is suppose to look as the following in Package Explorer of Eclipse:

Selenium with Python: PyDev Package Explorer

 

In order to install Selenium’s library in Python project, we’ll open a command line and enter the following line:

C:\Python27\Scripts\pip.exe install selenium

After performing the necessary installation, we will notice the message “Selenium successfully installed”:

Selenium with Python: Selenium successfully installed

Now we need to assign the Selenium libraries we’ve downloaded, to the new project we’ve opened: Right click on the project in Package Explorer>Properties. In the new window that will open, continue to PyDev- PYTHONPATH, click on the “External libraries” tab and select the destination: C:\Python27\Lib\site-packages

Selenium with Python: PyDev- PYTHONPATH

 

Automatic function in Selenium and Python

Now we’re ready for our first piece of code: return once again to Eclipse and open a new module in Python, click File>New>PyDev Module:

Selenium with Python: PyDev Module

*The Modules in Python also configure Templates- what we’ll especially focus on.

We’ll enter a Package and Module name (the Package contains the Module):

Selenium with Python: Create New PyDev Module

Click ‘Finish’. Now, at this stage we’ll select the ‘Module- Main’:

Selenium with Python: Creating a new module

A new py document was created. Now we’ll begin writing our Selenium code in Python – a very simple test automation case: Start with entering Google search>Enter a value>Click ‘Search’.

from selenium import webdriver  
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.google.com")

elem = driver.find_element_by_name("q")
elem.send_keys("testproject.io")
elem.send_keys(Keys.RETURN)

driver.close()

 

Automatic test in Selenium

The next step will be writing a program which illustrates a test, while applying well-known test automation tools (divisions, annotations, assets etc.).

And so, we’ll create a new module as done previously. Except for, now we’ll select ‘Template – Unittest’.

Selenium With Python: Creating a new module

The new document is supposed to look as the following:

Selenium with Python: Module Document

The template already consists of Unit Testing divisions we are already familiar with from Selenium’s other environments. We’ll use the same case, only now including tests:

Enter Google Search>Enter a value>Click ‘Search’ and make sure a specifically chosen word doesn’t show up in the search results:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class Test(unittest.TestCase):


    def setUp(self):
        self.driver = webdriver.Firefox()


    def tearDown(self):
        self.driver.quit()


    def testName(self):
        driver = self.driver
        driver.get("http://www.google.com")
        assert "Google" in driver.title
        elem = driver.find_element_by_name("q")
        elem.send_keys("testproject.io")
        elem.send_keys(Keys.RETURN)
        assert "Kuku" not in driver.page_source

The test passed successfully (the value ‘KuKu’ didn’t show up).

Selenium with Python: test passed

Now let’s try and make the test fail, by changing the Assert’s value in the end of the test:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class Test(unittest.TestCase):


    def setUp(self):
        self.driver = webdriver.Firefox()


    def tearDown(self):
        self.driver.quit()


    def testName(self):
        driver = self.driver
        driver.get("http://www.google.com")
        assert "Google" in driver.title
        elem = driver.find_element_by_name("q")
        elem.send_keys("testproject.io")
        elem.send_keys(Keys.RETURN)
        assert "Yoni" not in driver.page_source

This will bring us to the following result:

Selenium with Python : Test failed


Happy testing using Selenium with Python!


Reference


 

About the author

Yoni Flenner

A software engineer who likes to test things programmatically, and he’s doing it for a living for the past 10 years: https://atidcollege.co.il

Leave a Reply

FacebookLinkedInTwitterEmail