|
@@ -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
|