def test_news_type(self): '''事件分类用例''' classify = news(self.driver) screen = BasePage(self.driver) #进入要闻页 classify.classify() screen.get_screen_shot() time.sleep(1) #分类公告摘要 classify.classify_ggzy() screen.get_screen_shot() time.sleep(1) #分类大型评级 classify.classify_dxpj() screen.get_screen_shot() time.sleep(1) #分类业绩会直击 classify.classify_yjhzj() screen.get_screen_shot() time.sleep(1) #分类港股异动 classify.classify_ggyd() screen.get_screen_shot() time.sleep(1) #分类公司信息 classify.classify_gsxx() screen.get_screen_shot() time.sleep(1) #分类行业信息 classify.classify_hyxx() screen.get_screen_shot() time.sleep(1) #全部 classify.classify_all() screen.get_screen_shot()
def test_swipe(self): '''事件列表的滑动用例''' shijian = news(self.driver) screen = BasePage(self.driver) #进入事件列表 shijian.classify() #向上滑动两次 swipe_up(self, 2) screen.get_screen_shot()
def test_wlan(self): self.initbase = BasePage(self.driver) self.initbase.click("l => 新闻") try: self.assertTrue("热点要闻" in self.initbase.find_element( 's => a[data-control="pane-news"]').text) except Exception as e: logger.error("Fail to connect wlan: %s" % e) self.initbase.get_windows_screen()
def test_return_key(self): '''事件详情页返回键用例''' details01 = news(self.driver) screen = BasePage(self.driver) #进入详情页 details01.list_button() screen.get_screen_shot() #返回键 details01.font_button() screen.get_screen_shot()
def setUp(self): # 驱动浏览器 self.driver = self.select.selectBrowser(self.browserName) #隐士等待10秒钟 self.driver.implicitly_wait(10) #浏览器最大化 self.driver.maximize_window() #打开被测网址 base = BasePage(self.driver) base.open(self.url)
def test_graphic_videoplay(self): self.initbase = BasePage(self.driver) self.driver.get("https://www.iqiyi.com/v_19rsjaio7k.html") time.sleep(10) try: self.assertTrue("神话" in self.initbase.find_element("s => span.header-link").text) except Exception as e: logger.error("Fail to play video: %s" % e) self.initbase.get_windows_screen()
def test_graphic_muiscplay(self): self.initbase = BasePage(self.driver) self.driver.get("https://www.kugou.com/song/#hash=BE6372BA684D2B195FEB691FD8DFC6CC&album_id=528482") self.initbase.click('toggle') time.sleep(10) try: self.assertTrue("Five Hundred Miles" in self.initbase.find_element("c => audioName").text) except Exception as e: logger.error("Fail to play music: %s" % e) self.initbase.get_windows_screen()
def test_font(self): '''事件详情页字体放大缩小用例''' details02 = news(self.driver) screen = BasePage(self.driver) # 进入详情页 details02.list_button() #放大 details02.font_button() time.sleep(2) screen.get_screen_shot() #缩小 details02.font_button() time.sleep(2) screen.get_screen_shot()
def test_login_author_dy(self): '''专栏作家的订阅测试用例''' # 进入专栏作家详情页 zl.zuojia_icon() # 点击订阅 zl_author.dy_button() # 断言 Text = self.driver.find_element_by_id( 'com.gelonghui.glhapp:id/btn_subscribe') text = Text.text self.assertEqual('已订阅', text) screen = BasePage(self.driver) screen.get_screen_shot()
def test_wlan_Disconnect(self): self.initbase = BasePage(self.driver) subprocess.check_call("netsh interface set interface Wi-Fi disabled", shell=True) self.driver.get("https://wwww.baidu.com") try: self.assertTrue("No internet" in self.initbase.find_element( 's => span[jsselect = "heading"]').text) except Exception as e: logger.error("Fail to disconnect wlan: %s" % e) self.initbase.get_windows_screen() finally: subprocess.check_call( "netsh interface set interface Wi-Fi enabled", shell=True) time.sleep(10)
def test_login_shoucang(self): '''专栏文章收藏''' # 进入专栏文章详情页 zl.zl_details() # 点击收藏按钮 text_shoucang01 = self.driver.find_element_by_id( 'com.gelonghui.glhapp:id/tv_rd_fav_num').text zl.shoucang() screen = BasePage(self.driver) screen.get_screen_shot() # 断言 Text = self.driver.find_element_by_id( 'com.gelonghui.glhapp:id/tv_main_title') text_shoucang02 = Text.text self.assertEqual(text_shoucang01 + 1, text_shoucang02)
def log_out(self): self.driver.find_element_by_xpath( '//*[@id="topFrame"]/ul/li[4]/a/i').click() time.sleep(1) self.driver.find_element_by_xpath( '/html/body/div[6]/div[2]/div[2]/button[2]').click() time.sleep(1) txt = self.driver.find_element_by_xpath( '/html/body/div[1]/div/div/div/form/div/div[2]/a').text logoutpage = BasePage(self.driver) try: assert "修改密码" in txt logger.info("登出成功。") except Exception as e: logger.error("登出失败。", e) logoutpage.get_windows_img() # 调用基类截图方法
def test_login_dingyue(self): '''专栏订阅测试用例''' # 进入专栏详情页 zl.zl_details() # 点击订阅按钮 zl.dingyue() # 未登录去登录 android_login.login_text('17512064966', '995217') #订阅 zl.dingyue() time.sleep(1) screen = BasePage(self.driver) screen.get_screen_shot() text_dingyue = self.driver.find_element_by_id( 'com.gelonghui.glhapp:id/btn_subscribe').text self.assertEqual('已订阅', text_dingyue)
def test_login_like(self): '''专栏文章点赞''' # 进入专栏文章详情页 zl.zl_details() text_like01 = self.driver.find_element_by_id( 'com.gelonghui.glhapp:id/tv_rd_like_num').text # 点击爱心按钮 zl.like() screen = BasePage(self.driver) screen.get_screen_shot() # 断言 Text = self.driver.find_element_by_id( 'com.gelonghui.glhapp:id/tv_rd_like_num') text_like02 = Text.text #断言点赞成功,数量+1 self.assertEqual(text_like01 + 1, text_like02)
def get_pictures(self, selector): file_path = os.path.dirname(os.path.abspath('.')) + '/screenshots/' page = BasePage(self.driver) page.get_code_img() page_snap_obj = Image.open(file_path + 'code.png') img = page.find_element(selector) # 验证码元素位置 time.sleep(1) location = img.location size = img.size # 获取验证码的大小参数 left = location['x'] top = location['y'] right = left + size['width'] bottom = top + size['height'] image_obj = page_snap_obj.crop( (left, top, right, bottom)) # 按照验证码的长宽,切割验证码 # image_obj.show() # 打开切割后的完整验证码 return image_obj
def test_nologin_comment(self): '''专栏文章评论测试用例''' # 进入文章详情页 zl.zl_details() # 点击评论按钮 zl.comment() # 输入评论 zl.send_comment('文章不错') # 提交评论 zl.get_comment() time.sleep(1) screen = BasePage(self.driver) screen.get_screen_shot() # 断言 text = self.driver.find_element_by_id( 'com.gelonghui.glhapp:id/comment_content').text self.assertEqual('文章不错', text)
def log_in(self): self.driver.find_element_by_xpath('//*[@id="username"]').clear() self.driver.find_element_by_xpath('//*[@id="username"]').send_keys('yangfan5') self.driver.find_element_by_xpath('//*[@id="password"]').clear() self.driver.find_element_by_xpath('//*[@id="password"]').send_keys('ccs.xiangyun') self.driver.find_element_by_xpath('//*[@id="authcode"]').clear() self.driver.find_element_by_xpath('//*[@id="authcode"]').send_keys('aaaa') self.driver.find_element_by_xpath('//input[@name="submit"]').click() time.sleep(2) name = self.driver.find_element_by_xpath('//*[@id="_userName"]').text loginpage = BasePage(self.driver) try: assert "杨帆" in name logger.info("登录成功。") except Exception as e: logger.info("登录失败。", e) loginpage.get_windows_img() # 调用基类截图方法
def test_case_run(self): basepage = BasePage(self.driver) ts = TestSuites() #excel表内关键字列表 [('type', 'id=>kw', '输入搜索'), (), ()] action_list = KeyCases().key_case_list_all() test_step_dict = TestSteps().do_teststep_id() print action_list for i in range(len(action_list)): if action_list[i][0] == 'type': basepage.type(action_list[i][1], 'selenium') time.sleep(3) print 'selenium %d' %i print action_list[i][2] x = int(ts.get_key(test_step_dict, action_list[i][2])) print 'x:',x rb = open_workbook(r"D:\je_workspace\PythonCase\src\excel\testcase.xls") wb = copy(rb) sheet = wb.get_sheet(3) sheet.write(x+1, 5, 'pass') os.remove(r"D:\je_workspace\PythonCase\src\excel\testcase.xls") wb.save(r"D:\je_workspace\PythonCase\src\excel\testcase.xls") time.sleep(3) elif action_list[i][0] == 'search': basepage.click(action_list[i][1]) time.sleep(3) print 'search' x = int(ts.get_key(test_step_dict, action_list[i][2])) print 'x:',x rb = open_workbook(r"D:\je_workspace\PythonCase\src\excel\testcase.xls") wb = copy(rb) sheet = wb.get_sheet(3) sheet.write(x+1, 5, 'pass') os.remove(r"D:\je_workspace\PythonCase\src\excel\testcase.xls") wb.save(r"D:\je_workspace\PythonCase\src\excel\testcase.xls") time.sleep(3) else: raise NameError ('invalid method')
def test_baidu_search(self): """ 这里一定要test开头,把测试逻辑代码封装到一个test开头的方法里。 :return: """ homepage = Baidu(self.driver) ea = BasePage(self.driver) homepage.type_search('selenium') # 调用页面对象中的方法 ea.wait(10) homepage.send_submit_btn() #调用页面对象类中的点击搜索按钮方法 ea.wait(10) homepage.get_windows_img() time.sleep(5) # 调用基类截图方法 try: assert 'selenium' in homepage.get_page_title( ) # 调用页面对象继承基类中的获取页面标题方法 print('Test Pass.') except Exception as e: print('Test Fail.', format(e))
def test_wlan_login_pass(self): username="******" password="******" self.initbase = BasePage(self.driver) self.driver.get('https://gia-captiveportal.intel.com/guest/guest_hello_c.php?mac=84:3a:4b:c8:a8:f0&_browser=1') self.initbase.type('s => input#ID_form80308bd3_weblogin_username',username) self.initbase.type('s => input#ID_form80308bd3_weblogin_password',password) js_bottom = "var q=document.documentElement.scrollTop=10000" self.driver.execute_script(js_bottom) time.sleep(1) self.initbase.click('s => input#ID_form80308bd3_weblogin_visitor_accept_terms') self.initbase.click('s => input#ID_form80308bd3_weblogin_submit') self.initbase.get_windows_screen() try: self.assertTrue('Logging in' in self.initbase.find_element("s => div#content-marker > p").text) except Exception as e: logger.error("No logging message was printed: %s" %e) self.initbase.get_windows_screen()
def test_wlan_login_fail(self): wlan_dict={#"miss_username":['','19911992'], "miss_password":['*****@*****.**',''], "incorrect_username":['weix.b.sun.com','19911992'], "incorrect_password":['*****@*****.**','12345678']} self.initbase = BasePage(self.driver) for key in wlan_dict: self.driver.get('https://gia-captiveportal.intel.com/guest/guest_hello_c.php?mac=84:3a:4b:c8:a8:f0&_browser=1') self.initbase.type('s => input#ID_form80308bd3_weblogin_username',wlan_dict[key][0]) self.initbase.type('s => input#ID_form80308bd3_weblogin_password',wlan_dict[key][1]) js_bottom = "var q=document.documentElement.scrollTop=10000" self.driver.execute_script(js_bottom) time.sleep(1) self.initbase.click('s => input#ID_form80308bd3_weblogin_visitor_accept_terms') self.initbase.click('s => input#ID_form80308bd3_weblogin_submit') self.initbase.get_windows_screen() try: if key.find("username"): self.assertTrue('Invalid username' in self.initbase.find_element("ERROR_form39b008c_weblogin_username").text) else: self.assertTrue('enter a value' in self.initbase.find_element("ERROR_form39b008c_weblogin_password").text) except Exception as e: logger.error("No error message was printed: %s" %e)
from selenium import webdriver from framework.base_page import BasePage import time driver = BasePage("driver") driver.wait(10) driver.get("http://testhotel.hblckj.cn/login.html") driver.send_keys("xpath=/html/body/div[2]/div[2]/form/div[1]/input","18640857881") driver.send_keys("xpath=/html/body/div[2]/div[2]/form/div[2]/input","jt123456") driver.click("xpath=/html/body/div[2]/div[2]/form/div[4]/div/a") driver.wait(5) driver.set_window_size(600,600) # driver.sleep(3) driver.max_window() driver.get_window_img() driver.clear("xpath=/html/body/div[3]/div/div[1]/div[2]/form/div[1]/input") driver.right_click("xpath=/html/body/div[3]/div/div[1]/div[2]/form/div[1]/input") driver.move_to_element("xpath=/html/body/div[1]/div/ul/li[1]/a") driver.back() driver.forward() driver.get_attribute("xpath=/html/body/div[3]/div/div[1]/div[2]/form/div[5]/button","type") driver.get_text("xpath=/html/body/div[3]/div/div[1]/div[2]/form/div[5]/button")
# coding=utf-8 import os import unittest import time import sys sys.path.append("..") from framework.base_page import BasePage from HTMLTestRunner import HTMLTestRunner # make test_report dir report_path_make = BasePage("") report_path = report_path_make.make_file_path(os.getcwd(), "test_report") now_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime()) HtmlFile = report_path + now_time + "HTMLtemplate.html" case_path = os.path.join(os.path.abspath('.'), "testsuites\\") result = file(HtmlFile, "wb") discover_suite = unittest.defaultTestLoader.discover(os.getcwd(), pattern="TC*.py", top_level_dir=None) if __name__ == '__main__': runner = HTMLTestRunner(stream=result, title=u"web test report", description=u"All test case run result:") runner.run(discover_suite)
def setUp(self): bro = BasePage(self) self.driver = bro.open_browser()
def setUpClass(cls): cls.driver = IsOpenBrower(cls) cls.baseFn = BasePage(cls.driver) cls.indexPageFn = IndexPage(cls.driver) cls.driver.get('https://www.baidu.com/')
class CreatOrder(object): driver = webdriver.Chrome() basepage = BasePage(driver) def open_yongche(self): self.basepage.open_url("https://sso.yongche.org/") time.sleep(1) def login_yongche(self): username = "******" keys = "Zls@8891" self.driver.find_element_by_id('J_login').send_keys(username) time.sleep(1) self.driver.find_element_by_id('J_pwd').send_keys(keys) time.sleep(1) self.driver.find_element_by_id('id_submit').click() time.sleep(1) def jump_handler(self): self.driver.find_element_by_link_text('首页').click() self.driver.implicitly_wait(10) self.driver.find_element_by_xpath( "//*[contains(text(),'ERP')]").click() self.driver.implicitly_wait(10) currentwinfirstpage = self.driver.current_window_handle handles = self.driver.window_handles for handle in handles: if handle != currentwinfirstpage: self.driver.switch_to.window(handle) def find_user(self): self.driver.switch_to.frame("content") self.driver.find_element_by_id("cellphone").send_keys("16801015609") self.driver.find_element_by_id("cellphone_search").click() def create_order(self): #等待元素出现后,开始下面的代码 locator = (By.ID, 'btn_create') WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(locator)) #拖动滚动条到指定位置 target = self.driver.find_element_by_id("btn_create") self.driver.execute_script("arguments[0].scrollIntoView();", target) target.click() self.driver.implicitly_wait(10) #判断radio是否被选中 radioselected = self.driver.find_element_by_id("use_type_17") if radioselected.is_selected() == False: radioselected.click() self.driver.implicitly_wait(10) self.driver.find_element_by_name("start_position").click() self.driver.find_element_by_xpath( "//*[contains(text(),'中谷酒店')]").click() self.driver.implicitly_wait(10) self.driver.find_element_by_name("end_position").click() self.driver.implicitly_wait(10) self.driver.find_element_by_xpath( "//*/div[@id='end_addrlist']/li/a[contains(text(),'北京西站')]").click( ) self.driver.implicitly_wait(10) #拖动滚动条到指定位置 target1 = self.driver.find_element_by_xpath( "//*/input[@type='submit']") self.driver.execute_script("arguments[0].scrollIntoView();", target1) self.driver.find_element_by_id("car_type").click() self.driver.find_element_by_xpath("//*/option[@value='37']").click() self.driver.implicitly_wait(10) self.driver.find_element_by_xpath( "//*/select[@name='corporate_id']").click() self.driver.find_element_by_xpath("//*/option[@value='0']").click() self.driver.implicitly_wait(10) self.driver.find_element_by_xpath("//*/span[@id='self-use']").click() self.driver.implicitly_wait(10) self.driver.find_element_by_xpath( "//*/input[@name='passenger_phone']").click() self.driver.implicitly_wait(10) #判断checkbox是否被选中 is_auto_dispatch = self.driver.find_element_by_xpath( "//*/input[@id='is_auto_0']") if is_auto_dispatch.is_selected() == False: is_auto_dispatch.click() self.driver.implicitly_wait(10) send_message_to_passenger = self.driver.find_element_by_xpath( "//*/input[@name='passenger']") if send_message_to_passenger.is_selected(): send_message_to_passenger.click() self.driver.implicitly_wait(10) self.driver.find_element_by_xpath("//*/input[@type='submit']").click() self.driver.implicitly_wait(10) def send_order_to_alldrivers(self): target1 = self.driver.find_element_by_xpath( "//*/input[@id='selectCarButton']") self.driver.execute_script("arguments[0].scrollIntoView();", target1) target1.click() self.driver.find_element_by_xpath("//*/a[@id='allCheck']").click() self.driver.find_element_by_xpath("//*/input[@id='formSub']").click() # 判断是否弹出Alert窗口,如果弹出就点击确定,并且返回True,不是就返回False result = EC.alert_is_present()(self.driver) if result: result.accept() print("No driver") self.driver.get_screenshot_as_file( "/Users/zhaolisi/PycharmProjects/ScreenShots/No_driver_found.png" ) return True else: return False def choose_driver(self): target2 = self.driver.find_element_by_xpath( "//*/input[@id='driverCPhoneNumber']") self.driver.execute_script("arguments[0].scrollIntoView();", target2) target2.send_keys("16812345678") self.driver.find_element_by_xpath( "//*/input[@id='selectCarButton']").click() self.driver.find_element_by_xpath("//*/input[@id='formSub']").click() #判断是否弹出Alert窗口,如果弹出就点击确定,并且返回True,不是就返回False result = EC.alert_is_present()(self.driver) if result: result.accept() print("The driver is not online") self.driver.get_screenshot_as_file( "/Users/zhaolisi/PycharmProjects/ScreenShots/No_driver_found.png" ) return True else: print("The driver is online") return False ''' #判断元素是否存在,如果存在就点击,不存在就保存图片,返回False if self.basepage.iselementexist("//*/input[@value='确定使用']") == True: self.driver.find_element_by_xpath("//*/input[@value='确定使用']").click() return True else: self.driver.get_screenshot_as_file("/Users/zhaolisi/PycharmProjects/ScreenShots/No_driver_found.png") return False ''' def cancle_order(self): target3 = self.driver.find_element_by_xpath("//*/input[@value='取消订单']") self.driver.execute_script("arguments[0].scrollIntoView();", target3) target3.click() #time.sleep(1) locator = (By.ID, 'cancel_reason') WebDriverWait(self.driver, 10, 0.5).until(EC.element_to_be_clickable(locator)) self.driver.find_element_by_xpath("//*/option[@value='12']").click() self.driver.implicitly_wait(10) self.driver.find_element_by_id("J_cancel_submit").click() time.sleep(1) self.driver.switch_to.alert.accept() time.sleep(1) def quit_browser(self): self.basepage.quit_browser()
from selenium import webdriver from framework.base_page import BasePage from logs.log import log1 driver = webdriver.Chrome() driver.get("https://www.baidu.com") s = driver.window_handles kw = ('id', 'kw') base_driver = BasePage(driver) base_driver.send(kw, 'selenium+python') base_driver.framework_sleep(2) su = ('id', 'su') base_driver.click(su) base_driver.get_img() base_driver.framework_sleep(3) log1.info(base_driver.get_title()) base_driver.framework_quit()
def __init__(self): op_br = OpenBrowser() self.driver = op_br.get_driver() self.basepage = BasePage() self.Login = LoginPage()