Unittest method - to test method begins with examples Unittest - Project automatically send e-mail

 

 This post is below test as actual combat:  unittest methods - Project automatically send e-mail

1、test_01 

import unittest
from selenium import webdriver

class BaiduLink(unittest.TestCase):
def setUp(self):
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get("Http://www.baidu.com")

def tearDown(self):
self.driver.quit()

def test_baidu_news(self):
"""百度首页:点击新闻链接是否可正常跳转"""
self.driver.find_element_by_link_text("新闻").click()
self.assertEqual(self.driver.current_url,"http://news.baidu.com/")

test_baidu_map DEF (Self):
"" "Baidu home page: click on the map link is normally jump" ""
self.driver.find_element_by_link_text ( "map") .click ()
self.assertIn ( "https://map.baidu. COM / ", self.driver.current_url)

IF the __name__ == '__main__':
unittest.main (the verbosity = 2)


two, test_02
import unittest
from selenium import webdriver

class BaiduSo(unittest.TestCase):
def setUp(self):
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get("Https://www.baidu.com/")

def tearDown(self):
self.driver.quit()

def test_baidu_so_enabled(self):
"""百度首页:百度首页输入框可编辑"""
so = self.driver.find_element_by_id("kw")
"""断言"""
self.assertTrue(so.is_enabled())

def test_baidu_so(self):
"""百度首页:测试百度有搜索功能"""
self.driver.find_element_by_id("kw").send_keys("selenium")
# so = send_key("selenium")
self.driver.find_element_by_id("su").click()
"""断言"""
# self.assertEqual(self.driver.find_element_by_id("kw").text, "selenium")

if __name__ == '__main__':
unittest.main(verbosity=2)

三、test_03
# coding=utf-8
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time
import unittest

class BlogTitle(unittest.TestCase):

def setUp(self):
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.get("https://www.cnblogs.com/Teachertao/")
self.driver.implicitly_wait(10)

def tearDown(self):
self.driver.quit()

def test_blog_01(self):
u"""验证元素title完全等于 Teacher涛 - 博客园"""
# time.sleep(3)
result = EC.title_is(u'Teacher涛 - 博客园')(self.driver)
# print(result())
self.assertTrue(result)

def test_blog_02(self):
u"""验证元素判断title包含Teacher"""
# time.sleep(3)
title = EC.title_contains("Teacher")(self.driver)
# print(title())
self.assertTrue(title)


if __name__ == "__main__":
unittest.main(verbosity=2)

四、test_04
from selenium import webdriver
import unittest
import time
class Blog(unittest.TestCase):
def setUp(self):
'''登录博客'''
self.option = webdriver.ChromeOptions()
self.option.add_argument("headless")
self.driver = webdriver.Chrome(chrome_options=self.option)
self.driver.get("https://passport.cnblogs.com/user/signin")
self.driver.implicitly_wait(10)

def login(self,username,passwd):
'''这里写了一个登录的方法,账号和密码参数化'''
self.driver.find_element_by_id("LoginName").send_keys(username)
self.driver.find_element_by_id("Password").send_keys(passwd)
self.driver.find_element_by_id ( "IsRemember") the Click ().
self.driver.find_element_by_id ( "submitBtn") the Click ().
the time.sleep (3)

DEF is_login_sucess (Self):
'' 'to determine whether to acquire login account name '' '
the try:
text = self.driver.find_element ( "the above mentioned id", "header_user_left") text.
Print (text)
return True
the except:
return False

DEF test_login (Self):
' '' Login PORTFOLIO: account numbers, passwords themselves set '' '
self.login ( "XXX", "XXX")
# determination result
result = self.is_login_sucess ()
Self.assertTrue (the Result)

DEF test_02 (Self):
U '' 'Log PORTFOLIO: account numbers, passwords set up their own' ''
self.login (u "xxxx", u "xxxx") # call the login method
# get the name of the account after login
text = self.driver.find_element_by_id ( "lnk_current_user"). text
Print (text)
# assert actual results to expected results consistent
self.assertEqual (text, U "XXX")

DEF the tearDown (Self):
self.driver.quit ()

IF the __name__ == '__main__':
unittest.main ()

five, test_05
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import unittest
class BolgHome(unittest.TestCase):
u'''博客首页'''
@classmethod
def setUpClass(cls):
cls.option = webdriver.ChromeOptions()
cls.option.add_argument("headless")
cls.driver = webdriver.Chrome(chrome_options=cls.option)
url = "https://www.cnblogs.com/Teachertao/"
cls.driver.get(url)
cls.driver.implicitly_wait(30)

@classmethod
def tearDownClass(cls):
cls.driver.quit()

def test_01(self):
u'''验证元素存在:博客园'''
= Locator ( "the above mentioned id", "blog_nav_sitehome")
text = U "blog park"
the Result = EC.text_to_be_present_in_element (Locator, text) (self.driver)
self.assertTrue (the Result)

DEF test_02 (Self):
U '' 'verification elements are present: Home '' '
Locator = ( "ID", "blog_nav_myhome")
text U = "Home"
Result = EC.text_to_be_present_in_element (Locator, text) (self.driver)
self.assertTrue (Result)

DEF test_03 (Self) :
U '' 'to verify the presence of elements: send new essay' ''
Locator = ( "ID", "blog_nav_newpost")
text = U "send new essay"
Result = EC.text_to_be_present_in_element(locator,text)(self.driver)
self.assertTrue(result)

if __name__ == "__main__":
unittest.main()

 

Guess you like

Origin www.cnblogs.com/Teachertao/p/11183585.html