Bladeren bron

提交demo例子

tangs 7 jaren geleden
bovenliggende
commit
c67016f8be

+ 0 - 0
ctrip/__init__.py


BIN
ctrip/__pycache__/__init__.cpython-36.pyc


BIN
ctrip/__pycache__/ctrip.cpython-36.pyc


+ 89 - 0
ctrip/ctrip.py

@@ -0,0 +1,89 @@
+from selenium import webdriver
+from error import error
+import logging
+import time
+from selenium.common import exceptions
+
+
+class Ctrip:
+    def __init__(self):
+        self.index = "http://www.ctrip.com/"  # 初始化携程的首页地址
+        self.driver = webdriver.Chrome()  # 浏览器对象,之后所有自动化测试操作都会用到该对象
+
+    # 测试机票
+    def air_ticket(self):
+        self.close_advert()
+
+        try:
+            nav_flight = self.driver.find_element_by_id("nav_flight")
+        except (exceptions.NoSuchElementException, exceptions.WebDriverException) as e:
+            return error.new("air_ticket find element with id(%s) error: %s", "nav_flight", e.msg)
+
+        nav_flight.click()
+        time.sleep(5)
+        try:
+            search_btn = self.driver.find_element_by_id("search_btn")
+        except (exceptions.NoSuchElementException, exceptions.WebDriverException) as e:
+            return error.new("air_ticket find element with id(%s) error: %s", "search_btn", e.msg)
+
+        search_btn.click()
+        time.sleep(5)
+
+        return None
+
+    # 测试火车票
+    def railway_ticket(self):
+        self.close_advert()
+
+        try:
+            nav_trains = self.driver.find_element_by_id("nav_trains")
+        except (exceptions.NoSuchElementException, exceptions.WebDriverException) as e:
+            return error.new("railway_ticket find element with id(%s) error: %s", "nav_trains", e.msg)
+        nav_trains.click()
+        time.sleep(5)
+
+        try:
+            searchbtn = self.driver.find_element_by_id("searchbtn")
+        except (exceptions.NoSuchElementException, exceptions.WebDriverException) as e:
+            return error.new("air_ticket find element with id(%s) error: %s", "searchbtn", e.msg)
+
+    # 关闭携程打开时底下的广告
+    def close_advert(self):
+        time.sleep(5)
+        try:
+            appd_wrap_close = self.driver.find_element_by_id("appd_wrap_close")
+        except (exceptions.NoSuchElementException, exceptions.WebDriverException) as e:
+            logging.debug("close_advert close advert with id(%s) not found", "appd_wrap_close")
+
+        try:
+            appd_wrap_close.click()
+        except exceptions.ElementNotVisibleException:
+            logging.error("close_advert find advert success and it maybe already closed")
+
+    # 由于测试会使用浏览器或则占用其他资源,可在close中释放所有占用的资源
+    def close(self):
+        if self.driver is not None:
+            self.driver.close()
+
+    # 开始测试
+    # 该方法有一个返回值,即测试的错误信息,如果返回值为None,则测试成功
+    def run(self):
+
+        if len(self.index) < 1:
+            return error.new("Ctrip auto test error with empty index.")
+
+        self.driver.get(self.index)
+
+        self.close_advert()
+
+        err = self.air_ticket()
+        if err is not None:
+            logging.error("Ctrip test air_ticket with index(%s) error: %s", self.index, err.to_string())
+            return err
+
+        err = self.railway_ticket()
+        if err is not None:
+            logging.error("Ctrip test railway_ticket with index(%s) error: %s", self.index, err.to_string())
+            return err
+
+        return None

+ 0 - 0
error/__init__.py


BIN
error/__pycache__/__init__.cpython-36.pyc


BIN
error/__pycache__/error.cpython-36.pyc


+ 17 - 0
error/error.py

@@ -0,0 +1,17 @@
+# 简单的定义一个错误对象,用于返回错误信息时对于错误信息的封装
+class Error:
+    def __init__(self):
+        self.message = ""
+
+    def new(self, message):
+        self.message = message
+
+    def to_string(self):
+        return self.message
+
+
+# 返回一个错误对象
+def new(message):
+    err = Error()
+    err.new(message)
+    return err

+ 29 - 0
main.py

@@ -0,0 +1,29 @@
+import logging
+from ctrip import ctrip
+import sys
+
+# 该测试用例只是简单的简单的说明自动化测试如果编写,该脚本模拟用户在携程首页上点击机票和火车票两个按钮.
+# 该用例环境
+# python3.x Chrome浏览器
+# 如有疑问,请及时联系
+if "__main__" == __name__:
+    print("auto test start ...")
+
+    # 初始化日志配置
+    LOG_FORMAT = "%(filename)s [%(levelname)s]: %(message)s"
+    DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
+    logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(filename)s line:%(lineno)d [%(levelname)s] %(message)s',
+                        datefmt='%Y/%m/%d %H:%M:%S',)
+    logging.root.level = logging.INFO
+
+    # 初始化好日志等其他依赖的配置之后,启动测试,如果成功则释放所有占用资源,即调用close(),若失败则停留在错误页面
+    ctrip_tester = ctrip.Ctrip()
+    err = ctrip_tester.run()
+    if err is not None:
+        logging.debug("auto test some error happend %s", err.to_string())
+        sys.exit(1)
+
+    ctrip_tester.close()
+    logging.debug("auto test success")
+
+    print("auto test stop")