selenium自动化测试用例

选用的测试系统是之前项目中自行开发的系统
系统详情见博客:https://blog.csdn.net/sunshine543123/article/details/106908203
编写的测试脚本:
1.登录测试脚本

from selenium import webdriver
driver = webdriver.Chrome()
def login(user,userpassword):
    driver.get("http://localhost:8080/servletApply_war_exploded/test.html")
    driver.implicitly_wait(5)

    driver.find_element_by_id('user').send_keys(user)
    search_text=driver.find_element_by_id('userpassword')
    search_text.send_keys(userpassword)
    search_text.submit()

    driver.implicitly_wait(5)

    handle = driver.current_url

    if handle == 'http://localhost:8080/servletApply_war_exploded/yinglang/index.html':
        print('账号:' + user + '\n密码:' + userpassword + '\n登录成功\n')
    else:
        print('账号:' + user + '\n密码:' + userpassword + '\n登录失败\n')

2.新闻添加测试脚本:

from selenium import webdriver
import time
driver = webdriver.Chrome()
def add(title,category,author,newsdate,editor):
    driver.get("http://localhost:8080/servletApply_war_exploded/yinglang/index.html")
    driver.find_element_by_xpath("//*[@id='menu']/li/a").click()
    driver.implicitly_wait(3)
    driver.find_element_by_xpath("//*[@id='menu']/li/ul/li[2]/a").click()
    driver.implicitly_wait(3)

    time.sleep(3)
    driver.switch_to.frame('menuFrame')
    time.sleep(3)

    # 定位到table,并获得table中所有得tr元素
    menu_table = driver.find_element_by_xpath("/html/body/div[2]/table")
    rows = menu_table.find_elements_by_tag_name('tr')
    # python 得len()函数返回对象(字符、列表、元组)得长度或者元素得个数
    before_add_numbers = len(rows)
    #点击添加
    driver.find_element_by_xpath("/html/body/div[1]/a").click()

    driver.find_element_by_id('title').send_keys(title)
    driver.find_element_by_id('category').send_keys(category)
    driver.find_element_by_id('author').send_keys(author)
    driver.find_element_by_id('newsdate').send_keys(newsdate)

    driver.switch_to.frame('ueditor_0')
    time.sleep(1)

    driver.find_element_by_xpath('/html/body').send_keys(editor)
    time.sleep(1)

    #从ueditor_0 frame切回主文档:
    driver.switch_to.default_content()
    time.sleep(1)
    driver.switch_to.frame('menuFrame')
    time.sleep(1)
    #点击保存
    driver.find_element_by_id("addN").click()
    time.sleep(1)

    #driver.switch_to.frame('menuFrame')
   #time.sleep(1)

    # 定位到table,并获得table中所有得tr元素
    menu_table = driver.find_element_by_xpath("/html/body/div[2]/table")
    rows = menu_table.find_elements_by_tag_name('tr')

    # python 得len()函数返回对象(字符、列表、元组)得长度或者元素得个数
    after_add_numbers = len(rows)

    if after_add_numbers==before_add_numbers+1:
        print("添加成功")
    else:
        print("添加失败")

猜你喜欢

转载自blog.csdn.net/sunshine543123/article/details/107011550