登录自动化测试及登录与退出封装
1.登录测试代码举例如下:
2.登录、退出封装
可以使用login()#调用登录,logout()#调用登录
部分代码示例:
#coding=utf-8 #防止乱码
'''------------------------售卖登录页面自动化test--------------------'''
#导入selenium的webdriver包,使用webdriver API进行自动化脚本开发
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import csv
driver=webdriver.Chrome() #谷歌浏览器
#driver=webdriver.Firefox() #火狐浏览器
#Options=webdriver.ChromeOptions()
driver.get("http://IP/#/login")#向浏览器发送网址(URL)
#参数数字为像素点
print("设置浏览器宽1500、高800显示")
driver.set_window_size(1500,800)
print('Before login================================================')
#打印当前title
title=driver.title
print(title)
#打印当前URL
now_url=driver.current_url
print(now_url)
#登录
def login():
#清除登录账户输入提示
driver.find_element_by_name("username").clear()
#需要用xpath来定位输入框
driver.find_element_by_xpath("//input[@class='el-input__inner']").send_keys("账号")
#清除密码提示显示
driver.find_element_by_name("password").clear()
#有相同元素属性,需要用and同时确定密码输入框
driver.find_element_by_xpath("//*[@placeholder='password' and @class='el-input__inner']").send_keys("密码")
#是否显示密码,单击按键
driver.find_element_by_xpath("//span[@class='show-pwd']").click()
driver.find_element_by_xpath("//button[@class='el-button el-button--primary']").click()
time.sleep(3)
print('After login================================================')
title = driver.title#打印当前title
print(title)
now_url=driver.current_url#打印当前URL
print(now_url)
#获得登录的用户名
user =driver.find_element_by_css_selector(".user-name").text
print(user)
#退出
def logout():
time.sleep(1)#设置等待时间
#打开账号注销页签
driver.find_element(By.CSS_SELECTOR, ".el-icon-caret-bottom").click()
time.sleep(1)#设置等待时间
#选中“退出”单机按键
driver.find_element(By.CSS_SELECTOR, ".el-dropdown-menu__item > span").click()
#退出并关闭浏览器及相关驱动程序
driver.quit()