Later edit:In case you don’t want to go through the trouble of coding your own automated tests, you can just use Endtest.
Selenium supports Python and thus can be utilised with Selenium for testing.
You can run Python scripts for Firefox, Chrome, IE, etc.ondifferent Operating Systems.
In this tutorial, you will learn-
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.
KeywordMeaningUsageelifElse ifElse ifelseElseif: X; elif: Y; else: Jexceptdo this ,If an exception happens,except ValueError, a: print aexecRun string as Pythonexec ‘print “hello world !”’
Selenium is a tool to test your web application. You can do this in various ways, for instance
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.
PyDev is Python development environment for Eclipse.Step 1) Install PyDev plugin in Eclipse from 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,
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’.
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.
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.
Step 6) Make a New Project in Python. In this step,
You can see the new Python(PyDev) project is created.
Step 7) In this step,
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.
Step 10) Write your Python code.
from selenium import webdriver
fromselenium.webdriver.common.keys import Keys
user = ""
pwd = ""
driver = webdriver.Firefox()
driver.get("http://www.facebook.com")
assert "Facebook" in driver.title
elem = driver.find_element_by_id("email")
elem.send_keys(user)
elem = driver.find_element_by_id("pass")
elem.send_keys(pwd)
elem.send_keys(Keys.RETURN)
driver.close()
Snapshot of the Code
Explanation of the code
OUTPUT
The values of the username “guru99” and password entered.
The Facebook page will login with email and password. Page opened (see image below)
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.
from selenium import webdriver from selenium.common.exceptions import TimeoutException
browser = webdriver.Firefox() browser.get( www.facebook.com )
username = browser.find_element_by_id( "guru99" ) password = browser.find_element_by_id( "password@123" ) submit = browser.find_element_by_id( "submit" )
username.send_keys( "me" ) password.send_keys( "mykewlpass" )
submit.click()
wait = WebDriverWait( browser, 5 )
try: page_loaded = wait.until_not( lambda browser: browser.current_url == login_page ) except TimeoutException: self.fail( "Loading timeout expired" )
self.assertEqual( browser.current_url, correct_page, msg = "Successful Login" )
Snapshot of the code
Explanation of the code:
Summary: