Browse Source

modify 添加携程的登录功能

tangs 2 years ago
parent
commit
1402c2dd3e
2 changed files with 103 additions and 15 deletions
  1. 94 8
      ctrip/ctrip.py
  2. 9 7
      main.py

+ 94 - 8
ctrip/ctrip.py

@@ -1,8 +1,11 @@
+# -*- coding: utf-8 -*-
+
 from selenium import webdriver
 from selenium import webdriver
 from error import error
 from error import error
 import logging
 import logging
 import time
 import time
 from selenium.common import exceptions
 from selenium.common import exceptions
+from selenium.webdriver.common.by import By
 
 
 
 
 class Ctrip:
 class Ctrip:
@@ -10,6 +13,83 @@ class Ctrip:
         self.index = "http://www.ctrip.com/"  # 初始化携程的首页地址
         self.index = "http://www.ctrip.com/"  # 初始化携程的首页地址
         self.driver = webdriver.Chrome()  # 浏览器对象,之后所有自动化测试操作都会用到该对象
         self.driver = webdriver.Chrome()  # 浏览器对象,之后所有自动化测试操作都会用到该对象
 
 
+    def login(self):
+        # 进入登录页
+        print('----------------进入登录页')
+        time.sleep(2)
+        login_btn = self.driver.find_element(By.XPATH, '//*[@id="hp_nfes_accountbar"]/li[1]/div/button/div')
+        if not login_btn:
+            err = '找不到登录按钮'
+            logging.error(err)
+            return err
+        login_btn.click()
+
+        time.sleep(2)
+        code_btns = self.driver.find_elements(By.CLASS_NAME, 'login-entry-dynamic')
+        if len(code_btns) > 0:
+            code_btn = code_btns[0]
+        else:
+            err = '找不到切换手机验证码按钮'
+            logging.error(err)
+            return err
+
+        # 切换到手机号登录
+        time.sleep(2)
+        code_btn.click()
+
+        # 输入账号和发送验证码
+        dyphone = self.driver.find_element(By.ID, 'dyphone')
+        if not dyphone:
+            err = '找不到电话输入框'
+            logging.error(err)
+            return err
+
+        time.sleep(2)
+        dyphone.send_keys('15626215750')
+
+        # 发送验证码按钮
+        sdy = self.driver.find_element(By.ID, 'sdy')
+        if not sdy:
+            err = '找不到发送验证码按钮'
+            logging.error(err)
+            return err
+
+        time.sleep(2)
+        sdy.click()
+
+        # 关闭滑动验证码
+        time.sleep(2)
+        slider = self.driver.find_element(By.XPATH, '//*[@id="sliderdddynamic"]/div/div[1]/div[2]/div')
+        if not slider:
+            err = '找不到验证码关闭按钮'
+            logging.error(err)
+            return err
+        slider.click()
+
+        # 切换到账号密码登录
+        time.sleep(3)
+        go_prev_step = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[2]/div[2]/div/div[1]/div[3]/form/a')
+        if not go_prev_step:
+            err = '找不到返回账号登录按钮'
+            logging.error(err)
+            return err
+
+        time.sleep(2)
+        go_prev_step.click()
+
+        # 输入账号密码
+        time.sleep(2)
+        nloginname = self.driver.find_element(By.ID, 'nloginname')
+        npwd = self.driver.find_element(By.ID, 'npwd')
+
+        nloginname.send_keys('15626215750')
+        time.sleep(1)
+        npwd.send_keys('SFPbP3iW')
+        time.sleep(2)
+
+        nsubmit = self.driver.find_element(By.ID, 'nsubmit')
+        nsubmit.click()
+
     # 测试机票
     # 测试机票
     def air_ticket(self):
     def air_ticket(self):
         self.close_advert()
         self.close_advert()
@@ -74,16 +154,22 @@ class Ctrip:
 
 
         self.driver.get(self.index)
         self.driver.get(self.index)
 
 
-        self.close_advert()
+        # self.close_advert()
+        print('----------------打开了页面')
 
 
-        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())
+        err = self.login()
+        if err:
+            logging.error('Ctrip login err', err)
             return err
             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
+        # 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
         return None

+ 9 - 7
main.py

@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
 import logging
 import logging
 from ctrip import ctrip
 from ctrip import ctrip
 import sys
 import sys
@@ -9,18 +11,18 @@ import sys
 if "__main__" == __name__:
 if "__main__" == __name__:
     print("auto test start ...")
     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
+    # # 初始化日志配置
+    # 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(),若失败则停留在错误页面
     # 初始化好日志等其他依赖的配置之后,启动测试,如果成功则释放所有占用资源,即调用close(),若失败则停留在错误页面
     ctrip_tester = ctrip.Ctrip()
     ctrip_tester = ctrip.Ctrip()
     err = ctrip_tester.run()
     err = ctrip_tester.run()
     if err is not None:
     if err is not None:
-        logging.debug("auto test some error happend %s", err.to_string())
+        # logging.debug("auto test some error happend %s", err.to_string())
         sys.exit(1)
         sys.exit(1)
 
 
     ctrip_tester.close()
     ctrip_tester.close()