# **測試API、前端並輸出Json** ## 取得網站log ``` from selenium.webdriver.common.desired_capabilities import DesiredCapabilities import time import json d = DesiredCapabilities.CHROME ##d['goog:loggingPrefs']>>webdriver套件 d['goog:loggingPrefs'] = { u'browser':'ALL' }##利用webdriver套件,取得所有的log資料 ``` ## 前端測試的程式碼 ``` from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.options import Options options = webdriver.ChromeOptions() options.add_argument("incognito") user = "abcde" usr_mail = "cyhan014@gmail.com" usr_password = "12345678" signup_url = "https://yillkid.github.io/ntc-python-crawler-workshop-frontent/signup.html" signin_url = "https://yillkid.github.io/ntc-python-crawler-workshop-frontent/signin.html" driver = webdriver.Chrome(options = options) driver.get(signup_url) email = driver.find_element(By.ID,'email') email.send_keys(usr_mail) username = driver.find_element(By.ID,'username') username.send_keys(user) password = driver.find_element(By.ID, 'password') password.send_keys(usr_password) password2 = driver.find_element(By.ID, 'cfm_password') password2.send_keys(usr_password) button = driver.find_element(By.TAG_NAME,'button') button.click() driver.get(signin_url) signin_email = driver.find_element(By.ID, 'email') signin_email.send_keys(usr_mail) signin_password = driver.find_element(By.ID, 'password') signin_password.send_keys(usr_password) button2 = driver.find_element(By.TAG_NAME,'button') button2.click() ``` ## 用迴圈把Json寫入檔案 ``` for log in driver.get_log('browser'): if ("JWT" in log["message"]): print(log) with open("frontend.json", "w") as fp: json.dump(log, fp, indent=4) time.sleep(1) ``` ## 後端測試的程式碼 ``` import json import requests headers = {'Accept': 'application/json'} signup_info = {'email': 'cyhan014@gmail.com', 'username': 'abcde', 'password': '12345678'} response_signup = requests.post('https://beta-eid-backend.townway.com.tw/accounts/signup', headers=headers, data=signup_info) print(response_signup.text) with open("signup.json","w")as f: json.dump(json.loads(response_signup.text),f) signin_info = {'email': 'cyhan014@gmail.com', 'password': '12345678'} response_signin = requests.post('https://beta-eid-backend.townway.com.tw/accounts/signin', headers=headers, data=signin_info) print(response_signin.text) with open("signin.json","w")as f: json.dump(json.loads(response_signin.text),f) ```