http://techarena51.com/index.php/install-selenium-linux-automate-web-tests
Selenium is a web testing toolkit that allows you to test a web application in any browser of your choice. It does this with the help of a software called WebDriver which allows you to emulate a web browser and test your web application in it.
In this post I am going to show you how to test a simple CRUD application, by automating link clicking and form filling.
Software versions
Selenium (2.44.0)
Python 3.4.2
Linux kernel 3.13.0-37-generic and Mint 17.1 for Desktop.
Installing Selenium on Linux
To install the Selenium Bindings for Python on Linux use pip.
Getting started with the code.
The first thing you need to do is create an instance of the webdriver.
Now let’s test the “Create”, i.e add a post.
On clicking the “Add new” link you get the page to create a post.
Here we need to fill in the title, body, category, select whether the post will be published and then click on the “Save” button to add the post.
So let’s fill in the form with some text
For the “select” element we will need to import the “Select” class.
You can view the code in action in the video below.
Comments and feedback is welcome.
Ref (http://selenium-python.readthedocs.org/en/latest/getting-started.html#using-selenium-to-write-tests)
Selenium is a web testing toolkit that allows you to test a web application in any browser of your choice. It does this with the help of a software called WebDriver which allows you to emulate a web browser and test your web application in it.
In this post I am going to show you how to test a simple CRUD application, by automating link clicking and form filling.
Software versions
Selenium (2.44.0)
Python 3.4.2
Linux kernel 3.13.0-37-generic and Mint 17.1 for Desktop.
Installing Selenium on Linux
To install the Selenium Bindings for Python on Linux use pip.
[leo@selenium-on-linux]$ virtualenv -p /usr/bin/python3 venv-3.4 [leo@selenium-on-linux]$ source venv-3.4/bin/activate [leo@selenium-on-linux]$ pip3 install seleniumThe webdriver for Firefox is installed along with Selenium but if you want to test your applications on chrome then you need to download the chrome driver and ensure chrome is installed on your Linux distro.
Getting started with the code.
The first thing you need to do is create an instance of the webdriver.
[leo@selenium-on-linux]$gedit selenium-tests.py from selenium import webdriver driver=webdriver.Chrome('/home/leo/Downloads/chromedriver')Next you need to specify the url of the web application you need to test. I will be using a custom CMS application which is used to create, read, update and delete posts for this demonstration. Below is the screenshot of the app.
driver.get("http://url-for-posts.com") assert "Post Page Title" in driver.titleThe above code will fetch the web page and check if the web page has been successfully fetched by matching the content in the page title.
Now let’s test the “Create”, i.e add a post.
link=driver.find_element_by_link_text("Add new") NewWindow=link.click() assert "Save" in driver.page_sourceIn the code above I use “find_element_by_link_text()” to perform actions on the anchor HTML element which contains the text “Add new”. You can get elements with their ID or name or even by class name. You can click on elements with the “click()” method.
On clicking the “Add new” link you get the page to create a post.
Here we need to fill in the title, body, category, select whether the post will be published and then click on the “Save” button to add the post.
So let’s fill in the form with some text
#Add posts title=driver.find_element_by_name("title") title.send_keys("Your Title text here") content=driver.find_element_by_name("content") content.send_keys("Your Body text here") category=driver.find_element_by_name("category") category.send_keys("Your category text here")Below is the html of the form elements.
As I mention earlier you can fetch an element by it’s name with “find_element_by_name(“element name”)”. You can then perform an action on it like fill it with text using “send_keys()”.
from selenium.webdriver.support.ui import Select select = Select(driver.find_element_by_name('published')) published=select.select_by_value("Your option value")Now we need to click on Save, however the “Save” button does not have a name attribute hence we need to use the class name.
button=driver.find_element_by_class_name('button') success=button.click() assert "Add was successful" in driver.page_sourceSimilarly the code for update is
#Edit Posts link=driver.find_element_by_link_text("Edit") newindow=link.click() assert "Save" in driver.page_source title=driver.find_element_by_name("title") title.send_keys("Selenium web test edit") content=driver.find_element_by_name("content") content.send_keys("Selenium web test edit") category=driver.find_element_by_name("category") category.send_keys("Selenium web test edit") select = Select(driver.find_element_by_name('published')) published=select.select_by_value("0") button=driver.find_element_by_class_name('button') success=button.click() assert "Update was successful" in driver.page_sourceAnd the code for delete is
#Delete Posts link=driver.find_element_by_link_text("Delete") NewWindow=link.click() assert "Post was deleted successfully" in driver.page_sourceThe complete source code is available at https://github.com/Leo-g/Selenium/blob/master/python-selenium.py
You can view the code in action in the video below.
Comments and feedback is welcome.
Ref (http://selenium-python.readthedocs.org/en/latest/getting-started.html#using-selenium-to-write-tests)
No comments:
Post a Comment