本文解释的利用selenium测试超盟BI的核心代码.
最核心的一步在于
给driver实例化
然后获取元素的xpath,制定相应操作.
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
import unittest
class Singleton(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_inst'):
cls._inst=super(Singleton,cls).__new__(cls, *args, **kwargs)
return cls._inst
class StoreAbc(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""
测试固件的setUp()的代码,主要是测试的前提准备工作
:return:
"""
cls.driver = webdriver.Firefox()
cls.driver.maximize_window()
base_url = "http://www.chaomengbi.com/login"
cls.driver.get(base_url)
user_name = cls.driver.find_element_by_class_name('username')
pwd = cls.driver.find_element_by_name('password')
user_name.send_keys('xiaochen')
pwd.send_keys('chaomeng')
sub_btn = cls.driver.find_element_by_class_name('login_entry_all_v2')
sub_btn.click()
time.sleep(3)
def test_fast_query(self):
driver = self.driver
queres = driver.find_elements_by_xpath("//div[@class='shortcut-list']/span[@class='el-tag el-tag--info']")
for query in queres:
if query.text:
query.click()
time.sleep(2)
def test_chart_wrap(self):
driver = self.driver
buttons = driver.find_elements_by_xpath("//div[@class='store-module-warp']/div[@class='chart-wrap']/div[@class='button-group']/button")
for btn in buttons:
btn.click()
time.sleep(1)
is_active = 'active' in btn.get_attribute('class')
self.assertTrue(is_active)
def test_category_detail(self):
driver = self.driver
buttons = driver.find_elements_by_xpath("//div[@id='pane-Store']/div[@class='store']/div[@class='el-card cat-table']/div[@class='el-card__body']/div[@class='toolbar el-row is-justify-space-between el-row--flex']/div[@class='btn-box el-col el-col-12']/button")
for btn in buttons:
btn.click()
time.sleep(1)
is_active = 'active' in btn.get_attribute('class')
self.assertTrue(is_active)
def test_simple_store_category(self):
driver = self.driver
try:
areas_select = driver.find_element_by_xpath("//div[@class='el-select store-selection']/div[@class='el-input el-input--suffix']/span/span")
areas_select.click()
except NoSuchElementException as e:
self.assertTrue(False)
time.sleep(1.5)
search_result = driver.find_elements_by_xpath("//div[@class='el-select-dropdown el-popper is-multiple']/div[@class='el-scrollbar']/div[@class='el-select-dropdown__wrap el-scrollbar__wrap']/ul/li[@class='el-select-dropdown__item']")
self.assertNotEqual(0, len(search_result))
search_result[0].click()
time.sleep(2)
areas_select.click()
time.sleep(2)
search_btns = driver.find_elements_by_xpath("//button[@class='el-button el-button--danger el-button--medium']/span")
for btn in search_btns:
if btn.tag_name == 'span' and btn.text == u'查询':
btn.click()
break
time.sleep(3)
buttons = driver.find_elements_by_xpath("//div[@id='pane-Store']/div[@class='store']/div[@class='el-card cat-table']/div[@class='el-card__body']/div[@class='toolbar el-row is-justify-space-between el-row--flex']/div[@class='btn-box el-col el-col-12']/button")
for btn in buttons:
btn.click()
time.sleep(2)
is_active = 'active' in btn.get_attribute('class')
self.assertTrue(is_active)
if btn.find_element_by_xpath("span").text == u'对比全店':
imgs = driver.find_elements_by_xpath("//img[@src='data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IEFkb2JlIElsbHVzdHJhdG9yIDIxLjAuMiwgU1ZHIEV4cG9ydCBQbHVnLUluIC4gU1ZHIFZlcnNpb246IDYuMDAgQnVpbGQgMCkgIC0tPgo8c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkxheWVyXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IgoJIHZpZXdCb3g9IjAgMCA1MTIgNTEyIiBzdHlsZT0iZW5hYmxlLWJhY2tncm91bmQ6bmV3IDAgMCA1MTIgNTEyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+CjxzdHlsZSB0eXBlPSJ0ZXh0L2NzcyI+Cgkuc3Qwe2ZpbGw6I0UwNjg2Nzt9Cjwvc3R5bGU+CjxjaXJjbGUgY2xhc3M9InN0MCIgY3g9IjI1NiIgY3k9IjI1NyIgcj0iMjUwLjUiLz4KPC9zdmc+Cg==']")
self.assertLessEqual(30, len(imgs))
@classmethod
def tearDownClass(cls):
"""
测试结束后的操作,这里基本上都是关闭浏览器
:return:
"""
cls.driver.quit()
if name == 'main':
unittest.main()