This introduction should give you some hints about Python and Selenium WebDriver. I will use this in following tutorials as a base.
Preconditions
Python installed
pip (package manager) installed
Editor or IDE installed
Preparation
As first step simply install or upgrade the Selenium package.
# install or upgrade selenium
$ pip install -U selenium
# get information about package
$ pip show selenium
This is a fairly simple process. After the successful command execution you will have the Selenium WebDriver client library on your machine with all that is needed to create automated scripts.
The first script
Now start using the unittest library. The script comments help to describe the code.
#!/usr/bin/env python
import unittest
from selenium import webdriver
class SearchContentOnWebsite(unittest.TestCase):
"""define a class that inherits the TestCase class"""
def setUp(self):
"""perform some tasks at the start of each test"""
# create a new Firefox session
self.driver = webdriver.Firefox()
# wait for a certain amount of time
self.driver.implicitly_wait(30)
# maximize browser window
self.driver.maximize_window()
# navigate to the start URL
self.driver.get("http://softwaretester.info")
def test_search_headline(self):
"""a very simple test case"""
link_text = 'Modern Status Plugin'
title = 'Jenkins - Modern Status Plugin | - Softwaretester -'
# find a element with partial text
elem = self.driver.find_element_by_partial_link_text(link_text)
# click element
elem.click()
# assert that title have value
assert title in self.driver.title
def tearDown(self):
"""method to clean up any initialized values after the test"""
# close the browser window
self.driver.close()
if __name__ == "__main__":
unittest.main(verbosity=2)
Within the folder “report” all logs and reports should be stored. Inside the folder “testsuite” are all Robot Framework files. The “build.xml” looks like this:
*** Settings ***
Documentation This is just a tutorial
...
Metadata VERSION 0.1
Library Selenium2Library
Suite Setup Start Browser
Suite Teardown Close Browser
*** Variables ***
${SERVER} https://www.google.ch
${BROWSER} firefox
*** Keywords ***
Start Browser
[Documentation] Start firefox browser on Selenium Grid
Open Browser ${SERVER} ${BROWSER} None http://127.0.0.1:4444/wd/hub
*** Test Cases ***
Check something
[Documentation] Check the page title
Title Should Be Google
Now start the Selenium Grid (Hub and Node)
# start the hub (in terminal 1)
$ java -jar selenium-server-standalone-2.44.0.jar -role hub -port 4444
# start the node (in terminal 2)
$ java -jar selenium-server-standalone-2.44.0.jar -role node -hub http://localhost:4444/grid/register
Note: The actual version for selenium-server-standalone may be different!
In the last step we start the test. Open a 3rd terminal, browse to the folder and start the Robot Framework.
# run pybot
$ pybot tutorial.robot
If everything works well, the output should look like this:
================================================================
Tutorial :: This is just a tutorial
================================================================
Check something :: Check the page title | PASS |
----------------------------------------------------------------
Tutorial:: This is just a tutorial | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
================================================================
Output: /output.xml
Log: /log.html
Report: /report.html
This tutorial should show, how easy it is to use the Robot Framework with PyCharm. After this small setup you can use PyCharm for your test automation with Robot Framework.
# check installed modules
$ pip list
# install Robot Framework (if not listed)
$ sudo pip install robotframework
# upgrade to the latest version
$ sudo pip install --upgrade robotframework
# verify installation
$ pybot --version
Configuration of PyCharm
Now start PyCharm and open “Preferences” – “Plugins”. Press “Browse repositories…” button and search for “Intellibot”. Press “Install plugin” button and restart PyCharm.
Now create a new directory “testsuite” with new file named “example.robot” inside and insert the following content.
*** Test Cases ***
Example action log this is a test
Open “Preferences” – “Tools” – “External Tools” and press “+” button. Insert a value for “Name”, enable checkbox “Open console”, insert “pybot” into Program, “test suite/” into Parameters and select you specific Working directory.
After press “OK” buttons, go to menu “Tools” – “External Tools” and select your given name. The output should show something like this:
Now we create inside the “testsuite” directory a file called “1st_test.robot” with following content.
*** Settings ***
Documentation This is a simple test with Robot Framework
Library Selenium2Library
*** Variables ***
${SERVER} http://google.com
${BROWSER} Firefox
${DELAY} 0
*** Keywords ***
Open Browser To Login Page
Open Browser ${SERVER} ${BROWSER}
Maximize Browser Window
Set Selenium Speed ${DELAY}
*** Test Cases ***
Valid Login Open Browser To Login Page
[Teardown]. Close Browser
After running the output should look like this:
/usr/local/bin/pybot testsuite/
=============================================================
Testsuite
=============================================================
Testsuite.1St Test :: This is a simple test with Robot Framework
=============================================================
Valid Login | PASS |
-------------------------------------------------------------
Testsuite.1St Test :: This is a simple test with Robot Framework | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
=============================================================
Testsuite.Example
=============================================================
Example action | PASS |
-------------------------------------------------------------
Testsuite.Example | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
=============================================================
Testsuite | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
=============================================================
Output: /output.xml
Log: /log.html
Report: /report.html
Process finished with exit code 0