ssgdfs.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. import requests
  4. from selenium import webdriver
  5. from time import sleep
  6. from selenium.webdriver.common.keys import Keys
  7. from selenium.webdriver.support.wait import WebDriverWait
  8. from selenium.webdriver.support import expected_conditions as EC
  9. from selenium.webdriver.common.by import By
  10. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  11. class Ssgdfs(object):
  12. def setUp(self):
  13. #self.driver = webdriver.Chrome('E:\chromedriver_win32/chromedriver')
  14. self.driver = webdriver.Remote(command_executor='http://192.168.15.10:8910/', desired_capabilities=DesiredCapabilities.PHANTOMJS)
  15. self.config = {}
  16. self.config['id'] = 1
  17. self.config['login'] = 'http://cn.ssgdfs.com/shop/login/loginPopupForm';
  18. self.config['username'] = 'weiweizhou'
  19. self.config['password'] = 'P4220887'
  20. self.config['product'] = 'http://cn.ssgdfs.com/shop/product/productDetail?recopick=25&prdtCode=79519000009';
  21. self.config['chuguo'] = '3239636'
  22. def test_start(self):
  23. # 登录
  24. self.login()
  25. # 下单
  26. self.buy()
  27. # 支付
  28. self.pay()
  29. # 支付截图
  30. self.crop()
  31. def check():
  32. r = requests.get(self.config['product'])
  33. # 缺货标识
  34. string = 'http://image2.ssgdfs.com/images/shop/cn/renewal/content/btn_soldout.gif'
  35. data = r.text
  36. if string in data:
  37. return False
  38. else:
  39. return True
  40. def login(self):
  41. self.driver.get(self.config['login'])
  42. self.driver.find_element_by_id('login-id').send_keys(self.config['username'])
  43. self.driver.find_element_by_id('login-password').send_keys(self.config['password'])
  44. self.driver.find_element_by_xpath('//input[@alt="login"]').click()
  45. self.driver.implicitly_wait(5)
  46. def buy(self):
  47. self.driver.get(self.config['product'])
  48. # 立刻购买
  49. self.driver.execute_script('directOrderProduct()')
  50. # 等待加载页面完成
  51. self.wait('long-sub')
  52. # 等待弹层关闭
  53. self.waitNot('pay_popup')
  54. # 输入下单信息
  55. self.buy_content()
  56. # 确认订单
  57. self.driver.execute_script('goPaymentCheck()')
  58. # 等待加载页面完成
  59. self.wait('order-agree2')
  60. def buy_content(self):
  61. # 选择护照信息
  62. self.driver.execute_script('setExitInfoHistory("' + self.config['chuguo'] + '")')
  63. # 使用优惠券
  64. # 使用积分
  65. self.driver.find_element_by_xpath('//input[@name="nowUse" and @value="0"]').click()
  66. def pay(self):
  67. # 等待弹层关闭
  68. self.waitNot('loding_popup')
  69. # 输入支付信息
  70. self.pay_content()
  71. # 确认按钮
  72. self.driver.execute_script('window.confirm = function(msg) { return true; }');
  73. # 同意付款
  74. self.driver.execute_script('$("a.goPayment").click()')
  75. #self.driver.switch_to_alert().accept()
  76. #self.wait('qrcode-img-wrapper')
  77. self.wait('qrcode')
  78. sleep(2)
  79. link = self.driver.execute_script('return location.href')
  80. print link
  81. def pay_content(self):
  82. # 确认取货处
  83. self.driver.find_element_by_id('exitDestInfo').click()
  84. # 同意收集个人信息
  85. self.driver.find_element_by_id('agree').click()
  86. # 同意订购
  87. self.driver.find_element_by_id('agree01').click()
  88. # 选择支付方式:支付宝21 微信00
  89. self.driver.find_element_by_xpath('//input[@id="pymntMeansCode" and @value="00"]').click()
  90. def crop(self):
  91. pic_path = 'test.png'
  92. self.driver.save_screenshot(pic_path)
  93. def waitNot(self, name):
  94. WebDriverWait(self.driver, 100).until_not(lambda x: x.find_element_by_class_name(name).is_displayed())
  95. def wait(self, name):
  96. WebDriverWait(self.driver, 100).until(lambda x: x.find_element_by_class_name(name).is_displayed())
  97. def tearDown(self):
  98. self.driver.close()
  99. #pass
  100. if __name__ == "__main__":
  101. unittest.main()