Getting Started – Selenium with Python Bindings

Introduction

Selenium Python binding provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. Selenium Python bindings also provide a convenient API to access Selenium WebDrivers such as Firefox, IE, Chrome, etc. The current supported Python versions are 2.7, 3.5, and above. In this blog, I will explain the Selenium 3 WebDriver API and in the next one, I will explain how to install and configure PyDev in Eclipse.

What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. It’s high-level built in data structures, combined with dynamic typing and binding, makes it very attractive for Rapid Application Development, as well as for use as a scripting or glue language to connect existing components together. Python’s simple, easy-to-learn syntax emphasizes readability, and, therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse. The Python interpreter and the extensive standard library are available in source or binary form, free of charge, for all major platforms, and can be freely distributed.

Related: Define, Implement, Migrate and Script Automated Tests for End-to-end Automation of Applications Spanning Multiple Technologies.

What is Selenium?

Selenium is an open-source automation tool to test your web application. You can do this in various ways. For instance:

  • Selenium supports multiple languages such as Java, C#, Python, Ruby etc..
  • Selenium has components like, IDE, WebDriver.

Downloading Python bindings for Selenium

You can download Python bindings for Selenium from the PyPI page for the Selenium package. However, a better approach would be to use pip to install the selenium package. Python 3.6 has pip available in the standard library. Using pip, you can install selenium like this: `pip install selenium’. You may consider using virtualenv to create isolated Python environments. Python 3.6 has pyvenv which is almost the same as virtualenv.

Detailed instructions for Windows users

Note: You should have an Internet connection to perform this installation.

  1. Install Python 3.6 using the MSI available in python.org download page.
  2. Start a command prompt using the cmd.exe program and run the pip command as given below to install selenium.

C:Python35Scriptspip.exe install selenium.

Advantages of Python in Selenium

  1. Compared to other languages, Python takes less time to run the script and complete the execution.
  2. Python uses indentation, not braces ({}), making it easy to understand the code flow.
  3. Python is simpler and more compact.

Simple Usage

If you have installed Selenium Python bindings, you can start using it from Python in the following way:

from selenium import webdriver
 from selenium.webdriver.common.keys import Keys
 driver = webdriver.Firefox()
 driver.get("http://www.python.org")
 assert "Python" in driver.title
 elem = driver.find_element_by_name("q")
 elem.clear()
 elem.send_keys("pycon")
 elem.send_keys(Keys.RETURN)
 assert "No results found." not in driver.page_source
 driver.close()

If you found this interesting, don’t miss my next blog, ‘Install and Configure PyDev in Eclipse’.

Read Other Selenium related blogs:

Web Application Testing with Selenium WebDriver

Introduction to Selenium with C Sharp

Introduction to Selenium with C Sharp

Selenium WebDriver is a free and open-source library for automated testing web applications. Selenium tests can be written in multiple programming languages such as Java, C#, Python and Ruby and multiple browsers we execute scripts.

Prerequisite:

  • Download and install Visual Studio Frame Work.
  • Your target browser is installed, such as Chrome or Firefox.

Set up Visual Studio Solution:

Create a new project in Visual Studio Select template ‘Templates’ → ‘Visual C#’ → ‘Test’ → ‘Unit Test Project’.

Add Selenium WebDriver package to the project.

Run the following command in ‘Package Manager Console’ (selecting menu ‘Tools’ → ‘NuGet Package Manager’ → ‘Package Manager Console’).

After Selenium WebDriver is installed,  Solution References will appear like the following:

How to Create a test and run it:

  1. Add a new C# (a test) Class. Right mouse click the project name (in Solution Explorer) select ‘Add’ → ‘Unit Test’.
  2. Define the following class code to test and run it.
using System;
 using Microsoft.VisualStudio.TestTools.UnitTesting;
 using OpenQA.Selenium;
 using OpenQA.Selenium.Chrome;
 using OpenQA.Selenium.Firefox;
 using OpenQA.Selenium.Safari;

namespace TestAppDemo
{
[TestClass]
public class UnitTest2
{
static IWebDriver driverGC;
// static IWebDriver driverFF;
[AssemblyInitialize]
public static void setup(TestContext contest)
{
driverGC = new ChromeDriver(@”D:Documents StudySelenium detailschromedriver_win32″);
}
[TestMethod]
public void TestChromeDriver()
{
driverGC.Navigate().GoToUrl(“http://automationpractice.com/index.php”);
driverGC.FindElement(By.Id(“search_query_top”)).SendKeys(“CASUAL DRESSES”);
driverGC.FindElement(By.Id(“search_query_top”)).SendKeys(Keys.Enter);

}
[TestMethod]
public void TestClose()
{
driverGC.Quite();
}

}
}

Right Click on `class’ and select ‘Run Tests’.

After running successful, test case, it will show as below with pass mark:

We have to understand the below elements and their usages:

  • By Id: Finds the Webpage element by its unique control id.
  • By Name: Finds the Webpage element by the control name.
  • By Class: Finds the Webpage element by the Class name.
  • By LinkText: Finds the link element by its exact text displayed on web page
  • By partialLinkText: Finds webpage element that contains the given text
  • By TagName: Finds the Webpage element by the Tag name.
  • By XPath: This is most popular and majorly used for locating webpage element in a easiest way.
  • By CSS selector: Finds Webpage element by the CSS selectorEx.

What are the Advantages of Selenium Test using C#:

  • It is Open source.
  • It can be extended with different technologies.
  • In multiple browsers we execute scripts.
  • It supports multiple OS platforms and compatible with mobile devices.
  • Test executes within the browsers, so focus is not needed while executing the script.
  • Using Selenium Grids, you can execute tests in parallel.
  • Web Driver is faster than Selenium RC because it has simpler architecture.
  • Web Driver directly talks to the web browser whereas Selenium RC needs the help of the RC Server in order to do the same.

Disadvantages of Selenium Test:

  • It supports only web based applications.
  • It does not provide any facility for Object Repository.
  • It does not provide any facility for Recovery Scenario.
  • There is no report generation for test methods by default.
  • Users need to depend on minimum one programming language.

For more details, you can go through this click here.

Read Other Blogs related to Selenium:

Web Application Testing with Selenium WebDriver

Getting Started – Selenium with Python Bindings

Exit mobile version