import unittest
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time as t
def add(a,b):
return a+b
class TestAdd(unittest.TestCase):
def test_add_int(self):
assert add(3,5)==8
1 import unittest
2 from selenium import webdriver
3 from selenium.webdriver.common.by import By
4 from selenium.webdriver.common.action_chains import ActionChains
5 from test.init import Init
6 import time as t
7
8 class TestBaidu(Init):
9 def setUp(self) -> None:
10 self.driver = webdriver.Chrome()
11 self.driver.maximize_window()
12 self.driver.get("https://www.baidu.com/")
13
14
15 def tearDown(self) -> None:
16 self.driver.quit()
17
18
19 def test_baidu_title(self):
20 '''百度首页:验证百度的title是否正确'''
21 # assert self.driver.title=="百度一下,你就知道"
22 self.assertEqual(self.driver.title, "百度一下,你就知道")
23
24
25 def test_baidu_url(self):
26 '''百度首页:验证百度的网址是否正确'''
27 # 浏览器输入网址必须为https,域名后边必须有斜杠
28 # assert self.driver.current_url=="https://www.baidu.com/"
29 self.assertEqual(self.driver.current_url, "https://www.baidu.com/")
30
31
32 def test_baidu_so(self):
33 '''百度首页:验证搜索框输入的关键字是否是接口测试'''
34 so = self.driver.find_element(By.ID, 'kw')
35 so.send_keys("接口测试")
36 # assert so.get_attribute("value")=="接口测试"
37 self.assertEqual(so.get_attribute("value"), "接口测试")
38
39
40 def test_baidu_enabled(self):
41 '''百度首页:验证搜索框是否可编辑'''
42 so = self.driver.find_element(By.ID, 'kw')
43 self.assertTrue(so.is_enabled())
44
45 def test_baidu_displayed(self):
46 '''百度首页:验证关于百度的字条是否可见'''
47 so = self.driver.find_element(By.LINK_TEXT, '关于百度')
48 self.assertTrue(so.is_displayed())
49
50 def test_baidu_setting(self):
51 '''百度首页:验证百度首页的设置的搜索设置是否打开'''
52 t.sleep(3)
53 setting=self.driver.find_element(By.XPATH,'//*[@id="s-usersetting-top"]')
54 action=ActionChains(driver=self.driver)
55 action.move_to_element(setting).perform()
56 self.driver.find_element(By.XPATH,'//*[@id="s-user-setting-menu"]/div/a[1]/span').click()
57 t.sleep(3)
58 soSet=self.driver.find_element(By.XPATH,'//*[@id="wrapper"]/div[6]/div/div/ul/li[1]')
59 # assert devText.text=="搜索设置"
60 self.assertEqual(soSet.text,"搜索设置")
61
62 if __name__ == '__main__':
63 unittest.main()
1 import unittest
2 from selenium import webdriver
3 from selenium.webdriver.common.by import By
4 from selenium.webdriver.common.action_chains import ActionChains
5 from test.init import Init
6 import time as t
7
8 class TestBaidu(Init):
9 class TestBaidu(Init):
10 #使用setUpClass()方法
11 @classmethod
12 def setUpClass(cls) -> None:
13 cls.driver = webdriver.Chrome()
14 cls.driver.maximize_window()
15 cls.driver.get('http://www.baidu.com')
16 cls.driver.implicitly_wait(30)
17 #使用tearDownClass()类方法
18 @classmethod
19 def tearDownClass(cls) -> None:
20 cls.driver.quit()
21
22
23 def test_baidu_title(self):
24 '''百度首页:验证百度的title是否正确'''
25 # assert self.driver.title=="百度一下,你就知道"
26 self.assertEqual(self.driver.title, "百度一下,你就知道")
27
28
29 def test_baidu_url(self):
30 '''百度首页:验证百度的网址是否正确'''
31 # 浏览器输入网址必须为https,域名后边必须有斜杠
32 # assert self.driver.current_url=="https://www.baidu.com/"
33 self.assertEqual(self.driver.current_url, "https://www.baidu.com/")
34
35
36 def test_baidu_so(self):
37 '''百度首页:验证搜索框输入的关键字是否是接口测试'''
38 so = self.driver.find_element(By.ID, 'kw')
39 so.send_keys("接口测试")
40 # assert so.get_attribute("value")=="接口测试"
41 self.assertEqual(so.get_attribute("value"), "接口测试")
42
43
44 def test_baidu_enabled(self):
45 '''百度首页:验证搜索框是否可编辑'''
46 so = self.driver.find_element(By.ID, 'kw')
47 self.assertTrue(so.is_enabled())
48
49 def test_baidu_displayed(self):
50 '''百度首页:验证关于百度的字条是否可见'''
51 so = self.driver.find_element(By.LINK_TEXT, '关于百度')
52 self.assertTrue(so.is_displayed())
53
54 def test_baidu_setting(self):
55 '''百度首页:验证百度首页的设置的搜索设置是否打开'''
56 t.sleep(3)
57 setting=self.driver.find_element(By.XPATH,'//*[@id="s-usersetting-top"]')
58 action=ActionChains(driver=self.driver)
59 action.move_to_element(setting).perform()
60 self.driver.find_element(By.XPATH,'//*[@id="s-user-setting-menu"]/div/a[1]/span').click()
61 t.sleep(3)
62 soSet=self.driver.find_element(By.XPATH,'//*[@id="wrapper"]/div[6]/div/div/ul/li[1]')
63 # assert devText.text=="搜索设置"
64 self.assertEqual(soSet.text,"搜索设置")
65
66 if __name__ == '__main__':
67 unittest.main()
1 import unittest
2 from selenium import webdriver
3 from selenium.webdriver.common.by import By
4 from selenium.webdriver.common.action_chains import ActionChains
5 import time as t
6
7 class TestSina(unittest.TestCase):
8 def setUp(self) -> None:
9 self.driver=webdriver.Chrome()
10 self.driver.maximize_window()
11 self.driver.get("https://mail.sina.com.cn/#")
12 self.driver.implicitly_wait(30)
13
14 def tearDown(self) -> None:
15 self.driver.quit()
16
17 def test_username_null(self):
18 '''新浪首页:验证用户名为空时,直接登录时的提示信息是否正确'''
19 self.driver.find_element(By.CLASS_NAME, "loginBtn").click()
20 devText = self.driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
21 self.assertEqual(devText.text, '请输入邮箱名')
22
23 def test_email_error(self):
24 '''新浪首页:验证用户名的格式错误时登录的提示信息是否正确'''
25 self.driver.find_element(By.ID, 'freename').send_keys("dgg")
26 self.driver.find_element(By.ID, 'freepassword').send_keys("fhsk")
27 self.driver.find_element(By.CLASS_NAME, "loginBtn").click()
28 devText = self.driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
29 self.assertEqual(devText.text, '您输入的邮箱名格式不正确')
30
31 def test_username_password_error(self):
32 '''新浪首页:验证用户名和密码错误时登录的提示信息是否正确'''
33 self.driver.find_element(By.ID, 'freename').send_keys("134467@sina.com")
34 self.driver.find_element(By.ID, 'freepassword').send_keys("fhsk")
35 self.driver.find_element(By.LINK_TEXT, "登录").click()
36 t.sleep(5)
37 devText = self.driver.find_element(By.XPATH,'/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')
38 t.sleep(3)
39 self.assertEqual(devText.text, '登录名或密码错误')
40
41 if __name__ == '__main__':
42 unittest.main()
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |