14 回复
zmm medium avatar
#1 - 一年前
zmm
import unittest
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException


class LoginCase(unittest.TestCase):
domain="http://xxxxxx/"
def setUp(self):
self.dr=webdriver.Firefox()
self.dr.get(self.domain + "wp-login.php")
def tearDown(self):
self.dr.quit()
def by_id(self,b_id):
return self.dr.find_element_by_id(b_id)

def test_login_fail_1(self):
''' 用户名密码为空:无提示信息'''
self.by_id("wp-submit").click()
try:
errorLogin=self.by_id("login_error").text
result=True
except NoSuchElementException:
result=False

self.assertFalse(result)
def test_login_fail_2(self):
'''正确的用户名, 密码不填:错误:密码一栏为空。'''
user="admin"
self.by_id("user_login").send_keys(user)
self.by_id("wp-submit").click()

result=self.by_id("login_error").text
self.assertEqual(result,"错误:密码一栏为空。")
def test_login_fail_3(self):
'''正确的用户名, 密码乱填 错误:为用户名admin指定的密码不正确。 忘记密码?'''
user="admin"
pwd="123asd"
self.by_id("user_login").send_keys(user)
self.by_id("user_pass").send_keys(pwd)
self.by_id("wp-submit").click()

result=self.by_id("login_error").text
exc_result="错误:为用户名admin指定的密码不正确。 忘记密码?"
self.assertEqual(result,exc_result)
def test_login_fail_4(self):
'''不正确的用户名, 密码乱填 错误:无效用户名。 忘记密码?'''
user=pwd="123asd"
self.by_id("user_login").send_keys(user)
self.by_id("user_pass").send_keys(pwd)
self.by_id("wp-submit").click()

result=self.by_id("login_error").text
exc_result="错误:无效用户名。 忘记密码?"
self.assertEqual(result,exc_result)
if __name__ == '__main__':
unittest.main()

乙醇 medium avatar
#2 - 一年前
乙醇

@zmm 很不错了,建议把登录操作封装成1个单独的方法,减少冗余代码。

路路 medium avatar
#3 - 一年前
路路
import unittest
from selenium import webdriver
from time import sleep

username = ['admin', '123', 'admin1', 'admin2']
password = ['luxiaohua', 'luxiaohua1', 'luxiaohua', 'luxiaohua1']


class LoginCase(unittest.TestCase):

def setUp(self):
print('before test')
self.dr = webdriver.Chrome()
self.dr.get('http://localhost/wordpress/wp-login.php')

def by_id(self, the_id):
return self.dr.find_element_by_id(the_id)

def by_css(self, css):
return self.dr.find_element_by_css_selector(css)

def log_in(self):
self.by_id('wp-submit').click()

def test_login_success(self):
sleep(2)
self.by_id('user_login').send_keys(username[0])
self.by_id('user_pass').send_keys(password[0])
LoginCase.log_in(self)
sleep(2)
self.assertTrue(username[0] in self.by_css('#wp-admin-bar-my-account .ab-item').text)
sleep(2)

def test_login_failure(self):
sleep(2)
self.by_id('user_login').send_keys(username[1])
self.by_id('user_pass').send_keys(password[1])
LoginCase.log_in(self)
sleep(2)
self.assertTrue(username[1] in self.by_css('#wp-admin-bar-my-account .ab-item').text)
sleep(2)

def test_login_failure2(self):
sleep(2)
self.by_id('user_login').send_keys(username[2])
self.by_id('user_pass').send_keys(password[2])
LoginCase.log_in(self)
sleep(2)
self.assertTrue(username[2] in self.by_css('#wp-admin-bar-my-account .ab-item').text)
sleep(2)

def test_login_failure3(self):
sleep(2)
self.by_id('user_login').send_keys(username[3])
self.by_id('user_pass').send_keys(password[3])
LoginCase.log_in(self)
sleep(2)
self.assertTrue(username[3] in self.by_css('#wp-admin-bar-my-account .ab-item').text)
sleep(2)

def tearDown(self):
print('after every test')
self.dr.quit()


if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(LoginCase("test_login_success"))
suite.addTest(LoginCase("test_login_failure"))
suite.addTest(LoginCase("test_login_failure2"))
suite.addTest(LoginCase("test_login_failure3"))
runner = unittest.TextTestRunner()
runner.run(suite)


qwe7023804 medium avatar
#4 - 一年前
qwe7023804

import unittest
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException


class LoginCase(unittest.TestCase):

