Cyn*_*nic 0 python selenium automated-tests qa
有没有一种方法可以自动检查Google Page Speed分数?
因此,我想出了如何使用文档中包含的Google Page Speed API来执行此操作。
TL:DR的解释是,您可以使用以下URL设置,在Cloud Console中启用Google Pagespeed API后替换括号中的值(如果在浏览器中,您还必须已登录Authenticated Google Cloud User)。
https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url={YOUR_SITE_URL}/&filter_third_party_resources=true&locale=en_US&screenshot=false&strategy=desktop&key={YOUR_API_KEY}
Run Code Online (Sandbox Code Playgroud)
从上面的链接可以看到,您将需要一个Google Pagespeed API密钥。这些是从头开始的安装说明。如果您的项目已经在Cloud Console上,则可以跳过前几个步骤。
拥有API密钥后,您可以替换URL中的值,它看起来应该像这样:
https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=https://www.example.com/&filter_third_party_resources=true&locale=en_US&screenshot=false&strategy=desktop&key=FaKeApIKey29nS8U22mM
Run Code Online (Sandbox Code Playgroud)
网址中的参数strategy = desktop可以更改为strategy = mobile。对于移动设备,您可以获得速度和可用性得分。JSON的开头如下所示:
{
"kind": "pagespeedonline#result",
"id": "https://www.example.com/fake/page”,
"responseCode": 200,
"title": "Example Domain",
"ruleGroups": {
"SPEED": {
"score": 100
},
"USABILITY": {
"score": 100
}
},
....continued...
Run Code Online (Sandbox Code Playgroud)
因此,我使用Python和Python单元测试将其自动化。
import requests
import json
import unittest
from globes import *
api_key = '' # Add API key. Found here: https://console.developers.google.com/apis/credentials/key/
base = 'http://example.com'
locale_code = 'en_US'
def get_insights_json(self, page_url, local, device_type, api_key, speed_or_useability, expected_score):
url = 'https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=' + page_url + '&filter_third_party_resources=true&locale=' + local + '&screenshot=false&strategy=' + device_type + '&key=' + api_key
# print "Getting :: " + url
r = requests.get(url)
return_code = r.status_code
try: self.assertEqual(return_code, 200)
except AssertionError, e: self.verificationErrors.append(str(page_url) + " did not return 200")
return_text = r.text
return_json = json.loads(return_text)
score = return_json['ruleGroups'][speed_or_useability]['score']
print 'Getting ' + speed_or_useability + ' for ' + page_url + ' and got a score of ' + str(score)
try: self.assertTrue(int(score) >= expected_score)
except AssertionError, e: self.verificationErrors.append(str(page_url) + ' expected ' + device_type + ' speed score to be greater than ' + str(expected_score) + ', instead got ' + str(score) )
class TestAllAPIs(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.maxDiff = None
def tearDown(self):
self.assertEqual([], self.verificationErrors)
def test_desktop_speed(self):
current_page = base + '' # You could add to the url to test other pages, I tend to do this is a loop using a list I set up by base url.
device_type = 'desktop'
target = 'SPEED'
get_insights_json(self, current_page, locale_code, device_type, api_key, target, 80)
def test_mobile_speed(self):
current_page = base + ''
device_type = 'mobile'
target = 'SPEED'
get_insights_json(self, current_page, locale_code, device_type, api_key, target, 80)
def test_mobile_useability(self):
current_page = base + ''
device_type = 'mobile'
target = 'USABILITY'
get_insights_json(self, current_page, locale_code, device_type, api_key, target, 80)
if __name__ == "__main__":
unittest.main()
Run Code Online (Sandbox Code Playgroud)
对我来说,有个谜是为什么浏览器需要通过Google进行身份验证才能从URL获取JSON,而Python请求却不需要。
| 归档时间: |
|
| 查看次数: |
1534 次 |
| 最近记录: |