How to install Selenium on Linux and automate your 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 simpleCRUD 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 selenium

The 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.

Install Selenium on Linux

driver.get("http://url-for-posts.com")
assert "Post Page Title" in driver.title

The 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_source

In 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.

Install Selenium on Linux

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.

<input type="text" name="title" required/>
<input type="text" name="content" required/>
<input type="text" name="category" required/>
<select name="published">
   <option value="No">No</option>
   <option value="Yes" selected="">Yes</option>                            
    </select>
<button class="button " type="submit">Save</button>

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()”.

For the “select” element we will need to import the “Select” class.

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_source

Similarly 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_source

And 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_source

The 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)


Is a Technical project manager who through this blog shares his experience building Web Applications with Python, FlaskAngularjs and Linux.His Projects are opensource and available at https://github.com/Leo-g



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值