Open only once browser, generate HTML test report < small tension in ......>

Source: Internet
Author: User


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 ......>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.