我只想测试 getRates() 函数。但是我遇到了无法修复的错误: selenium.common.exceptions.TimeoutException 或者我收到未定义 getRates 的错误消息。
function getRates(currency_name) {
if(currency_name === "pln"){ return 1; }
return fetch(`http://api.nbp.pl/api/exchangerates/rates/a/${currency_name}`)
.then((response) => response.json())
.then(json_data => {
console.log(json_data);
const value_in_pln = json_data.rates[0].mid;
return value_in_pln;
})
.catch(error => {
console.error(error);
});
}
我的测试函数:
def test_conversion_getRates(self):
self.driver.get(self.live_server_url + '/conv')
curr = 'chf'
print("1")
wait = WebDriverWait(self.driver, 10)
print("2")
wait.until(expected_conditions.presence_of_element_located((By.XPATH, '//script[@src="js/converter.js"]')))
print("3")
# time.sleep(1)
resultFunc = self.driver.execute_script(f'return getRates("{curr}");')
print("resultFunc = " + resultFunc)
response = requests.get(f'http://api.nbp.pl/api/exchangerates/rates/a/{curr}/')
data = response.json()
rates = data['rates']
mid = rates[0]['mid']
print(mid)
self.assertEqual(resultFunc, str(mid), "Comparison Done")
我也试过:
def test_conversion_getRates(self):
self.driver.get(self.live_server_url + '/conv')
curr = 'chf'
wait = WebDriverWait(self.driver, 5)
wait.until(
expected_conditions.presence_of_element_located((By.XPATH, '//script[@src="js/converter.js"]')))
wait.until(lambda driver: driver.execute_script('return typeof exampleFunc !== "undefined";'))
resultFunc = self.driver.execute_script("return exampleFunc()")
response = requests.get(f'http://api.nbp.pl/api/exchangerates/rates/a/{curr}/')
data = response.json()
rates = data['rates']
mid = rates[0]['mid']
self.assertEqual(resultFunc, mid, "Comparison Done")
基本上它在第二次打印后崩溃,以 wait.until(...) 开头的行。
当然我的 converter.js 文件在 static 和 js 文件夹中 (WebApp/static/js/converter.js)
感谢您的帮助:)