Sunday, January 28, 2018

Functional Testing with Selenium and Python

The visual studio IDE can be used to do the python+selenium script writing. The best thing is, If you use unittest module, Test case is displayed on the test explorer. Which allows to run a test case using test explorer.

  • First you need to create python project

Go to Files—>New—>Project

Select Python from Left panel and you can give suitable name for project and select Location

pythonins

Let’s see how to create a test class using a unit test framework and organizing test cases. In this test case first moves to google search page, then verify “I’m Feeling Lucky” button available on the page. If button available, click on the button. After that verify the page title of the new page that navigate after click on “I’m Feeling Lucky” button.

python3

Above script can be explained as follows.

  • In the first part of the script, Import unit test module that provide a framework for organizing test cases. (The unit test module is in standard libraries. So no need to install anything new.) And also import selenium web driver module and other referencing classes.

import

  • Next define the test class that inherit from unittest.TestCase

testclass

  • After that, Inside this class create test cases to test functions we need to test.when we create methods there is naming convention that we need to follow.we need to start method name with word “test”

testcase

  • Create the instance of IE web driver

iewebdriver

navigation

  • Then define web driver wait time which tells how much time need to wait until element loaded completely.

waittime

  • Then find the element using element name and wait until element become clickable.In this case wait 1 min until element become clickable.
  • After that verify element available in the page before click on it.After that click on the “I’m Feeling Lucky” button.

feeling2

  • After click on the button, Verify navigated page using page title.

titleassert

  • This line close the browser windows.

quitbrowser

  • Test script can be run by right click inside the test case method and select “Run Tests” or using test explorer.

runtest

No comments:

Post a Comment