Sample Selenium Webdriver Scripts

  1. Simple Selenium Webdriver Script
  2. Selenium Webdriver Script
  3. Selenium Webdriver Projects
  4. Sample Selenium Webdriver Script In Java

Selenium supports Python and thus can be utilized with Selenium for testing.

  • Python is easy compared to other programming languages, having far less verbose.
  • The Python APIs empower you to connect with the browser through Selenium.
  • Selenium sends the standard Python commands to different browsers, despite variation in their browser's design.

Selenium WebDriver Client Library for Python enables us to utilize all the features available with Selenium WebDriver and interact with Selenium Standalone Server to perform Automated testing (both remote and distributed testing) of browser-based applications.

  1. Apr 02, 2013  In previous article we have seen about What is Selenium Webdriver? In this article we are concentrating on implementation of the WebDriver, we will see how to selenium webdriver download and configure the Selenium Webdriver with Eclipse & run your first selenium web driver script. Selenium WebDriver.
  2. Selenium WebDriver with TestNG Sample Script Selenium WebDriver with TestNG in Java - in this example, we will test the Google Calculator feature using Selenium for UI automation and TestNG as testing framework.
  3. Browse other questions tagged selenium webdriver selenium-webdriver selenium-chromedriver or ask your own question. Blog Job Hunting: How to Find Your Next Step by Taking Your Search Offline.
  4. Each and every browser has its own Driver to execute Selenium WebDriver Scripts. Selenium WebDriver supports browsers such as Mozilla Firefox, Google Chrome, Internet Explorer, Opera, Safari etc., Here we are going to see how to run Selenium WebDriver Script in Internet Explorer Browser. What is IEDriver and What it does?

You can run Python scripts for Firefox, Chrome, IE, etc.ondifferent Operating Systems.

In this tutorial, you will learn-

What is Python?

Python is a high-level object-oriented scripting language. It is designed in a user-friendly manner. Python uses simple English keywords, which is easy to interpret. It has less syntax complications than any other programming languages.

See some of the examples in the table below.
KeywordMeaningUsage
elif Else if Else if
else Else if: X; elif: Y; else: J
except do this ,If an exception happens, except ValueError, a: print a
exec Run string as Python exec 'print 'hello world !'

What is Selenium?

Selenium is a tool to test your web application. You can do this in various ways, for instance

  • Permit it to tap on buttons
  • Enter content in structures
  • Skim your site to check whether everything is 'OK' and so on.

Why to choose Python over Java in Selenium

Few points that favor Python over Java to use with Selenium is,

1. Java programs tend to run slower compared to Python programs.

2. Java uses traditional braces to start and ends blocks, while Python uses indentation.

3. Java employs static typing, while Python is dynamically typed.

4. Python is simpler and more compact compared to Java.

How to Install and Configure PyDev in Eclipse

PyDev is Python development environment for Eclipse.

Step 1) Got to Eclipse Marketplace. Help > Eclipse Marketplace

Now once the plugin 'eclipse market place' is opened. The next step is to install 'pydev IDE' for eclipse.

Step 2) In this step,

  1. Search for 'pydev' in search box and then
  2. Click install(In my system it is already installed).

Step 3) Select the checkbox button. It says 'PyDev.' The first check box is mandatory while the second one is optional. After marking the checkbox, press 'Next'.

Simple selenium webdriver script

Step 4) Now, in this step you will set preferences. With the help of preference option, you can use Python as per the project need.

Go to Windows > Preferences > Interpreter-Python. Click on 'OK' button.

A new window will open when you click on 'OK' button. In this window, follow the following steps.

  • Under interpreter dropdown, you select the option Interpreter-Python. It helps in running Python scripts.
  • Also, set workbench time interval. When a build is performed, the workbench will automatically save all resources that is changed since the last build.
  • Click on 'OK' button.

When you click on'OK' button, it sets the default Python Interpreter. It is just like you need to set java compiler for running a Java code. To change the interpreter name, double click on Python Tab.

Step 5)In this step, give the 'interpreter name' and the 'exe file name' of Python.

  1. Click on 'Browse' and find python.exe 'C:Python27python.exe.
  2. Click 'OK' button.

Step 6) Make a New Project in Python. In this step,

We will use this variable to score 1 point when the laser beam hits an invader. Scratch space invaders game. Adding a score variableLet’s make a new variable called score.

  1. Right click Package Explorer > New >
  2. Select option others.

You can see the new Python(PyDev) project is created.

Step 7) In this step,

  1. Select 'PyDev Project' and
  2. Press 'Next' button.

After creating 'PyDev Project', you will create a new Python package.

Step 8) Create a new Python package. After entering the name, click on 'Finish' button.

If you see in below screenshot, a new package is created.

After creating a new package, the next step is to createPyDev Module. The module contains somePython files for initialization. These files or functions from the module can be imported into other module. So, there will be no need to re-write the program again.

Step 9) Createa new PyDev module. Right click on package > New >Other>PyDev module.

Smack that mp3 download. Step 10) Write your Python code.

How to Create Test Scripts in Selenium with Python

Simple Selenium Webdriver Script

In this example, we did automation for 'Facebook login page' using the Firefox driver.

EXAMPLE 1

Snapshot of the Code

Explanation of the code

  • Code line 1: From selenium module import webdriver
  • Code line 2: From selenium module import Keys
  • Code line 3: User is a blank variable which will be we used to store values of username.
  • Code line 4: pwd is also a blank (here it is empty, but the user can provide values in it) variable. This will be used to store values of the password.
  • Code line 5: In this line, we are initializing 'FireFox' by making an object of it.
  • Code line 6: The 'driver.get method' will explore to a page given by the URL.WebDriver will hold up until the page has completely been loaded (that is, the 'onload' occasion has let go), before returning control to your test or script.
  • Code line 7: 'Asserts' keyword is used to verify the conditions. In this line, we are confirming whether the title is correct or not. For that, we will compare the title with the string which is given.
  • Code line 8: In this line, we are finding the element of the textbox where the 'email' has to be written.
  • Code line 9: Now we are sending the values to the email section
  • Code line 10: Same for the password
  • Code line 11: Sending values to the password section
  • Code line 12: Elem.send_keys(Keys.RETURN) is used to press enter after the values are inserted
  • Code line 13: Close

OUTPUT

The values of the username 'guru99' and password entered.

The Facebook page will login with email and password. Page opened (see image below)

EXAMPLE 2

In this example,

  • We will open a login page.
  • Fill the required field'username' and 'password'.
  • Then validate if the login was successful or not.

Snapshot of the code

Explanation of the code:

Selenium Webdriver Script

  • Code line 1-2: Import selenium package
  • Code line 4: Initialize Firefox by creating an object
  • Code line 5: Get login page (Facebook)
  • Code line 7-9: Fetch username, password input boxes and submit button.
  • Code line 11-12: Input text in username and password input boxes
  • Code line 15: Click on the 'Submit' button
  • Code line 18: Create wait object with a timeout of 5 sec.
  • Code line 20 -30: Test that login was successful by checking if the URL in the browser changed. Assert that the URL is now the correct post-login page

Selenium Webdriver Projects

Summary:

Sample Selenium Webdriver Script In Java

  • Selenium is an open-source web-based automation tool.
  • Python language is used with Selenium for testing. It has far less verbose and easy to use than any other programming language
  • The Python APIs empower you to connect with the browser through Selenium
  • Selenium can send the standard Python commands to different browsers, despite variation in their browser's design.