登录页面页面元素少,只有简单的用户名和密码,然后点击登录按钮即可,没有太多难度,一步一步来基本没什么问题,我直接把代码贴上来,每一句我都加了注释。在定位元素的时候我用的最多的还是xpath方法,这个方法有一些漏洞,关于定位元素,还需要深入研究,目前使用的都是最基础最简单的,还是遇到一些搞不定的情况。总体而言,登录页面我没有遇到什么大坑,比较顺利。
from time import sleep
import pytest
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
class NaxinUserLogin(object):
def __init__(self):
#启动chrome浏览器
self.driver = webdriver.Chrome()
#指定我们需要的页面
self.driver.get('http://10.100.103.204:10001/dist/index.html#/login')
#最大化窗口
self.driver.maximize_window()
# 测试用户登录成功
def test_user_login_ok(self):
username = 'admin'
pwd = '111111'
expected = 'xxx系统'
# 定位输入用户名,并且输入用户名
self.driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/form/div[1]/div/div[1]/input').clear()
self.driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/form/div[1]/div/div[1]/input').send_keys(username)
# 定位输入密码,并且输入密码
self.driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/form/div[2]/div/div/input').clear()
self.driver.find_element_by_xpath('//*[@id="app"]/div/div[2]/form/div[2]/div/div/input').send_keys(pwd)
# 定位点击登录,并且点击
self.driver.find_element_by_class_name('el-button').click()
#设置等待
sleep(3)
#验证登录之后的页面title和我们想要的title一致,一致表示登录成功
assert self.driver.title == expected