自动化登录账号密码并截图保存
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
class LoginCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
# 定义登录方法
def login(self, username, password):
# 登录到网站
self.driver.get('........')
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div/div/form/div[1]/div/div/input').send_keys(username)
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div/div/form/div[2]/div/div/input').send_keys(password)
self.driver.find_element(By.XPATH, '//*[@id="app"]/div/div[1]/div/div/form/div[4]/div/button/span').click()
def test_login_success(self):
# 正确用户名密码
self.login('test1', '12345')
time.sleep(2)
# 图片截图储存位置
self.driver.get_screenshot_as_file("F:\pycharm+selenium+zidonghua\\login_success.png")
def test_login_pwd_error(self):
# 正确用户名 错误密码
self.login('test1', '123')
time.sleep(2)
self.driver.get_screenshot_as_file("F:\pycharm+selenium+zidonghua\\pwd_error.png")
def test_login_user_success(self):
# 错误用户名 正确密码
self.login('test', '12345')
time.sleep(2)
self.driver.get_screenshot_as_file("F:\pycharm+selenium+zidonghua\\user_sucess.png")
def test_user__null(self):
# 空用户名 正确密码
self.login('', '12345')
time.sleep(2)
self.driver.get_screenshot_as_file("F:\pycharm+selenium+zidonghua\\user__null.png")