Let's assume we want to find the source of Python module Selenium.
In Python Shell:
>>> import selenium
>>>
>>> selenium.__file__
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/__init__.pyc'
>>>
Sunday, 9 August 2015
Thursday, 11 June 2015
Python & Webdriver - Resize & Maximize Browser window
# Import modules
from selenium import webdriver
#
# get initial window size
driver = webdriver.Firefox()
print driver.get_window_size()
#
# set window size (windth, height) in pixels
driver.set_window_size(480, 320)
#
# Print new size
print(driver.get_window_size())
#
# maximize window
driver.maximize_window()
#
# Print new size
print(driver.get_window_size())
#
driver.quit()
Sunday, 31 May 2015
Python Interview Questions
Software testing - Questions and Answers - Python Interview Questions
- Question: How Python can be used in software testing?
Answer:
1. To generate test data; parse test results; generate reports; testing API calls etc.
2. Python to extract requirements from a Word document.
3. For testing tasks automation, setting up environments for tests, extracting performance data, etc...
4. Testers use Python extensively in many companies with Selenium for test automation.
5. For writing desktop applications used by testers.
6. Test data manipulation.
7. To build test environment
8. Testing with IronPython on .NET - Question:What Python frameworks do you know?
Answer:Framework called Web2py, PAMIE (Python automation Module for I. E.), The py.test framework - Question:What tools that helps Python development do you know?
Answer:There are good tools for helping Python development such as Notepad++ with the PyNPP plugin and Eclipse with PyDev and PyUnit. - The following is displayed by a print function call:
yesterday
today
tomorrow
Please write an example of a print function.
Answer: print('yesterday\ntoday\ntomorrow') - The following is displayed by a print function call:
hello-how-are-you
Please write an example of a print function.
Answer: print('hello' + '-' + 'how' + '-' + 'are' + '-' + 'you') - Question: What does the expression len('') evaluate to?
Answer: 0 - Considering the following code:
s = 'catandapple'
Write an expression that evaluate to 'apple'.
Answer: s[-5:] - Question: Write an expression that evaluate to True
Answer: len('aaddgg') == 6 - Question: Write the code that calculate the sum of even numbers from x1 till x2
- Question: What are "tuples"
Answer: Tuples are immutable sequences: they cannot be modified. Tuples use parentheses instead of square brackets: tup = ('test', 5, -0.2) - Question: What are the rules for legal Python names?
Answer:
1. Names must start with a letter or _.
2. Names must contain only letters, digits, and _. - Question: What is the dictionary tipe in Python?
How to Access Information From Dictionaries and Modify it? - To display a value in the console, what Python keyword do you use? (Print)
- Python file names traditionally end in what characters after a period? (.py)
- An if statement can have how many elif parts? (Unlimited, i.e., 0 or more)
- How many control objects are allowed in a frame? (Unlimited, i.e., 0 or more)
- When you enter text into an input field and press enter, the text is passed to the input field's event handler.
What is the data type of the text? (string) - What does the draw handler parameter represent? (The canvas)
- What happens if you draw text outside the canvas coordinates?
- You want a timer to create exactly 1000 events. Suggest any solutions.
(Have a global counter for the number of timer calls. In the timer handler, increment the counter. In the timer handler, check the count and possibly stop the timer.) - How many timers can you have running at once? (Unlimited)
- Give example of list - [1, 2, 3]
- Give example of Tuple - (1, 2, 3)
- Which types of data are immutable in Python? (Numbers, Tuples, Strings, Booleans)
- Which types of data are mutable in Python? (List)
Python Webdriver - Common Keys
In this article, I want to introduce the common keys in Webdriver. The keys are used to simulate a user entering a key on the keyboard. Back space, enter, ALT, ESCAPE; these are the types of keys I am talking about. The place where this would be useful is, for example, the enter key could be used to submit a form in a web application. In the previous Hello World example, we entered the text 'Hello World!' into a search bar and then execute the search with a separate submit command. Another way we could have achieved the same result is to include the enter key at the end of the 'Hello World!' string.
As an example, below is a script to enter text into an input element to search the Yale University course catalog. The search string we will use is 'engl1*' which should return a list of all the one hundred level english courses.
#!/usr/bin/env python
from sys import argv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def yale_courses(s):
# start Firefox
driver = webdriver.Firefox()
# navigate to the Yale course catalog
driver.get('http://students.yale.edu/oci/search.jsp')
# find the Course number input page element
input = driver.find_element_by_name('CourseNumber')
# Enter the search string into the input element
input.send_keys(s)
# Take a screen shot
driver.save_screenshot('english_search')
# Execute search
input.send_keys(Keys.RETURN)
# Finished
driver.close()
if __name__=='__main__':
course = argv[1]
yale_courses(course)
As an example, below is a script to enter text into an input element to search the Yale University course catalog. The search string we will use is 'engl1*' which should return a list of all the one hundred level english courses.
#!/usr/bin/env python
from sys import argv
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
def yale_courses(s):
# start Firefox
driver = webdriver.Firefox()
# navigate to the Yale course catalog
driver.get('http://students.yale.edu/oci/search.jsp')
# find the Course number input page element
input = driver.find_element_by_name('CourseNumber')
# Enter the search string into the input element
input.send_keys(s)
# Take a screen shot
driver.save_screenshot('english_search')
# Execute search
input.send_keys(Keys.RETURN)
# Finished
driver.close()
if __name__=='__main__':
course = argv[1]
yale_courses(course)
Sunday, 25 January 2015
Python - Extract lines from text file starting with From
Source file:
Let's assume text file containing bunch of lines while some of them start with "From". As an example such a line can look like following:
From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
What we are trying to do is to parse all the lines from the source file starting with "From" and have them written line by line as a list and at the end count number of lines matching our criteria.
Python code is then as per following:
fname = raw_input("Enter file location: ")
# source file, e.g. C:\Python34\Doc\SourceFile.txt
fh = open(fname)
count = 0
lst = list()
for line in fh:
line = line.rstrip("\n")
if line.startswith("From "):
count = count + 1
lst.append(line.split())
#
# Define initial item in a list
i = 1
for i in lst:
# print second item in each list
print i[1]
#
print "There were", count, "lines in the file with From as the first word"
Example of results:
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
...
There were 15 lines in the file with From as the first word
Let's assume text file containing bunch of lines while some of them start with "From". As an example such a line can look like following:
From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
What we are trying to do is to parse all the lines from the source file starting with "From" and have them written line by line as a list and at the end count number of lines matching our criteria.
Python code is then as per following:
fname = raw_input("Enter file location: ")
# source file, e.g. C:\Python34\Doc\SourceFile.txt
fh = open(fname)
count = 0
lst = list()
for line in fh:
line = line.rstrip("\n")
if line.startswith("From "):
count = count + 1
lst.append(line.split())
#
# Define initial item in a list
i = 1
for i in lst:
# print second item in each list
print i[1]
#
print "There were", count, "lines in the file with From as the first word"
Example of results:
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
...
There were 15 lines in the file with From as the first word
Saturday, 3 January 2015
Python - Parse XML file
# ==================================================================
# >> Short Description:
# Script searches for xml file in given root directory matching
# given pattern and prints selected file to xml
#
# >> Author: Tomas Nemeth
# >> Creation Date: 12/2014
# ==================================================================
#
#-- Import Python modules -----------
import os
import fnmatch
import sys
import xml.etree.ElementTree as ET
from xml.dom import minidom
#------------------------------------
#
#
rootPath = str(input("Insert root path: ")) # e.g. C:\Python34\Doc\ScriptTest
pattern = str(input("Insert file pattern: ")) # including wildcards, e.g. "data*"
print('=================================================')
#
# Search for the files matching given directory & pattern
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
print(os.path.join(root, filename)) # prints the results
#
# User is asked here to select one file from list for parsing
print('=========================================')
file = str(input("Select one file from list for parsing: "))
print('=========================================')
#
# get the root
tree = ET.parse(file)
root = tree.getroot()
#
# Function prints selected file as whole xml
xmldoc = minidom.parse(file)
print(xmldoc.toxml())
#
print('==========================================')
print('')
# >> Short Description:
# Script searches for xml file in given root directory matching
# given pattern and prints selected file to xml
#
# >> Author: Tomas Nemeth
# >> Creation Date: 12/2014
# ==================================================================
#
#-- Import Python modules -----------
import os
import fnmatch
import sys
import xml.etree.ElementTree as ET
from xml.dom import minidom
#------------------------------------
#
#
rootPath = str(input("Insert root path: ")) # e.g. C:\Python34\Doc\ScriptTest
pattern = str(input("Insert file pattern: ")) # including wildcards, e.g. "data*"
print('=================================================')
#
# Search for the files matching given directory & pattern
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
print(os.path.join(root, filename)) # prints the results
#
# User is asked here to select one file from list for parsing
print('=========================================')
file = str(input("Select one file from list for parsing: "))
print('=========================================')
#
# get the root
tree = ET.parse(file)
root = tree.getroot()
#
# Function prints selected file as whole xml
xmldoc = minidom.parse(file)
print(xmldoc.toxml())
#
print('==========================================')
print('')
Tuesday, 23 December 2014
Python Web Testing
Web testing using module webbrowser
Usage Examples:
webbrowser.open_new(url)
Open url in a new window of the default browser, if possible, otherwise,
open url in the only browser window.
Example: webbrowser.open_new('http://google.com')
webbrowser.open_new_tab(url)
Open url in a new page (“tab”) of the default browser, if possible,
otherwise equivalent to open_new().
Search for something in Google
Opens new window in default browser.
import webbrowser
#
google = input('Google search: ')
webbrowser.open_new_tab('http://www.google.com/search?btnG=1&q=%s' % google)
Usage Examples:
webbrowser.open_new(url)
Open url in a new window of the default browser, if possible, otherwise,
open url in the only browser window.
Example: webbrowser.open_new('http://google.com')
webbrowser.open_new_tab(url)
Open url in a new page (“tab”) of the default browser, if possible,
otherwise equivalent to open_new().
Search for something in Google
Opens new window in default browser.
import webbrowser
#
google = input('Google search: ')
webbrowser.open_new_tab('http://www.google.com/search?btnG=1&q=%s' % google)
Another Web Testing tutorials
Follow the link below:
Subscribe to:
Posts (Atom)