from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
from test_admin_login import TestAdminLogin
class TestCategory(object):
# 因为需要建立再登录之上的操作
def __init__(self, login):
self.login = login
def test_add_category_success(self):
name = 'www'
parent = '顶级'
slug = 'test'
expect = None
# 点击文章 //*[@id="sidebar-menu"]/li[4]/a/span[1]
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(1)
# 点击分类
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a').click()
sleep(1)
# 输入分类名称
self.login.driver.find_element_by_name('category.title').send_keys(name)
# 选择父分类
parent_category_elem = self.login.driver.find_element_by_name('category.pid')
# 用到Select这个类
Select(parent_category_elem).select_by_visible_text(parent)
# 输入slug
self.login.driver.find_element_by_name('category.slug').clear()
self.login.driver.find_element_by_name('category.slug').send_keys(slug)
# 点击提交
self.login.driver.find_element_by_xpath(
'/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button').click()
# 没有异常就添加成功直接断言成功就好了
if __name__ == '__main__':
login = TestAdminLogin() # 登录
login.test_admin_login_success()
cate = TestCategory(login) # 传入登录
cate.test_add_category_success()