def setUp(self): #每个用例执行之前执行
print('before test')
self.dr = webdriver.Chrome()
self.dr.get('http://127.0.0.1/wordpress/wp-login.php')


def login(self, username, password):
time.sleep(2)
self.by_id('user_login').send_keys(username)
self.by_id('user_pass').send_keys(password)
self.by_id('wp-submit').click()
time.sleep(2)
try:
greeting_link = self.by_id('login_error').text
return greeting_link
except NoSuchElementException:
greeting_link = True

def test_login_password_none(self):
'''password is None'''
username = 'admin'
password = ''
t = self.login(username, password)
self.assertIn(t, '错误:密码一栏为空。')

def test_login_password_error(self):
'''password is error'''
username = 'admin'
password = '2342342343'
t = self.login(username, password)
self.assertIn(t, '错误:为用户名admin指定的密码不正确。 忘记密码?')

def test_login_username_password_error(self):
'''username & password is error'''
username = password = 'admin3434'
t = self.login(username, password)
self.assertIn(t, '错误:无效用户名。 忘记密码?')

def test_login_username_password_none(self):
'''username & password is None'''
username = password = ''
t = self.login(username, password)
self.assertFalse(t)


def by_id(self, the_id):
return self.dr.find_element_by_id(the_id)

def tearDown(self):
print('after test')
self.dr.quit()

if __name__ == '__main__':
unittest.main()


silvia medium avatar
#5 - 一年前
silvia
import unittest
from selenium import webdriver
import time
from selenium.common.exceptions import NoSuchElementException

class LoginCase(unittest.TestCase):
def setUp(self):
self.dr=webdriver.Chrome()
self.domain = 'http://xxxxx/'
def goto(self,path):
self.dr.get(self.domain + path)
def by_id(self,this_id):
return self.dr.find_element_by_id(this_id)
def by_css(self,this_css):
return self.dr.find_element_by_css_selector(this_css)
def login(self,username,password):
self.by_id('user_login').send_keys(username)
self.by_id('user_pass').send_keys(password)
self.by_id('wp-submit').click()
time.sleep(2)
try:
greet_link=self.by_id('login_error').text
return greet_link
except NoSuchElementException:
greet_link=True
def test_login_success(self):
username=password='admin'
self.goto('wp-login.php')
self.login(username,password)
self.assertIn('wp-admin',self.dr.current_url)
greet_link=self.by_css('#wp-admin-bar-my-account .ab-item')
self.assertTrue(username in greet_link.text)

def test_login_fail_empty(self):
username = password = ''
self.goto('wp-login.php')
self.login(username, password)
self.assertIn('wp-login.php', self.dr.current_url)

def test_login_fail_passwordempty(self):
username ='admin'
password = ''
self.goto('wp-login.php')
self.login(username, password)
result=self.by_id('login_error').text
self.assertIn(result," 错误:密码一栏为空。")

def test_login_fail_wrongpassword(self):
username = 'admin'
password = '123'
self.goto('wp-login.php')
self.login(username, password)
result = self.by_id('login_error').text
self.assertIn(result,'错误:为用户名admin指定的密码不正确。 忘记密码?')

def test_login_fail_wrongusername(self):
username = '123'
password = 'admin'
self.goto('wp-login.php')
self.login(username, password)
result = self.by_id('login_error').text
self.assertEqual(result, "错误:无效用户名。 忘记密码?")
def tearDown(self):
self.dr.quit()
if __name__=='__mian__':
unittest.main()

乙醇 medium avatar
#6 - 一年前
乙醇

重构一下,不要把测试的ip或域名暴露在测试用例里。

使用命令行参数的方式,运行时传入测试的地址。参考这里

效果是python login_case.py http://localhost:8080/wordpress

乙醇 medium avatar
#7 - 一年前
乙醇

实现编辑文章的测试用例。

  • 登录
  • 创建文章
  • 编辑文章的标题
  • 编辑后的文章标题应该是我编辑的文章的标题

要求:

  • 不暴露测试服务的ip或域名
  • 动态拼接row id
乙醇 medium avatar
#8 - 一年前
乙醇

实现查看文章的测试用例

  • 登录
  • 创建文章
  • 点击查看文章
  • 断言标题的一致性
  • 断言创建者的一致性
hu medium avatar
#9 - 一年前
hu
import unittest
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from sys import argv
import uuid
from urllib.parse import urlparse


class EditCase(unittest.TestCase):
script, url, username, password = argv

def setUp(self):
self.dr = webdriver.Firefox()
def tearDown(self):
self.dr.quit()

