python用selenium做自动化测试(二)
- checkbox实践
测试对象网址
代码先贴上:
1 from selenium import webdriver
2 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
3 import time
4
5 binary="/usr/local/firefox/firefox"
6 driver=webdriver.Firefox(firefox_binary=binary)
7
8 driver.get("http://sahitest.com/demo/formTest.htm")
#因为该网页刚开始有一个警告框,所以解散所有警告框
9 driver.switch_to_alert().dismiss()
#先选择所有checkbox
10 checkboxes=driver.find_elements_by_css_selector('input[type=checkbox]')
11 for checkbox in checkboxes:
12 checkbox.click()
#每隔2秒勾选一次
13 time.sleep(2)
#统计checkbox的个数
14 print(len(checkboxes))
#把页面上最后一个勾给去掉
15 driver.find_elements_by_css_selector('input[type=checkbox]').pop().click()
16
17 driver.quit()
刚开始遇到一个报错:
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status: 1
最后发现原因是用非root用户进入了。
- 多窗口切换
1 #!/usr/bin/python3.6
2 #-*- coding:utf-8 -*-
3
4 from selenium import webdriver
5 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
6 import time
7
8 binary="/usr/local/firefox/firefox"
9 driver=webdriver.Firefox(firefox_binary=binary)
10 driver.implicitly_wait(10)
11 driver.get("https://www.baidu.com/")
#获得当前窗口的句柄并赋值
12 sreach_windows=driver.current_window_handle
#打开登录弹窗
13 driver.find_element_by_link_text('登录').click()
#从登录弹窗上打开新的注册窗口
14 driver.find_element_by_xpath("//*[@id='passport-login-pop-dialog']/div/div/div/div[4]/a").click()
15 #获取当前打开的所有窗口句柄
16 all_handles=driver.window_handles
17 for handle in all_handles:
18 if handle != sreach_windows:
19 driver.switch_to.window(handle)
20 print('now register window')
21 driver.find_element_by_name("username").send_keys('zhengjiani')
22 time.sleep(2)
23 for handle in all_handles:
24 if handle == sreach_windows:
25 driver.switch_to.window(handle)
26 print('now sreach window!')
27 driver.find_element_by_id('TANGRAM__PSP_4__closeBtn').click()
28 driver.find_element_by_id("kw").send_keys("selenium")
29 driver.find_element_by_id("su").click()
30 time.sleep(2)
31 driver.quit()
- 上传文件
虫师书中是对windows控件的操作部分,故不做实验
- 下载文件
1 #!/usr/bin/python3.6
2 #-*- coding:utf-8 -*-
3 from selenium import webdriver
4 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
5 import os
6
7 fp=webdriver.FirefoxProfile()
8 fp.set_preference("browser.download.folderList",2)
9 fp.set_preference("browser.download.manager.showWhenStarting",False)
10 fp.set_preference("browser.download.dir",os.getcwd())
11 fp.set_preference("browers.helperApps.neverAsk.saveToDisk","application/octet-stream")
12 binary="/usr/local/firefox/firefox"
13 driver=webdriver.Firefox(firefox_binary=binary,firefox_profile=fp)
14 driver.get("http://pypi.Python.org/pypi/selenium")
15 driver.find_element_by_xpath("//*[@id='mobile-files-tab']").click()
代码就是这样,but网速太渣没出结果
- 调用JS
1 #!/usr/bin/python3.6
2 #-*- coding:utf-8 -*-
3 from selenium import webdriver
4 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
5 from time import sleep
6 binary="/usr/local/firefox/firefox"
7 driver=webdriver.Firefox(firefox_binary=binary)
8 driver.get("https://www.baidu.com")
9 #设置浏览器窗口大小
10 driver.set_window_size(600,600)
11 driver.find_element_by_id("kw").send_keys("selenium")
12 driver.find_element_by_id("su").click()
13 sleep(2)
14 js="window.scrollTo(100,450);"
15 driver.execute_script(js)
16 sleep(3)
17 driver.quit()
- 验证码处理
模块化驱动测试实例:
线性:
1 from selenium import webdriver
2 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
3 import time
4
5 binary=FirefoxBinary('/usr/local/firefox/firefox')
6 driver=webdriver.Firefox(firefox_binary=binary)
7 print('Before login================')
8 title=driver.title
9 print(title)
10 now_url=driver.current_url
11 print(now_url)
12
13 driver.implicitly_wait(10)
14 driver.get("https://mail.qq.com/")
15 driver.switch_to_frame("login_frame")
16 driver.find_element_by_id("u").clear()
17 driver.find_element_by_id("u").send_keys("*")
18 driver.find_element_by_xpath("//*[@id='p']").clear()
19 driver.find_element_by_xpath("//*[@id='p']").send_keys("*")
20 driver.find_element_by_xpath("//*[@id='login_button']").click()
21 print('After login===================')
22 title=driver.title
23 print(title)
24 now_url=driver.current_url
25 print(now_url)
26 user=driver.find_element_by_xpath("//*[@id='useralias']").text
27 print(user)
28 driver.quit()
模块化:
#登录与退出 public.py
1 #-*- coding:utf-8 -*-
2 class Login():
3 def user_login(self,driver):
4 driver.switch_to_frame("login_frame")
5 driver.find_element_by_id("u").clear()
6 driver.find_element_by_id("u").send_keys("936089353@qq.com")
7 driver.find_element_by_xpath("//*[@id='p']").clear()
8 driver.find_element_by_xpath("//*[@id='p']").send_keys("Nicezheng_1995@")
9 driver.find_element_by_xpath("//*[@id='login_button']").click()
10 def user_logout(self,driver):
11 driver.find_element_by_link_text("退出").click()
12 driver.quit()
#主文件 mailTest.py
1 #-*- coding:utf-8 -*-
2 from selenium import webdriver
3 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
4 from public import Login
5 binary=FirefoxBinary('/usr/local/firefox/firefox')
6 driver=webdriver.Firefox(firefox_binary=binary)
7 driver.implicitly_wait(10)
8 driver.get("https://mail.qq.com/")
9
10 Login().user_login(driver)
11 Login().user_logout(driver)
数据驱动测试(参数化)
1 #-*- coding:utf-8 -*-
2 class Login():
3 def user_login(self,driver,username,password):
4 driver.switch_to_frame("login_frame")
5 driver.find_element_by_id("u").clear()
6 driver.find_element_by_id("u").send_keys(username)
7 driver.find_element_by_xpath("//*[@id='p']").clear()
8 driver.find_element_by_xpath("//*[@id='p']").send_keys(password)
9 driver.find_element_by_xpath("//*[@id='login_button']").click()
10 def user_logout(self,driver):
11 driver.find_element_by_link_text("退出").click()
12 driver.quit()
1 #-*- coding:utf-8 -*-
2 from selenium import webdriver
3 from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
4 from public import Login
5
6 class LoginTest():
7 def __init__(self):
8
9 binary=FirefoxBinary('/usr/local/firefox/firefox')
10 self.driver=webdriver.Firefox(firefox_binary=binary)
11 self.driver.implicitly_wait(10)
12 self.driver.get("https://mail.qq.com/")
13 def test_admin_login(self):
14 username='*'
15 password='*'
16 Login().user_login(self.driver,username,password)
17
18 LoginTest().test_admin_login()
unittest自动识别测试用例并执行
测试用例:
#calculator.py
1 class Count():
2 def __init__(self,a,b):
3 self.a=int(a)
4 self.b=int(b)
5 def add(self):
6 return self.a+self.b
7 def sub(self):
8 return self.a-self.b
#testadd.py
1 from calculator import Count
2 import unittest
3
4 class TestAdd(unittest.TestCase):
5 def setUp(self):
6 print("test case start")
7 def tearDown(self):
8 print("test case end")
9 def test_add(self):
10 j=Count(2,3)
11 self.assertEqual(j.add(),5)
12 def test_add2(self):
13 j=Count(41,76)
14 self.assertEqual(j.add(),117)
15 if __name__=='__main__':
16 unittest.main()
17
#testsub.py
1 from calculator import Count
2 import unittest
3
4
5 class TestSub(unittest.TestCase):
6 def setUp(self):
7 print("test case start")
8 def tearDown(self):
9 print("test case end")
10 def test_add(self):
11 j=Count(2,3)
12 self.assertEqual(j.sub(),-1)
13 def test_add2(self):
14 j=Count(71,46)
15 self.assertEqual(j.sub(),25)
16 if __name__=='__main__':
17 unittest.main()
#runtest.py
1 import unittest
2 test_dir='./'
3 discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")
4 if __name__=='__main__':
5 runner=unittest.TextTestRunner()
6 runner.run(discover)
[root@localhost unittestcase]# python runtest.py
test case start
test case end
.test case start
test case end
.test case start
test case end
.test case start
test case end
.
----------------------------------------------------------------------
Ran 4 tests in 0.000s
OK