test_add_bug.py
from selenium import webdriver
from common.base import Base
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
"""
判断添加的bug是否成功
"""
class ZenTaoBug(Base):
#定位登陆
loc_user = ("id","account")
loc_pwd = ("name","password")
loc_login = ("id", "submit")
# 添加bug
loc_test = ("xpath", "//nav[@id='mainmenu']//ul[@class='nav']//li[4]//a[1]")
loc_bug = ("link text","Bug")
loc_add_bug = ("xpath"," //a[@class='btn btn-bug-create']")
loc_truck = ("xpath","//div[@id='openedBuild_chosen']//ul[@class='chosen-choices']")
loc_truck_add = ("xpath","//li[@class='active-result highlighted']")
loc_title = ("xpath","//div[@class='input-group w-p100']//input[@id='title']")
loc_content = ("xpath"," //body[@class='article-content']")
loc_save = ("xpath"," //table[@class='table table-form']//button[@id='submit']")
# 添加bug成功之后的列表
loc_new = ("xpath","//tr[1]//td[4]//a[1]")
def login(self,user ="admin",pwd = "123456"):
self.driver.get("http://127.0.0.1:81/zentao/user-login-L3plbnRhby8=.html")
self.sendKeys(self.loc_user,user)
self.sendKeys(self.loc_pwd,pwd)
self.click(self.loc_login)
def add_bug(self,title):
self.click(self.loc_test)
self.click(self.loc_bug)
self.click(self.loc_add_bug)
self.click(self.loc_truck)
self.click(self.loc_truck_add)
self.sendKeys(self.loc_title,title)
# 可以通过下标切换
self.driver.switch_to.frame(0)
body = "[测试步骤]:" \
"[测试结果]:"
self.sendKeys(self.loc_content,body)
self.driver.switch_to.default_content()
self.click(self.loc_save)
# fail
# def if_add_bug_success(self,_text,driver):
# print(_text)
#
# result = EC.text_to_be_present_in_element(self.loc_new, _text)(driver)
#
# return result
if __name__ == "__main__":
driver = webdriver.Chrome()
bug = ZenTaoBug(driver)
bug.login()
timestr = time.strftime("%Y %m %D %H %M %S")
title = "bug_title"+timestr
bug.add_bug(title)
# result = bug.if_add_bug_success(title,driver)
# print(result)
driver.close()
driver.quit()
base.py
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import os
class Base():
def __init__(self,driver:webdriver.Chrome):
self.driver = driver
self.timeout = 10
self.t = 0.5
# 定位一个
def findElement(self,locator):
ele = WebDriverWait(self.driver, self.timeout, self.t).until(lambda x: x.find_element(*locator))
return ele
# 定位一组
def findElements(self, locator):
eles = WebDriverWait(self.driver, self.timeout, self.t).until(lambda x: x.find_elements(*locator))
return eles
def sendKeys(self,locator,text):
ele = self.findElement(locator)
ele.send_keys(text)
def click(self,locator):
ele = self.findElement(locator)
ele.click()
def clear(self,locator):
ele = self.findElement(locator)
ele.clear()
def is_Selected(self,locator):
ele = self.findElement(locator)
result = ele.is_selected()
return result
def isElementExist(self,locator):
try:
self.findElements(locator)
return True
except:
return False
def isElementExist2(self, locator):
eles = self.findElements(locator)
n = len(eles)
if n == 0 :
return True
elif n == 1:
return True
else:
return True
def is_title(self,_title):
try:
result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_is(_title))
return result
except:
return False
def id_title_contains(self,locator,_title):
try:
result = WebDriverWait(self.driver, self.timeout, self.t).until(EC.title_contains(_title))
return result
except:
return False
if __name__ == "__main__":
driver = webdriver.Chrome()
driver.get("file:///"+os.path.abspath("login.html"))
login = Base(driver)
# 通过By的方式定位
# loc1 = (By.NAME, 'user')
# loc2 = (By.NAME, 'password')
# loc3 = (By.ID, 'tijiao')
loc1 = ("name","user")
loc2 = ("name","password")
loc3 = ("id","tijiao")
login.sendKeys(loc1,"user")
login.sendKeys(loc2,"password")
login.click(loc3)
driver.close()
driver.quit()