def login_success(self):
self.goto('/wp-login.php')
self.by_id('user_login').send_keys(self.username)
self.by_id('user_pass').send_keys(self.password)
self.by_id('wp-submit').click()
self.assertTrue('wp-admin' in self.dr.current_url)
greet_link = self.by_css('#wp-admin-bar-my-account .ab-item')
self.assertTrue(self.username in greet_link.text)

def goto(self, path):
self.dr.get(self.url + path)

def by_id(self, the_id):
return self.dr.find_element_by_id(the_id)

def by_css(self, css):
return self.by_css(css)

def js(self, js_code):
return self.dr.execute_script(js_code)

def by_link(self, link):
return self.dr.find_element_by_link_text(link)

def get_random_str(self):
return str(uuid.uuid4())

def set_content(self, content):
script = 'document.getElementById("content_ifr").contentWindow.document.body.innerHTML="%s"' % (content)
self.js(script)

def get_params(sele, url):
# 解析url内容
url_arr = urlparse(url)
# 获取参数内容
query_arr = url_arr.query.split("&")
query_dict = {}
index = 0

# 封装参数列表
while index < len(query_arr):
param_entry = query_arr[index]
entry_arr = param_entry.split("=")
query_dict[entry_arr[0]] = entry_arr[1]
index += 1

return query_dict

def create_post(self, title, content):
self.goto('/wp-admin/post-new.php')
self.by_id('title').send_keys(title)
self.set_content(content)
self.by_id('publish').click()
# 获取发布成功的url中的postid
curr_url = self.dr.current_url
dir = self.get_params(curr_url)
self.post_id = dir.get('post')

# 创建文章
def create_post_success(self):
self.login_success()
self.title = self.content = self.get_random_str()
self.create_post(self.title, self.content)

self.assertTrue("/wp-admin/post.php" in self.dr.current_url)
message = self.by_id('message')
self.assertTrue(EC.visibility_of(message))
self.goto('/wp-admin/edit.php')
first_title = self.by_css('.row-title').text
self.assertEqual(first_title, self.title)

# 编辑文章
def test_edit_post_success(self):
self.create_post_success()
self.get_row_id()
ActionChains(self.dr).move_to_element(self.row).perform()
self.row.find_element_by_css_selector(".edit").click()
old_title = self.by_id("title").clear()
update_title = "更新标题"
old_title.send_keys(update_title)
self.by_id("publish").click()
self.goto('/wp-admin/edit.php')
title = self.by_id(self.row_id).find_element_by_css_selector(".row-title").text
self.assertEquals(title, update_title)

# 查看文章
def test_view_post_success(self):
self.create_post_success()
self.get_row_id()
ActionChains(self.dr).move_to_element(self.row).perform()
self.row.find_element_by_css_selector(".view").click()
entry_title = self.by_css(".entry-title").text
self.assertEqual(entry_title, self.title)
entry_content = self.by_css(".entry-content").text
self.assertEqual(entry_content, self.content)
author = self.by_css(".url").text
self.assertEqual(author, self.username)

def get_row_id(self):
self.row_id = "post-" + self.post_id
self.row = self.by_id(self.row_id)
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(EditCase('test_edit_post_success'))
suite.addTest(EditCase('test_view_post_success'))
runner = unittest.TextTestRunner()
runner.run(suite)


hatty medium avatar
#10 - 一年前
hatty

import unittest
from selenium import webdriver
from sys import argv
import uuid
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep


class PostCase(unittest.TestCase):

def setUp(self):
self.script, self.ip, self.username, self.password = argv
self.dr = webdriver.Chrome()
self.dr.get(self.ip)
self.title = self.content = str(uuid.uuid4())

def login(self):
self.goto('wp-login.php')
sleep(1)
self.by_id("user_login").send_keys(self.username)
self.by_id("user_pass").send_keys(self.password)
self.by_id("wp-submit").click()

def create_post(self):
self.goto('wp-admin/post-new.php')
self.by_id('title').send_keys(self.title)
self.set_content(self.content)
self.by_id('publish').click()

def test_edit_post(self):
self.login()
get_id = self.get_postID()
self.goto('wp-admin/edit.php')
row_id = 'post-' + get_id
post_ele = self.by_id(row_id)
ActionChains(self.dr).move_to_element(post_ele).perform()
post_ele.find_element_by_css_selector('span.edit>a').click()
self.by_id('title').send_keys("Edit")
self.by_id('publish').click()
self.goto('wp-admin/edit.php')
new_title = self.dr.find_element_by_class_name('row-title').text
self.assertEqual(self.content + "Edit",new_title)

