【发布时间】:2021-10-21 11:16:49
【问题描述】:
我正在使用 Selenium / Protractor 来测试我的应用程序的 UI 和 html。我有一个注入到网站中的,我想获取这个脚本标签的内容,以便我可以将它用于一些测试。
脚本示例如下:
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "LocalBusiness",
"name": "The Best Company Name",
"image": "https://www.urltowebsite.us/WebContent/Images/Dealer/6/Logo.png?preset=Dealer.Logo",
"address": {
"@type": "PostalAddress",
"streetAddress": "19 North Suite 2 ",
"addressLocality": "Mobile",
"addressRegion": "MS",
"postalCode": "39107",
"addressCountry": "USA"
},
"geo": {
"@type": "GeoCoordinates",
"latitude": "32.351923",
"longitude": "-88.73434"
},
"openingHoursSpecification": [{
"@type": "OpeningHoursSpecification",
"dayOfWeek": [
"Monday"
],
"opens": "07:30:00",
"closes": "17:00:00"
},
{
"@type": "OpeningHoursSpecification",
"dayOfWeek": [
"Tuesday"
],
"opens": "07:30:00",
"closes": "17:00:00"
},
{
"@type": "OpeningHoursSpecification",
"dayOfWeek": [
"Wednesday"
],
"opens": "07:30:00",
"closes": "12:00:00"
}
]
}
</script>
我在下面尝试了这种方法,有时它似乎有效,但其他时候我只会得到 NULL。可能是因为时间问题?
// site.po.ts (po == page object)
export class Site {
async getLDJson() {
const ldJsonScript = await browser.element(by.xpath(
'//script[@type="application/ld+json"]'
)).getAttribute('innerHTML');
const parsedLdJson = JSON.parse(ldJsonScript);
return parsedLdJson;
}
}
我会在测试之前调用它,这样我就可以保证我有信息
// dealership.test.ts
describe('Dealership Site', () => {
const site: Site = new Site();
const cu: ContactUsPage = new ContactUsPage();
beforeAll(async () => {
await cu.goToDealership();
browser.sleep(3000);
await site.getLDJson();
});
});
//contact_us.po.ts
async goToDealership(dealerName: string = 'wilsonsaw', nextPage: string = "") {
await browser.waitForAngularEnabled(false);
var url = this.composeUrl(dealerName);
const currentUrl = await browser.getCurrentUrl();
url = `${url}${nextPage}`;
if (url !== currentUrl) {
await browser.navigate().to(url);
}
}
【问题讨论】:
标签: javascript selenium testing protractor