1.if ...else ...判断进行断言
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from
time
import
*
from
selenium
import
webdriver
def
login(user
=
"admin"
,pwd
=
"123456"
):
driver
=
webdriver.Chrome()
driver.implicitly_wait(
10
)
driver.get(
"http://192.168.1.110:8080/cms"
)
driver.maximize_window()
sleep(
1
)
driver.find_element_by_id(
"userAccount"
).send_keys(user)
sleep(
1
)
driver.find_element_by_id(
"loginPwd"
).send_keys(pwd)
sleep(
1
)
driver.find_element_by_id(
"loginBtn"
).click()
#登录
sleep(
1
)
users
=
driver.find_element_by_class_name(
"c-white"
).text
#获取用户名
sleep(
1
)
driver.quit()
if
users[
6
:]
=
=
user:
pass
else
:
raise
AssertionError
if
__name__
=
=
'__main__'
:
login()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from
time
import
*
from
selenium
import
webdriver
def
login(user
=
"admin"
,pwd
=
"123456"
):
driver
=
webdriver.Chrome()
driver.implicitly_wait(
10
)
driver.get(
"http://192.168.1.110:8080/cms"
)
driver.maximize_window()
sleep(
1
)
driver.find_element_by_id(
"userAccount"
).send_keys(user)
sleep(
1
)
driver.find_element_by_id(
"loginPwd"
).send_keys(pwd)
sleep(
1
)
driver.find_element_by_id(
"loginBtn"
).click()
#登录
sleep(
1
)
users
=
driver.find_element_by_class_name(
"c-white"
).text
#获取用户名
sleep(
1
)
driver.quit()
if
users[
6
:
7
]
=
=
user:
pass
else
:
assert
False
if
__name__
=
=
'__main__'
:
login()
2.try ... except ...断言
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from
time
import
*
from
selenium
import
webdriver
def
fatie(title,body):
dr
=
login()
sleep(
2
)
dr.find_element_by_partial_link_text(
"版块"
).click()
sleep(
1
)
dr.find_element_by_id(
"subject"
).send_keys(title)
sleep(
1
)
dr.find_element_by_id(
"fastpostmessage"
).send_keys(body)
sleep(
2
)
dr.find_element_by_name(
"topicsubmit"
).click()
sleep(
2
)
try
:
dr.find_element_by_id(
"thread_subject"
)
except
:
raise
AssertionError
else
:
pass
sleep(
3
)
dr.quit()
3.assert断言(基于unittest)
- assertIn(arg1, arg2, msg=None) 验证 arg1 是 arg2 的子串,不是则 fail
- assertNotIn(arg1, arg2, msg=None) 验证 arg1 不是 arg2 的子串,是则 fail
- assertEqual(arg1, arg2, msg=None) 验证 arg1=arg2,不等则 fail
- assertNotEqual(arg1, arg2, msg=None) 验证 arg1 != arg2, 相等则 fail
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import
time
import
unittest
class
Test(unittest.TestCase):
def
test01(
self
):
self
.assertEqual(
2
,
2
,msg
=
"test01 error!"
)
def
test02(
self
):
self
.assertNotEqual(
1
,
2
,msg
=
"test02 error!"
)
def
test03(
self
):
self
.assertIn(
"fang"
,
"xinfangshuo"
,msg
=
"test03 error!"
)
def
test04(
self
):
self
.assertNotIn(
"xing"
,
"xinfangshuo"
,msg
=
"test04 error!"
)
def
tearDown(
self
):
time.sleep(
1
)
if
__name__
=
=
'__main__'
:
unittest.main()
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:485187702【暗号:csdn11】
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】
版权归原作者 程序员Baby~ 所有, 如有侵权,请联系我们删除。