自动化测试中各种流量的模拟4-selenium篇

xiaoxiao2021-02-27  190

1、对于自动化测试中的http流量的创建,最强大的一种应该是直接使用selenium来控制浏览器行为了,这里我自己搭建了一个PHP网站来完成http的各种操作

2、http的上传数据的操作,代码如下:

def faq_add(myusername,mypassword,faqname,faqanswer): firefoxpath = '/usr/lib/firefox/' #对于火狐浏览器需要使用相应的驱动程序geckodriver os.environ['webdirver.firefox.driver'] = firefoxpath browser = webdriver.Firefox(firefoxpath) browser.get('http://172.19.23.41/phpmyfaq/?action=login') browser.maximize_window() #最大化窗口方便查看-* try: username = browser.find_element_by_id("faqusername"); username.clear() username.send_keys(myusername); password = browser.find_element_by_id("faqpassword"); password.clear() password.send_keys(mypassword); logon = browser.find_element_by_xpath("//button[@class='btn btn-lg btn-primary btn-block']") logon.click(); time.sleep(3) #火狐的浏览器跟IE机制还不一样,要经常sleep来控制,防止元素未刷新时就去寻找,导致失败 newfaq = browser.find_element_by_link_text("Add new FAQ") newfaq.click(); time.sleep(3) department = browser.find_element_by_id("rubrik"); Select(department).select_by_index(1); question = browser.find_element_by_id("question"); question.send_keys(faqname); answer = browser.find_element_by_id("answer"); answer.send_keys(faqanswer); submit = browser.find_element_by_id("submitfaq"); submit.click(); time.sleep(3) browser.quit() except Exception, e: print Exception, "could not login",str(e) time.sleep(3) browser.quit()

geckodriver和firefox是有对应关系的,目前我使用的版本为geckodriver16和firefox52,大家也要小心linux中的firefox自动更新,升级到新版本后很有可能geckodriver就不在支持firefox了,此时出现的典型现象就是:浏览器可以打开,但一直到不到元素

3、http上传文件附件的操作,代码如下:

def faq_upload(faqname,filepath,waittime): browser=faq_login('admin','Talent123') time.sleep(3) faq=browser.find_element_by_link_text(faqname) faq.click() time.sleep(3) faq_edit=browser.find_element_by_link_text('Edit Record') faq_edit.click() time.sleep(3) add_attachment=browser.find_element_by_link_text('Add new attachment') add_attachment.click() time.sleep(2) now_handle=browser.current_window_handle all_handle=browser.window_handles for handle in all_handle: if handle!=now_handle: browser.switch_to_window(handle) print 'change handle' #获取弹出的文件上传网页句柄 time.sleep(2) file_upload=browser.find_element_by_id('fileUpload') file_upload.send_keys(filepath) #对于常用的input type=file的输入框来说,直接就可以想input框里输入文件字符串,但要保证文件存在 time.sleep(2) file_submit=browser.find_element_by_xpath("//button[@class='btn btn-primary']") file_submit.click() time.sleep(waittime) #必须设定等待文件上传的时间,然后结束窗口 browser.quit()

4、http下载文件的代码如下:

def faq_download(filetype,myusername,mypassword,faqname,attachname,waittime): profile = webdriver.FirefoxProfile() #对于火狐浏览器,可以直接用selenium来编辑浏览器配置,设置下载TXT文件不需要进行询问 profile.set_preference('browser.download.folderList', 0) profile.set_preference('browser.download.manager.showWhenStarting', False) profile.set_preference('browser.helperApps.neverAsk.saveToDisk',filetype)#text/plain也是标准的mime类型,可以在标准文档里寻找其他类型的代码 firefoxpath = '/usr/lib/firefox/' os.environ['webdirver.firefox.driver'] = firefoxpath browser = webdriver.Firefox(firefox_profile=profile) browser.get('http://172.19.23.41/phpmyfaq/?action=login') browser.maximize_window() try: username = browser.find_element_by_id("faqusername"); username.clear() username.send_keys(myusername); password = browser.find_element_by_id("faqpassword"); password.clear() password.send_keys(mypassword); logon = browser.find_element_by_xpath("//button[@class='btn btn-lg btn-primary btn-block']") logon.click(); except Exception, e: print Exception, "could not login", str(e) time.sleep(3) browser.quit() time.sleep(3) faq = browser.find_element_by_link_text(faqname) faq.click() time.sleep(3) download=browser.find_element_by_link_text(attachname) download.click() time.sleep(waittime) browser.quit()

对于http的文件附件下载有一个比较麻烦的事情,firefox浏览器在点击要下载的文件后不会自动下载,而是会弹出一个确认框,让你确认是否下载,而这个确认框是windows的弹出框,使用selenium是定位不到了。这里我使用的方式是直接修改firefox的配置文件,让下载指定的文件不需要确定,直接下载

还有点麻烦的是虽说文件类型使用标准mime类型,但具体到一些文件,比如金山的wps文件就都被识别成bin文件,需要在mime中查找bin文件的文件类型,还有微软的office系列的升级文件类型,如docx、xlsx、pptx这种,在mime中也是有相应的文件类型代码的,但firefox还是一律识别成bin文件

对于selenium的定位问题如果比较难定位可以使用过xpath来定位,但最好不要使用绝对位置定位,如果非用不可的话可以采用绝对定位与相对定位结合的方式,如下

check_box=browser.find_element_by_xpath("/html/body/div/div[3]/div[2]/div[2]/div[1]/div/div[2]/div/div/div/div[3]/table//input[@class='ember-view ember-checkbox']") check_box.click()

这样会让定位的试用性强一点

转载请注明原文地址: https://www.6miu.com/read-11783.html

最新回复(0)