class Testpractic(unittest.TestCase): log = cl.customlogger(logging.DEBUG) data_reader = DataReader() @pytest.fixture(autouse=True) def classsetup(self, test_setup, request): self.login = loginpage(self.driver) self.practice = practicepage(self.driver) self.HU = Homepage(self.driver) self.base = basepage(self.driver) self.Teststatuser = TestStatus(self.driver) if self.data_reader.get_data(request.function.__name__, "Runmode") != "Y": pytest.skip("Excluded from current execution ") @pytest.mark.order1 def test_login2(self): self.login.click_loginlnk() self.login.enter_username("*****@*****.**") self.login.enter_password("abcabc") self.login.click_loginbutton() print("login was successfull") @pytest.mark.order2 def test_prac(self): testname = conftest.whoami() self.practice.click_practice() result = self.practice.verfiypracticepage() self.Teststatuser.markFinal(testname, result, "Practicage page verified ") self.practice.select_check("bmw") self.practice.select_check("benz") self.driver.implicitly_wait(3) self.practice.select_radio("benz") self.practice.select_dropdown("Honda") @pytest.mark.order3 def test_pract2(self): self.practice.alert_accept("harish") self.practice.alert_dismiss() @pytest.mark.order4 def test_pract3(self): self.practice.iframe_acion_Search("java") @pytest.mark.order5 def test_pract4logout(self): self.driver.switch_to.default_content() # self.HU.scrolledup_userprofile() self.HU.click_userprofile() self.HU.click_logout() result = self.base.backbase() testname = conftest.whoami() self.Teststatuser.mark(result, "log was sucessful") result1 = self.base.backbase() self.Teststatuser.markFinal(testname, result1, "log out sucess")
class TestStatus(SeleniumDriver): log = cl.customlogger(logging.INFO) def __init__(self, driver): """ Inits CheckPoint class """ super(TestStatus, self).__init__(driver) self.resultList = [] def setResult(self, result, resultMessage): try: if result is not None: if result: self.resultList.append("PASS") self.log.info("### VERIFICATION SUCCESSFUL :: " + resultMessage) else: self.resultList.append("FAIL") self.log.error("### VERIFICATION FAILED :: " + resultMessage) self.screenshots(resultMessage) else: self.resultList.append("FAIL") self.log.error("### VERIFICATION FAILED :: " + resultMessage) self.screenshots(resultMessage) except: self.resultList.append("FAIL") self.log.error("### Exception Occurred !!!") self.screenshots(resultMessage) print_stack() def mark(self, result, resultMessage): """ Mark the result of the verification point in a test case """ self.setResult(result, resultMessage) def markFinal(self, testName, result, resultMessage): """ Mark the final result of the verification point in a test case This needs to be called at least once in a test case This should be final test status of the test case """ self.setResult(result, resultMessage) if "FAIL" in self.resultList: self.log.error(testName + " ### TEST FAILED") self.resultList.clear() assert True == False else: self.log.info(testName + " ### TEST SUCCESSFUL") self.resultList.clear() assert True == True
class ConfigUtility: """ #This class includes basic reusable config_helpers. """ log = cl.customlogger(logging.DEBUG) def __init__(self): self.cur_path = os.path.abspath(os.path.dirname(__file__)) self.config_path = os.path.join(self.cur_path, r"../utils/config.ini") def load_properties_file(self): config = None try: # noinspection PyBroadException config = ConfigParser() config.read(self.config_path) except Exception as ex: self.log.error("Failed to load ini/properties file.", ex) print_stack() return config def change_properties_file(self, section, property_name, property_value): # #This method is used to change the property value #:param section: property section in ini file #:param property_name: property name to change #:param property_value: property value to set #:return: it returns boolean value for successful change property operation ### try: config = self.load_properties_file() config[section][property_name] = property_value with open(self.config_path, 'w') as configfile: config.write(configfile) time.sleep(1) return True except Exception as ex: self.log.error("Failed to change ini/properties file.", ex) print_stack() return False
class Homepage(SeleniumDriver): log = cl.customlogger(logging.DEBUG) def __init__(self, driver): self.driver = driver self.userprofileicon_path = "//img[@class='gravatar']" self.logout_link = "Log Out" self.mycourse_linktext = "My Courses" self.allcourse_lnktxt = "All Courses" def click_userprofile(self): self.driver.find_element_by_xpath(self.userprofileicon_path).click() def verifyloginsuccessfull(self): try: mycourse = self.driver.find_element_by_link_text( self.mycourse_linktext) ccourstxt = mycourse.text if ccourstxt in "My Courses": self.log.info(ccourstxt) return True else: return False except: return False def click_allcourse(self): self.driver.find_element_by_link_text(self.allcourse_lnktxt).click() def click_logout(self): time.sleep(5) self.driver.find_element_by_link_text(self.logout_link).click() def scrolledup_userprofile(self): target = self.driver.find_element_by_xpath(self.userprofileicon_path) target.location_once_scrolled_into_view def click_mycousre(self): self.driver.find_element_by_link_text(self.mycourse_linktext).click()
class Testloginpage(unittest.TestCase): log = cl.customlogger(logging.DEBUG) data_reader = DataReader() @pytest.fixture(autouse=True) def classsetup(self, test_setup, request): self.HU = Homepage(self.driver) self.login = loginpage(self.driver) self.base = basepage(self.driver) self.Teststatuser = TestStatus(self.driver) if self.data_reader.get_data(request.function.__name__, "Runmode") != "Y": pytest.skip("Excluded from current execution ") @pytest.mark.order1 def test_login1(self): testname = conftest.whoami() self.login.click_loginlnk() result = self.login.verfiyloginscreen() self.Teststatuser.markFinal(testname, result, "Login element are present") self.login.enter_username("*****@*****.**") self.login.enter_password("abcabc") self.login.click_loginbutton() result2 = self.HU.verifyloginsuccessfull() self.Teststatuser.mark(result2, "login sucess") result1 = self.HU.verifyloginsuccessfull() self.Teststatuser.markFinal(testname, result1, "login was scuess") @pytest.mark.order2 def test_logout(self): testname = conftest.whoami() #logout.click_mycousre() self.HU.click_userprofile() self.HU.click_logout() result3 = self.base.backbase() self.Teststatuser.mark(result3, "log out scuess") result2 = self.base.backbase() self.Teststatuser.markFinal(testname, result2, "log out sucess")
class basepage(SeleniumDriver): log = cl.customlogger(logging.DEBUG) def __init__(self, driver): self.driver = driver self.practice_lintext = "Practice" self.login_lintext = "Login" self.Signup_lintxt = "Signup" self.class_welcom = " //h2[@class='headline']" def backbase(self): try: element = self.driver.find_element_by_xpath(self.class_welcom) elele = element.text if elele in "Welcome to Let's Kode It": self.log.info(elele) return True else: return False except: return False def verifyPageTitle(self, titleToVerify): """ Verify the page Title Parameters: titleToVerify: Title on the page that needs to be verified """ try: actualTitle = self.getTitle() return self.verifyTextContains(actualTitle, titleToVerify) except: self.log.error("Failed to get page title") print_stack() return False
class loginpage(SeleniumDriver): log = cl.customlogger(logging.DEBUG) def __init__(self, driver): self.driver = driver self.allcourse_linktxt = "All Courses" self.username_id = "user_email" self.password_id = "user_password" self.loginbutton_xpath = "//input[@name='commit']" self.login_linktxt = "Login" self.usericon_xpath = "//img[@class='gravatar']" def click_loginlnk(self): self.elementclick(self.login_linktxt, locatorType="link") def enter_username(self, username): self.sendKeys(username, self.username_id) def enter_password(self, password): self.sendKeys(password, self.password_id) def click_loginbutton(self): self.elementclick(self.loginbutton_xpath, "xpath") def verfiylogintitle(self): return self.verifyPageTitle("Let's Kode It") def verfiyloginscreen(self): locator_dict = { self.username_id: "id", self.password_id: "id", self.loginbutton_xpath: "xpath", } result = self.verify_element_located(locator_dict) return result
class practicepage(SeleniumDriver): log = cl.customlogger(logging.DEBUG) def __init__(self, driver): self.driver = driver self.login_linktxt = "Login" self.iframe_xpath = "//iframe[@id='courses-iframe']" self.iframe_search_button_id = "search-course-button" self.iframe_txt_search_xpath = "//input[@id='search-courses']" self.entername_id = "name" self.alertpoper_id = "alertbtn" self.alertdismiss_id = "confirmbtn" self.checkbox_xpath = "//input[(@type='checkbox') and (@value = '%s')]" self.radio_xpath = "//input[(@type= 'radio') and (@value = '%s')]" self.dropdown = "//select[@id = 'carselect']" self.practic_linktxt = "Practice" def click_practice(self): self.elementclick(self.practic_linktxt, "link") def select_check(self, value, to_select=True): #xpath = self.checkbox_xpath % value #check_ele =self.driver.find_element_by_xpath(xpath).click() self.xpath = self.checkbox_xpath % value self.elementclick(self.xpath, "xpath") def select_radio(self, value, to_select=True): #xpath1 = self.radio_xpath % value #check_ele = self.driver.find_element_by_xpath(xpath1).click() self.xpath1 = self.radio_xpath % value self.elementclick(self.xpath1, "xpath") def select_dropdown(self, value): Sel = Select(self.driver.find_element_by_xpath(self.dropdown)) Sel.select_by_visible_text(value) def alert_accept(self, entername): self.sendKeys(entername, self.entername_id) self.elementclick(self.alertpoper_id, "id") time.sleep(2) self.driver.switch_to_alert().accept() def alert_dismiss(self): self.elementclick(self.alertdismiss_id, "id") time.sleep(2) self.driver.switch_to_alert().dismiss() def iframe_acion_Search(self, value): iframe = self.driver.find_element_by_xpath(self.iframe_xpath) self.driver.switch_to.frame(iframe) #self.driver.find_element_by_xpath(self.iframe_txt_search_xpath).send_keys(value) self.sendKeys(value, self.iframe_txt_search_xpath, "xpath") #self.driver.find_element_by_id(self.iframe_search_button_id).click() self.elementclick(self.iframe_search_button_id, "id") def verfiypracticepage(self): locator_dict = { self.entername_id: "id", self.alertpoper_id: "id", self.alertdismiss_id: "id", self.practic_linktxt: "link" } result = self.verify_element_located(locator_dict) return result def logintxt(self): result = self.driver.find_element_by_link_text( self.login_linktxt).is_displayed() return
class SeleniumDriver(unittest.TestCase): log = cl.customlogger(logging.INFO) def __init__(self, driver): self.driver = driver def screenshots(self,testname): try: x = moment.now().strftime("%d-%m-%Y_%H-%M-%S") y = testname + x print("Captured the Screenshot named::", y) self.driver.get_screenshot_as_file( y + ".PNG") except: self.log.info("Exception Occured while taking screenshot") # Below line works fine with allure adopter # allure.MASTER_HELPER.attach(name=y, contents=self.driver.get_screenshot_as_png(), # type=allure.MASTER_HELPER.attach_type.PNG) # Below line works fine with allure-pytest allure.attach(self.driver.get_screenshot_as_png(), name=y, attachment_type=allure.attachment_type.PNG) def wait_for_element_to_be_clickable(self, locator, locatorType="xpath", max_time_out=10): """ This function is used for explicit waits till element clickable :param locator_properties: it takes locator string as parameter :param locator_type: it takes locator type as parameter :param max_time_out: this is the maximum time to wait for particular element :return: it returns the boolean value according to the element located or not """ try: WebDriverWait(self.driver, max_time_out, ignored_exceptions=[StaleElementReferenceException]).until( EC.element_to_be_clickable((self.get_locator_type(locatorType), locator))) return True except: self.log.error("Exception occurred while waiting for element to be clickable.") return False def getTitle(self): return self.driver.title def verifyTextContains(self, actualText, expectedText): """ Verify actual text contains expected text string Parameters: expectedList: Expected Text actualList: Actual Text """ self.log.info("Actual Text From Application Web UI --> :: " + actualText) self.log.info("Expected Text From Application Web UI --> :: " + expectedText) if expectedText.lower() in actualText.lower(): self.log.info("### VERIFICATION CONTAINS !!!") return True else: self.log.info("### VERIFICATION DOES NOT CONTAINS !!!") return False def getByType(self,locatorType): locatorType = locatorType.lower() if locatorType == "id": return By.ID elif locatorType =="xpath": return By.XPATH elif locatorType =="css": return By.CSS_SELECTOR elif locatorType =="class": return By.CLASS_NAME elif locatorType =="name": return By.NAME elif locatorType =="link": return By.LINK_TEXT else: self.log.info(" Locator type " + locatorType + "not correct/supported") def getlelement(self, locator, locatorType="id"): element = None try: locatorType = locatorType.lower() byType = self.getByType(locatorType) element = self.driver.find_element(byType,locator) self.log.info("Element found with locator : " + locator + " and locator type " + locatorType) except: self.log.info("Element not found with locator : " + locator + " and locator type " + locatorType) return element def elementclick(self,locator,locatorType ="id"): try: element = self.getlelement(locator,locatorType) element.click() self.log.info("clicked on element with locator :" + locator + " locatorType :" + locatorType) except: self.log.info(" Cannot clicked on element with locator :" + locator + " locatorType :" + locatorType) print_stack() def sendKeys(self,data,locator,locatorType= "id"): try: element = self.getlelement(locator,locatorType) element.send_keys(data) self.log.info("Send data on element with :" + locator + "locatorType :" + locatorType) except: self.log.info(" Cannot Send data on element with :" + locator + "locatorType :" + locatorType) print_stack() def wait_for_element_to_be_present(self,locator_properties,locatorType = "id", max_time_out = 10): try: WebDriverWait(self.driver, max_time_out, ignored_exceptions=[StaleElementReferenceException]).until( EC.presence_of_element_located((self.getByType(locatorType), locator_properties))) return True except: return False def verify_element_located(self,locator_dict,max_timeout=10): flag = False result = [] try: for locator_prop in locator_dict.keys(): prop_type = locator_dict[locator_prop] if self.wait_for_element_to_be_present(locator_prop, prop_type, max_timeout): self.log.info( "Element found with locator_properties " + locator_prop + " and locator_type :: " + locator_dict[ locator_prop]) flag = True else: self.log.error( "Element not found with locator_properties" + locator_prop + " and locator_type" + locator_dict[ locator_prop]) flag = False result.append(flag) except Exception as Ex: self.log.error("Exception Occured during element identification", Ex) if False in result: return False else: return True