selenium自动化登录测试用例案例

import unittest
import  time
from selenium import  webdriver
class IwebshopLoginTest(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Firefox()
        #self.driver.maximize_window()
        self.driver.implicitly_wait(30)
        self.driver.get(r"http://bbs.aircheng.com/")
        time.sleep(1)
    def test_login(self):
        self.driver.find_element_by_link_text("登录").click()
        self.driver.find_element_by_name("username").send_keys("testww")
        self.driver.find_element_by_name("password").send_keys("wxm123456")
        self.driver.find_element_by_xpath(".//*[@id='J_u_login_form']/div/dl[4]/dd/button").click()
        login_info=self.driver.find_element_by_class_name("username").text
        try:
            self.assertIn("testww",login_info)
        except AssertionError as e:
            print("登录失败")
            self.driver.get_screenshot_as_file("./images/%s.png" % str(time.time()).replace(".","_"))
            raise
        time.sleep(3)

    def test_login1(self):
        self.driver.find_element_by_link_text("登录").click()
        self.driver.find_element_by_name("username").send_keys("testww1")
        self.driver.find_element_by_name("password").send_keys("wxm123456")
        self.driver.find_element_by_xpath(".//*[@id='J_u_login_form']/div/dl[4]/dd/button").click()
        login_info=self.driver.find_element_by_class_name("username").text
        try:
            self.assertIn("testww1",login_info)
        except AssertionError as e:
            print("登录失败")
          #截图
            self.driver.get_screenshot_as_file("./images/%s.png" % str(time.time()).replace(".","_"))
            raise
        time.sleep(3)


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

利用HTMLTestRunner.py文件生成测试报告

from iwebshop import IwebshopLoginTest
from tools.HTMLTestRunner import HTMLTestRunner
import unittest
suite=unittest.TestSuite()
suite.addTest(unittest.makeSuite(IwebshopLoginTest))

with open("./reports/iwebshop.html","wb") as f:
    h=HTMLTestRunner(stream=f,title="测试报告",description="windows Firefox")
    h.run(suite)

你可能感兴趣的:(#,自动化测试,selenium,自动化,测试用例)