def test_view_post(self):
self.login()
post_id = self.get_postID()
self.goto('wp-admin/edit.php')
row_id = 'post-' + post_id
post_ele = self.by_id(row_id)
ActionChains(self.dr).move_to_element(post_ele).perform()
post_ele.find_element_by_css_selector('span.view>a').click()
view_title = self.dr.find_element_by_class_name("entry-title").text
view_content = self.dr.find_element_by_class_name("entry-content").text
self.assertEqual(view_title,self.title)

def get_postID(self):
self.create_post()
return self.dr.current_url.split('=')[1].split('&')[0]

def set_content(self,text):
js = 'document.getElementById("content_ifr").contentWindow.document.body.innerHTML="%s"' %(text)
self.dr.execute_script(js)

def goto(self,path):
return self.dr.get(self.ip+ path)

def by_id(self, the_id):
return self.dr.find_element_by_id(the_id)

def by_css(self, css):
return self.dr.find_element_by_css_selector(css)


def tearDown(self):
self.dr.quit()

if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(PostCase('test_edit_post'))
suite.addTest(PostCase('test_view_post'))
runner = unittest.TextTestRunner()
runner.run(suite)

zmm medium avatar
#11 - 一年前
zmm
import sys
import uuid
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
import unittest
import time

class test(unittest.TestCase):
script,ip=sys.argv
def setUp(self):
self.dr=webdriver.Chrome()
def tearDown(self):
self.dr.quit()
def by_id(self,id):
return self.dr.find_element_by_id(id)
def by_css(self,css):
return self.dr.find_element_by_css_selector(css)
def goto(self,url):
'''访问url'''
self.dr.get(self.ip+url)
def login(self,user,pwd):
'''登陆'''
self.goto("wp-login.php")
user=self.by_id("user_login").send_keys(user)
pwd=self.by_id("user_pass").send_keys(pwd)
self.by_id("wp-submit").click()
def get_random_str(self):
return str(uuid.uuid4())
def js(self,comd):
return self.dr.execute_script(comd)
def set_conment(self,content):
js='document.getElementById("content_ifr").contentWindow.document.body.innerHTML="%s"'%(content)
self.js(js)
def by_link(self,text):
return self.dr.find_element_by_link_text(text)
def by_xpath(self,path):
return self.dr.find_element_by_xpath(path)
def creatPost(self):
'''创建文章'''
self.login("admin","admin")
self.goto("wp-admin/post-new.php")
self.title=self.get_random_str()
self.by_id("title").send_keys(self.title)
self.content=self.get_random_str()
self.set_conment(self.content)
self.by_id("publish").click()

def get_post_id(self):
return self.dr.current_url.split("=")[1].split("&")[0]
def get_tr_id(self):
tr_id="post-"+self.get_post_id()
return tr_id
def get_current_author(self):
self.now_author=self.by_xpath("//li[@id='wp-admin-bar-my-account']/a").text
return self.now_author
def test_view_post(self):
'''查看文章'''
self.creatPost()
tr_id=self.get_tr_id()

self.goto("wp-admin/edit.php")
ActionChains(self.dr).move_to_element(self.by_id(tr_id)).perform()
self.by_link("查看").click()
self.view_title=self.by_css(".entry-title").text
#断言标题
self.assertEqual(self.title,self.view_title)
#断言作者
author=self.by_css(".url.fn.n").text
self.assertTrue(author in self.get_current_author())
def test_edit_post(self):
self.creatPost()
tr_id=self.get_tr_id()
self.goto("wp-admin/edit.php")
ActionChains(self.dr).move_to_element(self.by_id(tr_id)).perform()
self.by_link("编辑").click()

self.by_id("title").clear()
edit_title = self.get_random_str()
self.by_id("title").send_keys(edit_title)
self.by_id("publish").click()
self.goto("wp-admin/edit.php")
edit_title_new=self.by_css(".row-title").text

self.assertEqual(edit_title,edit_title_new)

if __name__ =="main":
unittest.main()

乙醇 medium avatar
#13 - 一年前
乙醇

第四课作业:

  • 安装jenkins,安装完成的同学回帖既可
  • 使用webium重构创建,编辑以及删除文章的测试用例
zmm medium avatar
#14 - 一年前
zmm

已安装jenkins,搭建了一个项目

测试学习小组 — 软件测试学习讨论区
现在注册
已注册用户请 登入
内容分享
收藏此话题的成员
路路 mini avatar 乙醇 mini avatar 柚子清风还有你 mini avatar

欢迎关注我们的公众号: 测试圈TC


小贴士

你的问题在测试教程网中能找到答案