-
学习中心
[
博客
为创始人和工程经理提供关于扩展、管理和产品开发的见解。
社区帖子
阅读编程教程,分享你的知识,并一起成为更好的开发者。
](www.codementor.io/community/t…)
热门话题
写一篇文章
注册
寻找开发者和导师社区帖子博客注册登录
[
](www.codementor.io/projects?re…)
关注
Python开发者和数据布道者
在Python中用Selenium添加Firefox扩展程序
发表于 7月26日, 2021
现代浏览器配备了各种功能,即从书签到GPS位置跟踪、开发者工具等。几乎所有的现代网络浏览器都有巨大的功能--火狐有内置的屏幕截图功能,Opera有免费的VPN,Edge有内置的EPUB扩展,支持显示电子书。虽然像火狐、Chrome等浏览器主要用于网页浏览,但事实上,火狐的附加功能(或扩展功能)为网页浏览器提供了额外的功能同样的经验法则也适用于其他浏览器,如Chrome、Edge等。
需要额外的附加组件、插件、扩展来扩展或定制浏览器的功能,提高生产力,并满足用户和开发者的特定要求。使用Selenium测试自动化的测试自动化工程师也可以像正常的最终用户一样从浏览器扩展中受益。事实上,测试人员经常需要对这些扩展本身进行自动化测试🙂。
问题是,当你使用Gecko Driver和Selenium WebDriver启动Firefox实例时,在本地机器上存在的Firefox浏览器上安装的扩展将无法使用。在Selenium Python教程的这一部分,我们将深入探讨使用Selenium WebDriver和Python在Firefox中添加扩展程序。
什么是Firefox WebExtensions API?
2015年9月,Mozilla开始对每个扩展进行签名和验证,以确保第三方扩展能够在不牺牲安全或使用户容易受到恶意软件攻击的情况下实现Firefox的定制。Mozilla废弃了XPCOM和基于XUL的附加组件,并适应了新的扩展程序接口(即WebExtensions API)。
新的扩展程序接口与Opera、Chrome和其他最新浏览器使用的模式兼容。因此,火狐浏览器的扩展开发者可以轻松地开发和管理跨浏览器的扩展。
什么是XPI?
XPI是一个跨平台的安装模块文件。它与zip文件相同,可以使用任何存档/压缩程序来访问。XPI安装文件被Firefox、SeaMonkey和Thunderbird所使用。它用于安装一个网络浏览器插件、附加组件、扩展等。
XPI插件可以通过简单地拖放到任何打开的火狐浏览器窗口来安装。Mozilla使用XPInstall来读取XPI文件中包含的安装指令,随后执行安装。
基于Chromium的浏览器,如Chrome、Opera和Edge使用CRX文件作为扩展安装程序。Safari使用SAFARIEXTZ。CRX文件不能用于在Mozilla中安装扩展。XPI不能在基于Chromium的浏览器中使用。然而,所有这些浏览器的底层API都是一样的,因此,维护扩展功能是相当容易的。
如何使用Selenium WebDriver和Python在Firefox中添加扩展?
现在我们已经介绍了火狐浏览器中扩展功能的基本原理,让我们来看看如何使用Selenium WebDriver和Python来添加火狐浏览器的扩展功能。让我们看看有哪些方法可以利用Selenium和Firefox扩展来实现Selenium网络自动化。
使用Selenium WebDriver的install_addon API,用Python添加Firefox扩展程序
Selenium WebDriver的install_addon方法需要两个输入参数--所需的Firefox扩展的路径和一个布尔值,表示扩展的安装是否是临时的(或不是)。
第一步:让我们下载目标Firefox扩展的XPI。
你可以将你的光标指向 "ADD to Firefox button",右键单击并复制XPI链接。然后,在一个新的标签页中打开它。
或者,你可以通过在Firefox以外的不同浏览器中打开扩展的登陆页面,找到专门的XPI下载链接。
为了演示,我们要下载以下Firefox扩展程序--"VPN大师"和 "Ghostery隐私广告屏蔽器"。" 如果使用Chrome浏览器打开,VPN Master的XPI下载器链接在绿色的按钮下面(在下图中可见)。
第二步:这里是webdriver的install_addon函数的实现方式,用于在Mozilla安装附加组件。
def install_addon(self, path, temporary=None):
# Usage: driver.install_addon('/path/to/firebug.xpi')
# ‘self’ refers to the “Webdriver” class
# 'path' is absolute path to the addon that will be installed
payload = {"path": path}
if temporary:
payload["temporary"] = temporary
# The function returns an identifier of the installed addon.
# This identifier can later be used to uninstall installed addon.
return self.execute("INSTALL_ADDON", payload)["value"]
现在,让我们使用这个install_addon方法来临时安装Ghostery Firefox扩展。
下面显示的是使用Selenium WebDriver和install_addon方法来安装Ghoserty插件的Python实现。前两行导入了依赖的库。我在try、except和finally python代码块中加入了足够的注释,以方便代码的演练过程。
文件名:FFExt.py
from selenium import webdriver
import time
try:
# Fire a remote Firefox instance using geckodriver.
# You need to have Geckodriver in the same directory as the automation testing script OR
# you need to add it in the "path" environment variable OR
# you need to know the full path to the geckodriver executable file and use it as:
# driver = webdriver.Firefox(executable_path=r'your\path\geckodriver.exe')
driver = webdriver.Firefox()
# path to your downloaded Firefox addon extension XPI file
extension_path = "G:\\jsalert\\vpnxpi\\ghostery_privacy_ad_blocker-8.5.5-an+fx.xpi"
# using webdriver's install_addon API to install the downloaded Firefox extension
driver.install_addon(extension_path, temporary=True)
# Opening the Firefox support page to verify that addon is installed
driver.get("about:support")
# xpath to the section on the support page that lists installed extension
addons = driver.find_element_by_xpath('//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]')
# scrolling to the section on the support page that lists installed extension
driver.execute_script("arguments[0].scrollIntoView();", addons)
# introducing program halt time to view things, ideally remove this when performing test automation in the cloud using LambdaTest
print("Success. Yayy!!")
time.sleep(20)
except Exception as E:
print(E)
finally:
# exiting the fired Mozilla Firefox selenium webdriver instance
driver.quit()
# End Of Script
{"mode":"full","isActive":false}
测试执行
通过在终端触发以下命令来运行测试。
python FFExt.py
下面是使用Selenium WebDriver的Firefox扩展实例的执行输出。
从执行快照中可以看出,Ghostery插件的图标出现在右上角。使用install_addon方法(Selenium WebDriver的)安装的Firefox扩展是默认启用的。
我们来试试这个。注释掉下面一行。
driver.install_addon(extension_path, temporary=True)
从下面的快照中可以看出,Ghostery扩展不再是已安装扩展的一部分,因为它是临时安装的。
使用PyTest添加Firefox扩展,使用Selenium和Python
PyTest是一个开源的Python测试框架,它更广泛地取代了PyUnit框架。你可以使用PyTest为基于Python的应用程序/库编写小型测试和复杂的功能测试。在Github上,PyTest获得了7.4k+颗星。你可以查看我们现有的博客,了解使用PyTest在Python中实现Selenium网络自动化的快速回顾。
文件名。FFExt.py
from selenium import webdriver
import time
import pytest
# Pytest fixture is used when before running the test you need to execute some code
@pytest.fixture
def FFExt():
driver = webdriver.Firefox()
extension_path = "G:\\jsalert\\vpnxpi\\ghostery_privacy_ad_blocker-8.5.5-an+fx.xpi"
driver.install_addon(extension_path, temporary=True)
driver.get("about:support")
addons = driver.find_element_by_xpath('//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]')
driver.execute_script("arguments[0].scrollIntoView();", addons)
time.sleep(10)
addons1 = driver.find_elements_by_xpath('//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]/following-sibling::*[1]/*[2]/*/*[1]')
# a list is created to return names of all the installed addons
addonsList = []
for addon in addons1:
addonsList.append(addon.text)
time.sleep(10)
driver.quit()
return addonsList
# This is the test function that will ASSERT whether addon is present in the list of installed add-ons returned by the FFExt function.
def test_addon(FFExt):
ghostery = "Ghostery – Privacy Ad Blocker"
assert ghostery in FFExt
{"mode":"full","isActive":false}
通过在终端上触发以下命令来运行该测试。
pytest FFExt.py
这里是执行快照,表明有一个测试是成功的。
注意:对于每个Python项目来说,pytest是可靠和可维护的测试架构的完美选择。要快速了解如何开始使用pytest,请看下面LambdaTest YouTube频道的视频。
在Selenium WebDriver中使用预装的Firefox扩展和FirefoxProfile
FirefoxProfile是在Firefox浏览器上完成的设置、扩展和定制的集合。你可以创建几个Firefox配置文件,根据你的喜好定制浏览器的外观和感觉。
要使用FirefoxProfile在Selenium中添加Firefox扩展,请按照下面提到的步骤进行。
第1步:创建一个Firefox配置文件。在这样做之前,你需要关闭所有活动的Firefox窗口。
第2步:按 "win + R "打开 "运行 "对话窗口。输入 "firefox.exe -P",然后按确定。
第3步:现在,点击 "创建配置文件 "按钮。
第4步:点击下一步。
第5步:为你的新Firefox配置文件输入一个名称。复制到目标文件夹的路径(这将是以后需要的)。点击 "完成 "来创建配置文件。
第6步:用你新创建的配置文件启动Firefox。
第7步:添加Ghostery插件到这个配置文件。
现在,让我们写一个PyTest脚本,以检查当我们使用Selenium Python在远程控制的Firefox实例中启动创建的Firefox配置文件时,是否安装了Ghostery扩展。
现有的Firefox配置文件可以在Selenium webdriver中使用加载。
webdriver.FirefoxProfile("\\path to the new Firefox profile’s folder")
文件名:FFExt_method_2.py
from selenium import webdriver
import time
import pytest
@pytest.fixture
def FFExt2():
# the value you pass to parameter should be the name of folder which represents your target Firefox profile, you created earlier
profile = webdriver.FirefoxProfile("G:\\7m1dwmtw.FFExt")
driver = webdriver.Firefox(profile)
time.sleep(5)
driver.get("about:support")
addons = driver.find_element_by_xpath('//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]')
driver.execute_script("arguments[0].scrollIntoView();", addons)
addons1 = driver.find_elements_by_xpath('//*[contains(text(),"Add-ons") and not(contains(text(),"with"))]/following-sibling::*[1]/*[2]/*/*[1]')
addonsList = []
for addon in addons1:
addonsList.append(addon.text)
time.sleep(10)
driver.quit()
return addonsList
# this is the test function that will assert whether addon is present in the list of installed addons returned by FFExt function.
def test_addon(FFExt2):
ghostery = "Ghostery – Privacy Ad Blocker"
assert ghostery in FFExt2
{"mode":"full","isActive":false}
在终端上运行以下命令来触发测试。
pytest FFExt_method_2.py
下面是执行快照。
结论。
扩展,尤其是VPN扩展,通常需要测试你的应用程序在不同地域的表现。另外,如果你在你的网络应用程序中实施了广告屏蔽器检测技术,那么你需要安装广告屏蔽器扩展来检查你的广告屏蔽器检测技术是否有效。
在Selenium Python教程的这一部分,我们看了在Selenium控制的Firefox实例中安装插件的两种不同机制。谈到扩展程序,我们希望你已经安装了LambdaTest chrome扩展程序,以便在路上测试应用程序。LambdaTest是一个出色的工具,可以满足你在云端的所有Selenium网络自动化要求。请查看LT浏览器,在50多个移动屏幕上测试应用程序。
测试愉快!
报告
喜欢这篇文章吗?如果有帮助,请给Nishant Choudhary一个赞。
3
分享
Python开发人员和数据布道者
Nishant是一名Web Scraping Python开发者和数据布道者,他也喜欢通过撰写技术内容来宣传初创企业和技术。
关注
发现并阅读Nishant Choudhary的更多文章
开始
喜欢这个帖子吗?
为Nishant留下喜欢和评论
3
成为第一个分享您意见的人
提交
显示更多回复