class SignIn(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_SigninPage() #返回到student 签到的页面 self.wd = WebDriverDoBeforeTest() self.wd.className = "studentSignin.Signin" self.wd.beforeClass() self.wd.driver = self.weblogin.driver self.webdriverpai = WebdriverApi(self.weblogin.driver) self.conn = Execute_SQL() def tearDown(self): unittest.TestCase.tearDown(self) self.wd.afterClass() self.weblogin.driver.close() self.conn.execute_conn_close() def test_student_singin(self): '''学生签到测试用例''' try: self.logger.info("SignIn.test_student_singin 测试用例已执行") api = self.webdriverpai sql = 'select * from test_lab;' res = self.conn.execute_select(sql) for i in res: #for循环式,默认从0--n-1 print "------ mysql testcase:", i row_A = i[0] row_B = i[1] row_C = i[2] api.webList_RandomSelectByOption(By.ID, "class_id", 3) api.webList_RandomSelectByOption(By.ID, "lab_id", 3) api.sendKeys(By.ID, "name", row_A) api.sendKeys(By.ID, "stuno", row_B) api.sendKeys(By.ID, "info", row_C) api.click(By.ID, "sign") b = api.is_Displayed(By.CLASS_NAME, "layui-layer-btn0") if b: capturename = api.getText(By.CLASS_NAME, "layui-layer-content") if "欢迎使用实验室数字化平台签到" != capturename: self.wd.capture(capturename) api.click(By.CLASS_NAME, "layui-layer-btn0") self.weblogin.back_SigninPage() #返回到student 签到的页面 print u"平台学生签到测试用例通过" except Exception, e: self.logger.error("SignIn.test_student_singin 测试用例出现异常" + str(e))
class SignIn(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_SigninPage() #返回到student 签到的页面 self.wd = WebDriverDoBeforeTest() self.wd.className ="studentSignin.Signin" self.wd.beforeClass() self.wd.driver = self.weblogin.driver self.webdriverpai = WebdriverApi(self.weblogin.driver) self.em = ExcelManager("UsersLogin.xls") def tearDown(self): unittest.TestCase.tearDown(self) self.wd.afterClass() self.weblogin.driver.close() def test_student_singin(self): '''学生签到测试用例''' try: self.logger.info("SignIn.test_student_singin 测试用例已执行") api = self.webdriverpai tes_xls_data = self.em.readexcel("students") #得到sheet页 nrows = tes_xls_data.nrows print "------ excel中的测试用例条数是:",nrows-1 #excel中 的数据行数从1行开始 for i in range(1,nrows): #for循环式,默认从0--n-1 print "------ 这是第:",i," 条用例" row_A = str(tes_xls_data.cell(i,0).value) row_B = str(tes_xls_data.cell(i,1).value) row_C = tes_xls_data.cell(i,2).value print row_A, row_B, row_C api.webList_RandomSelectByOption(By.ID, "class_id", 3) api.webList_RandomSelectByOption(By.ID, "lab_id", 3) api.sendKeys(By.ID, "name",row_A) api.sendKeys(By.ID, "stuno",row_B) api.sendKeys(By.ID, "info",row_C) api.click(By.ID, "sign") b = api.is_Displayed(By.CLASS_NAME,"layui-layer-btn0") if b: capturename = api.getText(By.CLASS_NAME, "layui-layer-content") if "欢迎使用实验室数字化平台签到" != capturename: self.wd.capture(capturename) api.click(By.CLASS_NAME,"layui-layer-btn0") self.weblogin.back_SigninPage() #返回到student 签到的页面 except Exception,e: self.logger.error("SignIn.test_student_singin 测试用例出现异常"+str(e))
class InitBrowser(): ''' configbrowser本身的self对象去调用其他的类方法 ''' def __init__(self): self.__GBD = GetBrowserDriver() self.__logger = LogObj() self.driver = None fc = FileChecK() boolean = fc.is_has_file("framework.ini") if boolean: self.inipath = fc.get_fileabspath() self.fw_conf = Config(self.inipath) self.waitTimeout = self.fw_conf.get("TimeSet", "waitTimeout") self.scriptTimeout = self.fw_conf.get("TimeSet", "scriptTimeout") self.pageLoadTimeout = self.fw_conf.get("TimeSet", "pageLoadTimeout") def beforeTestInitBrowser(self, browsername, baseURL): if "chrome" == browsername: self.startChromeDriver(baseURL) elif "ie" == browsername: self.startInternetExplorerDriver(baseURL) elif "firefox" == browsername: self.startFirefoxDriver(baseURL) else: self.startFirefoxDriver(baseURL) def startFirefoxDriver(self, baseURL): try: firefoxdriver = self.__GBD.get_FirefoxDrvier() os.environ["webdriver.firefox.bin"] = firefoxdriver self.driver = webdriver.Firefox() self.driver.set_page_load_timeout(self.pageLoadTimeout) self.__logger.debug("set pageLoadTimeout : " + self.pageLoadTimeout) self.driver.implicitly_wait(self.waitTimeout) self.__logger.debug("set waitTimeout : " + self.waitTimeout) self.driver.set_script_timeout(self.scriptTimeout) self.__logger.debug("set scriptTimeout : " + self.scriptTimeout) self.__logger.info("初始化火狐浏览器成功") self.driver.maximize_window() self.get(baseURL, 3) except Exception, e: self.__logger.error("getFirefoxDriver()方法发生异常,异常信息:" + e)
class Login(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.wd = WebDriverDoBeforeTest() self.wd.className ="login_lab.Login" self.wd.beforeClass() self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_LoginPage(5) self.driver = self.weblogin.driver self.em = ExcelManager("UsersLogin.xls") def tearDown(self): unittest.TestCase.tearDown(self) self.wd.afterClass() self.driver.close() def test_login_labPlatf(self): '''身份登录测试用例''' self.logger.info("Login.test_login_labPlatf测试用例已执行") tes_xls_data = self.em.readexcel("users") #得到sheet页 nrows = tes_xls_data.nrows print "------ excel中的测试用例条数是:",nrows-1 #excel中 的数据行数从1行开始 for i in range(1,nrows): #for循环式,默认从0--n-1 print "------ 这是第:",i," 条用例" row_A = tes_xls_data.cell(i,0).value row_B = tes_xls_data.cell(i,1).value row_C = tes_xls_data.cell(i,2).value print row_A, row_B, row_C b = self.weblogin.userType_login(row_A, row_B, row_C) if b: title = self.driver.title print title if "实验室数字化平台" == title: print "------ 登录成功 ------" self.em.writexcel(i, 3, "pass") self.weblogin.back_LoginPage(5) else: print "------ 登录失败 ------" self.em.writexcel(i, 3, "failed") self.weblogin.back_LoginPage(5) print "------ 登录测试用例执行成功 ------"
class GetBrowserDriver(): def __init__(self): self.conf = ConfigParser.ConfigParser() self.__fileabspath = None self.__projectpath = None self.__logger = LogObj() fc = FileChecK() boolean = fc.is_has_file("driverpath.conf") if boolean: self.__fileabspath = fc.get_fileabspath() self.__projectpath = fc.getProjectPath() self.conf.read(self.__fileabspath) def get_IEDriver(self): try: # 获取指定的section, 指定的option的值 iedriverpath = self.conf.get("ie", "iedriver") iedriverabspath = os.path.join(self.__projectpath, iedriverpath) self.__logger.info("获取到IE Driver") return iedriverabspath except Exception, e: self.__logger.error("get_IEDriver()方法出现异常", e)
class FileChecK(): def __init__(self): self.__fileabspath = None #不可访问的 self.__logger = LogObj() ''' 是否存在指定的文件,路径默认为当前项目的目录 ''' def is_has_file(self, filename): propath = self.getProjectPath() boolean = self.is_path_has_file(propath, filename) return boolean ''' 指定目录下是否存在指定的文件 ''' def is_path_has_file(self, path, filename): boolean = self.check_has_file(path, filename) return boolean ''' 扫描指定目录下的所有文件,找到所要找的文件,return True or False ''' def check_has_file(self, path, filename): try: for filep, dirs, filelist in os.walk(path): for fl in filelist: if cmp(fl, filename) == 0: #这个字符串的比较存在风险,python3不支持,待修改 self.__fileabspath = os.path.join(filep, fl) self.__logger.info("查找的%s文件存在" % filename) return True return False except Exception, e: self.__logger.error("check_has_file()方法出现异常", e)
class SignIn(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.wd = WebDriverDoBeforeTest() self.wd.className = "studentSignin.Signin" self.wd.beforeClass() self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_SigninPage() #返回到student 签到的页面 self.webdriverpai = WebdriverApi(self.weblogin.driver) self.em = ExcelManager("UsersLogin.xls") def tearDown(self): unittest.TestCase.tearDown(self) self.wd.afterClass() self.weblogin.driver.close() def test_student_singin(self): try: self.logger.info("SignIn.test_student_singin 测试用例已执行") driver = self.weblogin.driver api = self.webdriverpai tes_xls_data = self.em.readexcel("students") #得到sheet页 nrows = tes_xls_data.nrows print "------ excel中的测试用例条数是:", nrows - 1 #excel中 的数据行数从1行开始 for i in range(1, nrows): #for循环式,默认从0--n-1 print "------ 这是第:", i, " 条用例" row_A = tes_xls_data.cell(i, 0).value row_B = tes_xls_data.cell(i, 1).value row_C = tes_xls_data.cell(i, 2).value print row_A, row_B, row_C clss = Select(driver.find_element_by_id("class_id")).options random.choice(clss).click() driver.implicitly_wait(2) lab = Select(driver.find_element_by_id("lab_id")).options random.choice(lab).click() driver.implicitly_wait(2) driver.find_element_by_id("name").clear() driver.find_element_by_id("name").send_keys(row_A) driver.implicitly_wait(2) driver.find_element_by_id("stuno").clear() driver.find_element_by_id("stuno").send_keys(row_B) driver.implicitly_wait(2) driver.find_element_by_id("info").clear() driver.find_element_by_id("info").send_keys(row_C) driver.implicitly_wait(2) driver.find_element_by_id("sign").click() b = driver.find_element_by_class_name( "layui-layer-btn0").is_displayed() if b: driver.find_element_by_class_name( "layui-layer-btn0").click() self.weblogin.back_SigninPage() #返回到student 签到的页面 except Exception, e: self.logger.error("SignIn.test_student_singin 测试用例出现异常" + str(e))
class WebDriverDoBeforeTest(): def __init__(self): self.driver = None self.className = None self.beforeSuiteStarts = 0 self.afterSuiteStops = 0 self.beforeClassStarts = 0 self.afterClassStops = 0 self.beforeTestStarts = 0 self.afterTestStops = 0 fc = FileChecK() boolean = fc.is_has_file("framework.ini") if boolean: self.projectpath = fc.getProjectPath() self.fwInipath = fc.get_fileabspath() self.logger = LogObj() self.capturePath = os.path.join( self.projectpath, Config(self.fwInipath).get("capturePath", "capturePath")) def getDriverTooler(self, initbrowsername, baseURL): initbrowser = InitBrowser() initbrowser.beforeTestInitBrowser(initbrowsername, baseURL) self.driver = initbrowser.getWebDriver() return self.driver def beforeSuite(self): begins = DateTimeManager().formatedTime("%Y-%m-%d %H:%M:%S:%f") self.beforeSuiteStarts = time.time() self.logger.info("======" + begins + ":测试集开始======") def afterSuite(self): ends = DateTimeManager().formatedTime("%Y-%m-%d %H:%M:%S:%f") self.afterSuiteStops = time.time() self.logger.info("======" + ends + ":测试集结束======") self.logger.info("======本次测试集运行消耗时间 " + str(self.afterSuiteStops - self.beforeSuiteStarts) + " 秒!======") def beforeClass(self): begins = DateTimeManager().formatedTime("%Y-%m-%d %H:%M:%S:%f") self.beforeClassStarts = time.time() self.logger.info("======" + str(begins) + ":测试【" + str(self.className) + "】开始======") def afterClass(self): ends = DateTimeManager().formatedTime("%Y-%m-%d %H:%M:%S:%f") self.afterClassStops = time.time() self.logger.info("======" + str(ends) + ":测试【" + str(self.className) + "】结束======") self.logger.info("======本次测试运行消耗时间 " + str(self.afterClassStops - self.beforeClassStarts) + " 秒!======") def beforeTest(self, methodName): begins = DateTimeManager().formatedTime("%Y-%m-%d %H:%M:%S:%f") self.beforeTestStarts = time.time() self.logger.info("======" + begins + ":案例【" + str(self.className) + "." + methodName + "】开始======") def afterTest(self, methodName, isSucceed): ends = DateTimeManager().formatedTime("%Y-%m-%d %H:%M:%S:%f") captureName = "" if (isSucceed): self.logger.info("案例 【" + str(self.className) + "." + methodName + "】 运行通过!") else: dateTime = DateTimeManager().formatedTime("-%Y%m%d-%H%M%S%f") captureName = self.capturePath + str( self.className) + "." + methodName + str(dateTime) + ".png" self.captureScreenshot(captureName) self.logger.error("案例 【" + str(self.className) + "." + methodName + "】 运行失败,请查看截图快照:" + captureName) self.logger.info("======" + ends + ":案例【" + str(self.className) + "." + methodName + "】结束======") afterTestStops = time.time() self.logger.info("======本次案例运行消耗时间 " + str(afterTestStops - self.beforeTestStarts) + " 秒!======") return captureName ''' * 截取屏幕截图并保存到指定路径 * @param name:保存屏幕截图名称 * @return 无 ''' def capture(self, name): time.sleep(3) dateTime = DateTimeManager().formatedTime("-%Y%m%d-%H%M%S-%f") captureName = self.capturePath + name + dateTime + ".png" self.captureScreenshot(captureName) self.logger.debug("请查看截图快照:" + captureName) ''' * 截取屏幕截图并保存到指定路径 * @param filepath:保存屏幕截图完整文件名称及路径 * @return 无 ''' def captureScreenshot(self, filepath): try: self.driver.get_screenshot_as_file(filepath) except Exception, e: self.logger.error("保存屏幕截图失败,失败信息:" + str(e))
class Class_Management(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.wd = WebDriverDoBeforeTest() self.wd.className = "Class_Management" self.wd.beforeClass() self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_LoginPage(3) self.driver = self.weblogin.driver self.driverapi = WebdriverApi(self.driver) self.em = ExcelManager("UsersLogin.xls") def tearDown(self): unittest.TestCase.tearDown(self) self.driver.close() self.wd.afterClass() def test_ClassManagement(self): '''网站管理员班级管理测试用例''' self.logger.info("Class_Management.test_ClassManagement测试用例已执行") api = self.driverapi loginlab = self.weblogin tes_xls_data = self.em.readexcel("users") nrows = tes_xls_data.nrows for i in range(1, nrows): typeNum = tes_xls_data.cell(i, 2).value if typeNum == '4': user = tes_xls_data.cell(i, 0).value passwd = tes_xls_data.cell(i, 1).value print "实验室管理员账号密码及类型", user, passwd, typeNum b = loginlab.userType_login(user, passwd, typeNum) if b: api.click(By.XPATH, "//a[@href='classmanage']") boolean = True while boolean: api.click(By.ID, "ref_but") api.click(By.ID, "add_clazz_but") api.webList_RandomSelectByOption(By.ID, "grade_id", 5) api.webList_RandomSelectByOption(By.ID, "major_id", 5) api.webList_RandomSelectByOption(By.ID, "class_no", 5) api.click(By.ID, "save_clazz_but") bb = api.is_Displayed(By.CLASS_NAME, "layui-layer-content") if bb: content = api.getText(By.CLASS_NAME, "layui-layer-content") if "班级信息存在" == content: api.click(By.CLASS_NAME, "layui-layer-btn0") continue else: api.click(By.CLASS_NAME, "layui-layer-btn0") api.click(By.CLASS_NAME, "close") boolean = False break print "班级管理测试用例通过"
class LaboratoryManagement(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.wd = WebDriverDoBeforeTest() self.wd.className = "LaboratoryManagement" self.wd.beforeClass() self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_LoginPage(3) self.driver = self.weblogin.driver self.driverapi = WebdriverApi(self.driver) self.em = ExcelManager("UsersLogin.xls") def tearDown(self): unittest.TestCase.tearDown(self) self.driver.close() self.wd.afterClass() def test_LaboratoryManagement(self): '''实验员管理测试用例''' self.logger.info( "LaboratoryManagement.test_LaboratoryManagement测试用例已执行") api = self.driverapi loginlab = self.weblogin tes_xls_data = self.em.readexcel("users") nrows = tes_xls_data.nrows for i in range(1, nrows): typeNum = tes_xls_data.cell(i, 2).value if typeNum == '2': user = tes_xls_data.cell(i, 0).value passwd = tes_xls_data.cell(i, 1).value print "实验室管理员账号密码及类型", user, passwd, typeNum b = loginlab.userType_login(user, passwd, typeNum) if b: api.click(By.XPATH, "//a[@href='lab_tech']") api.click(By.ID, "ref_but") api.click(By.ID, "add_lab_tech_but") num = random.randint(1, 20) newname = "jayzhen" + str(num) api.sendKeys(By.ID, "lab_tech_name_text", newname) newemail = "*****@*****.**" api.sendKeys(By.ID, "lab_tech_email_text", newemail) newqq = str(random.randint(111111, 999999999)) api.sendKeys(By.ID, "lab_tech_qq_text", newqq) newphone = str(random.randint(18311111111, 18399999999)) api.sendKeys(By.ID, "lab_tech_phone_text", newphone) api.webList_RandomSelectByOption(By.ID, "lab_id", 3) api.click(By.ID, "save_lab_tech_but") bb = api.is_Displayed(By.CLASS_NAME, "layui-layer-btn0") if bb: errorcont = api.getText(By.CLASS_NAME, "layui-layer-btn0") self.logger.debug("填完新增实验员的内容后,点击确定,提示:%s" % errorcont) api.click(By.CLASS_NAME, "layui-layer-btn0") api.click(By.CLASS_NAME, "close") print "实验室管理测试用例通过"
class ApplyLab(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.wd = WebDriverDoBeforeTest() self.wd.className ="ApplyLab" self.wd.beforeClass() self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_LoginPage(3) self.driver = self.weblogin.driver self.driverapi = WebdriverApi(self.driver) self.em = ExcelManager("UsersLogin.xls") def tearDown(self): unittest.TestCase.tearDown(self) self.driver.close() self.wd.afterClass() def test_ApplyLab(self): '''教师申请实验室用例''' self.logger.info("ApplyLab.test_ApplyLab测试用例已执行") api = self.driverapi loginlab = self.weblogin tes_xls_data = self.em.readexcel("users") nrows = tes_xls_data.nrows for i in range(1,nrows): typeNum = tes_xls_data.cell(i,2).value if typeNum == '3': user = tes_xls_data.cell(i,0).value passwd = tes_xls_data.cell(i,1).value print "实验室管理员账号密码及类型",user, passwd, typeNum b = loginlab.userType_login(user, passwd, typeNum) if b: api.click(By.XPATH, "//a[@href='apply']") api.webList_RandomSelectByOption(By.ID,"year_id", 2) api.webList_RandomSelectByOption(By.ID,"lab_id", 2) api.webList_RandomSelectByOption(By.ID,"class_id", 2) api.webList_RandomSelectByOption(By.ID,"course_id", 2) bb = True while bb : begin = random.randint(1,68) api.sendKeys(By.ID, "ksjc", str(begin)) end = random.randint(begin+1,begin+3) api.sendKeys(By.ID, "jsjc", str(end)) show = api.getText(By.ID, "ErrorInfo") ebtn = api.is_Displayed(By.CLASS_NAME, "layui-layer-btn0") if ebtn: api.click(By.CLASS_NAME, "layui-layer-btn0") continue if show.strip()=="": bb = False print "执行选择周次" beginWeek = random.randint(1,4) api.selectByValue(By.ID,"kszc",str(beginWeek), 5) endWeek = random.randint(beginWeek+1,beginWeek+5) print "结束周次是:%s"%endWeek api.selectByValue(By.ID,"jszc",str(endWeek), 5) self.logger.debug("课时选择提示:%s"%show) api.click(By.ID, "check_but") show2 = api.getText(By.ID, "ErrorInfo") if "可申请,请点击右侧申请按钮进行申请" == show2: api.click(By.ID, "apply_but") api.click(By.CLASS_NAME, "layui-layer-btn0") result = api.getText(By.CLASS_NAME,"layui-layer-content") self.logger.debug("申请实验室的结果:%s" %result) api.click(By.CLASS_NAME, "layui-layer-btn0") print "申请实验室测试用例通过"
class PersonalCenter(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.wd = WebDriverDoBeforeTest() self.wd.className = "PersonalCenter" self.wd.beforeClass() self.logger = LogObj() self.weblogin = web_login() self.weblogin.back_LoginPage(3) self.driver = self.weblogin.driver self.driverapi = WebdriverApi(self.driver) self.em = ExcelManager("UsersLogin.xls") def tearDown(self): unittest.TestCase.tearDown(self) self.driver.close() self.wd.afterClass() def test_tester_Personal_Center(self): '''实验员个人中心测试用例''' self.logger.info( "test_PersonalCenter.test_tester_Personal_Center测试用例已执行") api = self.driverapi loginlab = self.weblogin tes_xls_data = self.em.readexcel("users") nrows = tes_xls_data.nrows for i in range(1, nrows): #for循环式,默认从0--n-1 typeNum = tes_xls_data.cell(i, 2).value if typeNum == '1': user = tes_xls_data.cell(i, 0).value passwd = tes_xls_data.cell(i, 1).value print "实验室实验员账号密码及类型", user, passwd, typeNum b = loginlab.userType_login(user, passwd, typeNum) if b: api.click(By.XPATH, "//a[@href='myinfo']") api.click(By.ID, "ref_but") api.click(By.ID, "update_myinfo_but") oldname = api.getText(By.ID, "show_name") #api.sendKeys(By.ID, "input_name_text", oldname) oldemail = api.getText(By.ID, "show_email") #api.sendKeys(By.ID, "input_email_text",oldemail ) newQQ = "1157757910" api.sendKeys(By.ID, "input_tencentno_text", newQQ) newPhone = "18511403089" api.sendKeys(By.ID, "input_phone_text", newPhone) bb = api.isSelected(By.ID, "is_change_pw") if bb is False: api.scrollbar_SlideTOBottom('main_table_left_box') api.click(By.ID, "is_change_pw") newPW = "123456" api.sendKeys(By.ID, "input_pw_text", newPW) api.click(By.ID, "go_update_myinfo_but") api.accept_alert() loginlab.back_LoginPage(2) self.logger.debug( "个人中心信息修改为:%s::%s::%s::%s::%s" % (oldname, oldemail, newQQ, newPhone, newPW)) print "个人中心信息修改测试用例通过"