简体   繁体   English

如何自动测试Chrome扩展程序中的内容脚本?

[英]How do i test content-scripts in chrome extensions automatically?

I programmed a chrome extension which parses various site informations through content scripts. 我编写了一个chrome扩展程序,该扩展程序通过内容脚本解析各种站点信息。 Now every other month one of the websites i support change their html layout, move to another address etc. And so i have to update my extension. 现在每隔一个月,我支持的一个网站就会更改其html布局,移至其他地址等。因此,我必须更新我的扩展名。

With 14 supported website (growing) with 5-7 properties each its just too much to do by hand. 拥有14个受支持的网站(正在增长),具有5-7个属性,这些都是手工完成的。 Is there a way to automatically run my js functions in the context of the websites and check the result? 有没有一种方法可以在网站的上下文中自动运行我的js函数并检查结果?

I tryed it with iframes, but that didnt work cause of the same origin policy. 我尝试使用iframe,但由于相同的原产地政策而无法正常工作。

I created test extension for this. 我为此创建了测试扩展。 It navigates to the predefined list of pages and checked for expected modifications on each page (presence of specific tags). 它导航到页面的预定义列表,并检查每个页面上是否存在预期的修改(存在特定标签)。 In your case it could be data tags with parsing results. 在您的情况下,它可能是带有解析结果的数据标签。 Or you could share business logic code with your test extension and just adds additional validation code to it 或者,您可以与测试扩展共享业务逻辑代码,而只需向其添加其他验证代码

I ended up creating a test page within my extension. 我最终在扩展程序中创建了一个测试页面。 It's only accessible if you know the correct path. 只有知道正确的路径才能访问它。

This test page then opens all sites supported by my extension with chrome.tabs.create and then inserts my test code with chrome.tabs.executeScript . 这个测试页面,然后打开我的扩展与支持的所有站点chrome.tabs.create ,然后插入我的测试代码chrome.tabs.executeScript executeScript can return the test results which i then display on my test page. executeScript可以返回测试结果,然后将其显示在测试页面上。

Code: 码:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM