python+selenium自动化测试之登录

Posted 坚持是一种习惯

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python+selenium自动化测试之登录相关的知识,希望对你有一定的参考价值。

selenium_login.py


import unittest
from selenium import webdriver


class LoginTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()

        cls.driver.get(http://pms.yuncesu12.cn/login)
# 用户名错误密码正确
    def testlogin(self):
        login_account = self.driver.find_element_by_name(account)
        login_account.clear()
        login_account.send_keys()
        # name = self.driver.find_element_by_xpath("//input[@class=‘layui-input‘]")
        login_password = self.driver.find_element_by_name(password)
        login_password.clear()
        login_password.send_keys(123456)
        login_button = self.driver.find_element_by_class_name(layui-btn)
        login_button.click()
        print(用户名或密码错误)


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


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


import unittest
from selenium import webdriver


class LoginTest1(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()

        cls.driver.get(http://pms.yuncesu12.cn/login)

    # 用户名正确密码错误
    def testlogin1(self):
        login_account = self.driver.find_element_by_name(account)
        login_account.clear()
        login_account.send_keys(墨子)
        # name = self.driver.find_element_by_xpath("//input[@class=‘layui-input‘]")
        login_password = self.driver.find_element_by_name(password)
        login_password.clear()
        login_password.send_keys(12346)
        login_button = self.driver.find_element_by_class_name(layui-btn)
        login_button.click()
        print(用户名或密码错误)

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


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




selenium_login2.py

import unittest
from selenium import webdriver


class LoginTest2(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()

        cls.driver.get(‘http://pms.yuncesu.cn/login‘)

    # 用户名密码都错误
    def testlogin2(self):
        login_account = self.driver.find_element_by_name(‘account‘)
        login_account.clear()
        login_account.send_keys(‘墨‘)
        # name = self.driver.find_element_by_xpath("//input[@class=‘layui-input‘]")
        login_password = self.driver.find_element_by_name(‘password‘)
        login_password.clear()
        login_password.send_keys(‘12346‘)
        login_button = self.driver.find_element_by_class_name(‘layui-btn‘)
        login_button.click()
        print(‘用户名或密码错误‘)

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


if __name__ == ‘__main__‘:
    unittest.main(verbosity=2)
selenium_login3.py


import unittest
from selenium import webdriver


class LoginTest3(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()

        cls.driver.get(http://pms.yuncesu12.cn/login)

    # 用户名正确密码为空
    def testlogin3(self):
        login_account = self.driver.find_element_by_name(account)
        login_account.clear()
        login_account.send_keys(‘‘)
        # name = self.driver.find_element_by_xpath("//input[@class=‘layui-input‘]")
        login_password = self.driver.find_element_by_name(password)
        login_password.clear()
        login_password.send_keys(‘‘)
        login_button = self.driver.find_element_by_class_name(layui-btn)
        login_button.click()
        print(用户名或密码不能为空)

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


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


import unittest
from selenium import webdriver


class LoginTest4(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(5)
        cls.driver.maximize_window()

        cls.driver.get(http://pms.yuncesu12.cn/login)

    # 用户名正确都正确
    def testlogin4(self):
        login_account = self.driver.find_element_by_name(account)
        login_account.clear()
        login_account.send_keys(墨子)
        # name = self.driver.find_element_by_xpath("//input[@class=‘layui-input‘]")
        login_password = self.driver.find_element_by_name(password)
        login_password.clear()
        login_password.send_keys(123456)
        login_button = self.driver.find_element_by_class_name(layui-btn)
        login_button.click()
        print(登录成功!)

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


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


from selenium_login import LoginTest
from selenium_login1 import  LoginTest1
from selenium_login2 import LoginTest2
from selenium_login3 import LoginTest3
from selenium_login4 import LoginTest4
import unittest

login_test = unittest.TestLoader().loadTestsFromTestCase(LoginTest)
login_test1 = unittest.TestLoader().loadTestsFromTestCase(LoginTest1)
login_test2 = unittest.TestLoader().loadTestsFromTestCase(LoginTest2)
login_test3 = unittest.TestLoader().loadTestsFromTestCase(LoginTest3)
login_test4 = unittest.TestLoader().loadTestsFromTestCase(LoginTest4)

smoke_test = unittest.TestSuite([login_test,login_test1,login_test2 ,login_test3,login_test4])

if __name__ == __main__:
    unittest.TextTestRunner(verbosity=2).run(smoke_test)

 

以上是关于python+selenium自动化测试之登录的主要内容,如果未能解决你的问题,请参考以下文章

Python实战之Selenium自动化测试web刷新FW

一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

用python+selenium进行一次cnblog的登录测试

selenium+python自动化测试--数据驱动