Last active
February 18, 2016 05:55
-
-
Save devashish2203/7ff28286eab31d9b1924 to your computer and use it in GitHub Desktop.
Simple Filter Tests on babaJobs.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This demonstrates using selenium to automate testing of applying filters on the babajobs.com website. | |
# The functions apart from the actual test functions can easily be moved to a seperate file to avoid clutter here. | |
# The reusable nature of the helper functions makes it very easy to author cases to test various combinations of filters very easily. | |
# The below tests can be easily run using the nosetests framework. | |
# Nostests - https://nose.readthedocs.org/en/latest/ | |
# Install nosetests via pip install nose | |
# The tests can be run then via the command nosetests -s from the folder containing this file | |
#Author: Devashish Chandra | |
from selenium import webdriver | |
from selenium.common.exceptions import TimeoutException, NoSuchElementException, NoSuchAttributeException | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.common.by import By | |
import time | |
class TestJobSearchFilters: | |
def setup(self): | |
self.driver = webdriver.Chrome() | |
#Set implicit wait for 10 secs | |
self.driver.implicitly_wait(10) | |
#Set explicit wait for 10 secs | |
self.wait = WebDriverWait(self.driver, 10) | |
#Navigate to Search page | |
self.driver.get("https://www.babajobs.com") | |
self.driver.find_element_by_link_text("SEARCH JOBS").click() | |
#Collapse all filters | |
filterPanel = self.driver.find_element_by_class_name("job-filter-panel") | |
filterCarets = filterPanel.find_elements_by_class_name("filter--caret") | |
self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "filter--caret"))) | |
for caret in filterCarets: | |
self.collapseFilterNode(caret) | |
def test_SalaryRangeFilter(self): | |
initResultCount = self.getSearchResultCount() | |
self.applyFilter("salaryFilter", "Rs 8000 - Rs 11999") | |
self.verifyAppliedFilters({"SALARY" : "Rs 8000 - Rs 11999"}) | |
filteredResultCount = self.getSearchResultCount() | |
assert filteredResultCount < initResultCount, "Result count not reduced after filter" | |
def test_GenderFilter(self): | |
initResultCount = self.getSearchResultCount() | |
self.applyFilter("genderFilter", "Male") | |
self.verifyAppliedFilters({"GENDER" : "Male"}) | |
filteredResultCount = self.getSearchResultCount() | |
assert filteredResultCount < initResultCount, "Result count not reduced after filter" | |
def test_SalaryAndGenderFilter(self): | |
initResultCount = self.getSearchResultCount() | |
self.applyFilter("salaryFilter", "Rs 8000 - Rs 11999") | |
self.verifyAppliedFilters({"SALARY" : "Rs 8000 - Rs 11999"}) | |
filteredResultCount = self.getSearchResultCount() | |
assert filteredResultCount < initResultCount, "Result count not reduced after filter" | |
initResultCount = self.getSearchResultCount() | |
self.applyFilter("genderFilter", "Male") | |
self.verifyAppliedFilters({"GENDER" : "Male", "SALARY" : "Rs 8000 - Rs 11999"}) | |
filteredResultCount = self.getSearchResultCount() | |
assert filteredResultCount < initResultCount, "Result count not reduced after filter" | |
def tearDown(self): | |
#Clear Filters | |
try: | |
clearButton = self.driver.find_element_by_class_name("m-filter-controls") | |
clearButton.click() | |
except NoSuchElementException: | |
print("Filter not applied. Unable to clear") | |
self.driver.quit() | |
## All the below functions can be moved to a seperate helper file | |
def getSearchResultCount(self): | |
searchBox = self.driver.find_element_by_id("searchForm") | |
parentContainer = searchBox.find_element(By.XPATH, "..") | |
resultCountElement = parentContainer.find_element_by_tag_name("h4") | |
resultCount = resultCountElement.text.split("ACTIVE")[0].strip() | |
print(resultCount) | |
return int(resultCount) | |
def applyFilter(self, filterId, filterLinkText): | |
filterPanel = self.driver.find_element_by_class_name("job-filter-panel") | |
caret = self.driver.find_element_by_id(filterId+"-caret") | |
self.expandFilterNode(caret) | |
header = self.driver.find_element_by_id(filterId+"-header") | |
print("Applying filter for: "+header.text) | |
filterBody = self.driver.find_element_by_id(filterId) | |
try: | |
filterBody.find_element_by_link_text(filterLinkText).click() | |
#Check that page is loaded after applying filter | |
old_page = self.driver.find_element_by_tag_name('html') | |
self.wait.until(EC.staleness_of(old_page)) | |
except NoSuchElementException as e: | |
print("Filter: "+filterLinkText+" not found for: "+filterId) | |
print(e.message) | |
def verifyAppliedFilters(self, filterLabelValueDict): | |
filterContainer = self.driver.find_element_by_class_name("l-filter-container") | |
filterLabels = [x.text.strip() for x in filterContainer.find_elements_by_class_name("l-filter-label")] | |
filterValues = [x.text.replace("|", "").strip() for x in filterContainer.find_elements_by_class_name("l-filter-value")] | |
print(filterLabels) | |
print(filterValues) | |
for label, value in filterLabelValueDict.iteritems(): | |
filterLabel = label+":" | |
assert filterLabel in filterLabels, "Filter Label not found in applied filters: "+str(filterLabels) | |
index = filterLabels.index(filterLabel) | |
assert filterValues[index] == value, "Filter value: "+filterValues[index]+" does not match expected filter value " +"("+value+")"+"for filter: "+filterLabel | |
def collapseFilterNode(self, element): | |
className = element.get_attribute("class") | |
#print("Collapse"+className) | |
if "is-transform" in className: | |
#Collapse node | |
try: | |
element.click() | |
except Exception as e: | |
print(e.message) | |
def expandFilterNode(self, element): | |
className = element.get_attribute("class") | |
#print("Expand"+className) | |
if "is-transform" not in className: | |
#Expand node | |
try: | |
element.click() | |
except Exception as e: | |
print(e.message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment