From selenium import Webdriver
Import UnitTest
Import Time
class Blog (unittest. TestCase):
"" " Login Blog Park " "
# Add @classmethod adorner, open browser only once
@classmethod
def setupclass (CLS):
"" " Initialize " "
Cls.browser = Webdriver. Chrome ()
cls.url = ' Https://passport.cnblogs.com/user/signin '
cls.browser.get (Cls.url)
cls.browser.implicitly_wait (+)
Cls.browser.maximize_window ()
def login (self, username, password):
"" " login account password parameterization " "
self.browser.find_element_by_id (' input1 '). Send_keys (username)
self.browser.find_element_by_id (' Input2 '). Send_keys (password)
self.browser.find_element_by_id (' signin '). Click ()
Time.sleep (3)
def is_login_sucess (self):
"" " determines whether to obtain the account name after login " "
Time.sleep (2)
Try:
Text = self.browser.find_element_by_id (' Lnk_current_user '). Text
print (text)
return True
except:
return False
def back_out (self):
# Exit back to login page
Time.sleep (2)
Self.browser.find_element_by_xpath ('//*[@id = ' header_user_right ']/a[5] '). Click ()
Time.sleep (2)
# Alert method to remove the exit bullet box
s = Self.browser.switch_to_alert ()
s.accept ()
Time.sleep (2)
Self.browser.find_element_by_xpath ('//*[@id = ' header_user_left ']/a[1] '). Click ()
Time.sleep (2)
def clear_box (self):
"" To empty the input box "" "
self.browser.find_element_by_id (' input1 '). Clear ()
self.browser.find_element_by_id (' Input2 '). Clear ()
time.sleep (1)
def test_01 (self):
"" Call the login function login "" "
self.login (' uesrname ', ' password ')
result = self.is_login_sucess ()
self.asserttrue (Result)
def test_02 (self):
"" "The error of the demonstration" ""
self.back_out ()
Self.clear_box ()
self.login (' 1 ', ' 2 ')
result = self.is_login_sucess ()
self.asserttrue (result, msg= ' failed, did not get the account name after login! ')
@classmethod
def teardownclass (CLS):
cls.browser.quit ()
if __name__ = = ' __main__ ':
"" generates HTML report "" "
Import Htmltestrunner
# now = Time.s
Now = Time.strftime ('%y-%m-%d%h-%m-%s ')
report_title = ' Blog Park login Automation test '
# Report_path = R ' D:\PythonProject\com\report.html '
Report_path = ' d:\\pythonproject\\com\\ ' + now + ' report.html '
explain = ' Blog Park login test report '
test_suite = unittest. TestSuite ()
test_suite.addtest (Blog (' test_01 '))
test_suite.addtest (Blog (' test_02 '))
with open (Report_path, ' WB ') as FP:
runner = Htmltestrunner.htmltestrunner (Title=report_title, STREAM=FP, Description=explain)
Runner.run (test_suite)
fp.close ()
Open only once browser, generate HTML test report < small tension in ......>