在 Selenium 中测试 Service Worker 和缓存
Testing Service Worker and Cache in Selenium
我有一个使用 Service Worker 和缓存的应用 API。 Service Worker 中使用的缓存名称包含 GIT 修订(哈希),因此每次发布时,都会使用新缓存并删除旧缓存。这是为了 100% 确保用户将始终获得最新的资源(这种情况对于预期的发布时间表来说很好)。
我正在使用 Selenium Java (WebDriver) 每天测试 运行 几次(假设 GIT 存储库发生变化时),我想确保每个新 GIT 修订都正确擦除 worker 的缓存。
默认情况下,Selenium 为每个浏览器会话创建一个新的临时配置文件目录,这意味着测试开始时始终没有缓存。不过我想使用缓存,我的想法是(现在让我们谈谈 Firefox):
- 保留一些模型配置文件目录
- 当 Selenium 启动 Firefox 时,将模型配置文件传递给新的临时配置文件,例如
new FirefoxProfile(new File(PROFILE_MODEL))
- 这将确保在启动浏览器之前存在一些缓存
- 一旦测试完成,备份临时配置文件(因为它包含更新的缓存)并用最新的配置文件替换(现在较旧的)
PROFILE_MODEL
,以便下一次测试 运行 将使用它再次更新缓存
测试运行结束时的"model profile update step"在伪代码中看起来像这样
File modelDir = new File(PROFILE_MODEL);
// copy current model for backup
bckPath = Files.createTempDirectory("bckProfile");
Util.copy(modelDir, bckPath);
// remove the now obsolete PROFILE_MODEL
Util.deleteDirectory(modelDir);
// copy current profile to the model dir
File profileSnapshot = currentProfile.layoutOnDisk();
Util.copy(profileSnapshot.toPath(), modelPath);
问题是调用 currentProfile.layoutOnDisk();
似乎不包含任何缓存相关信息,我不知道如何获取当前使用的临时配置文件的路径(在 Linux ,通常类似于 /tmp/anonymous6209009552324183685webdriver-profile
所以基本上我的问题是,如何跨多个测试 运行s 保存和控制浏览器 (Firefox) 缓存?我知道在测试中通常需要使用新缓存启动浏览器,但我认为对于测试 Service Worker 的这种特殊情况,对其进行一些控制非常有用。
请注意,我 运行 在 CI 服务器中进行测试,因此一些手动解决方案可能非常困难(但是,嘿,至少它可以为我指明方向.. .)
更新: 我可以使用
启动 Firefox 配置文件
profile = new ProfilesIni().getProfile("test")
然后知道 "test" 模型配置文件的实际路径(模型,而不是 getProfile 调用创建的副本),我可以用最后调用 ProfilesIni().getProfile("test")
的文件替换它的所有文件测试,但这似乎不是例如复制访问页面的历史记录。
更新 2: 这是我启动浏览器的方式:
caps = DesiredCapabilities.firefox();
FirefoxProfile sampleProfile = new ProfilesIni().getProfile("test");
caps.setCapability(FirefoxDriver.PROFILE, sampleProfile);
browser = new FirefoxDriver(caps);
我可以看到它创建了 2 个配置文件临时文件夹:
- 一个是通过调用
new ProfilesIni().getProfile("test")
创建的,在测试期间不会更新 "ever"
- 第二个是从
FirefoxDriver.startClient
方法创建的,该方法调用 NewProfileExtensionConnection.start
,然后调用 FirefoxProfile.layoutOnDisk
现在在测试期间,只有第二个配置文件正在更新,我想这是有道理的。问题是如何获取根据功能创建的实际配置文件目录...
您可以覆盖 FirefoxProfile.layoutOnDisk
方法以始终对配置文件使用相同的文件夹:
public class FirefoxProfileEx extends FirefoxProfile {
File profileDir;
public FirefoxProfileEx(File profileDir){
super(profileDir);
this.profileDir = profileDir;
}
public File layoutOnDisk() {
try {
File userPrefs = new File(profileDir, "user.js");
installExtensions(profileDir);
deleteLockFiles(profileDir);
deleteExtensionsCacheIfItExists(profileDir);
updateUserPrefs(userPrefs);
return profileDir;
} catch (IOException e) {
throw new UnableToCreateProfileException(e);
}
}
}
用法:
FirefoxProfileEx profile = new FirefoxProfileEx(new File("C:\temp\profile"));
WebDriver driver= new FirefoxDriver(profile);
已知layoutOnDisk() 将用于调用当前配置文件,我们将其写入或保存到磁盘。我们需要尽可能早地使用这个保存的配置文件或目录,因为一旦执行完成,使用 layoutOnDisk() 保存的配置文件就没有用了。它们不起作用。
更多信息请见here
因此,对于 Firefox,唯一的事情就是使用 "driver.Manage().Cookies.AllCookies;" 保存所有 cookie,并在下次执行时使用这些 cookie。
我希望它更喜欢使用命名配置文件而不是从目录中获取配置文件。 Here创建个人资料的方式
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("myProfilewhichIsSaved");
WebDriver driver = new FirefoxDriver(myprofile);
我希望 IE 在启动前不清除,对您有所帮助。这是link帮助你
你也可以试试 chrome profile
谢谢,
穆拉利
我有一个使用 Service Worker 和缓存的应用 API。 Service Worker 中使用的缓存名称包含 GIT 修订(哈希),因此每次发布时,都会使用新缓存并删除旧缓存。这是为了 100% 确保用户将始终获得最新的资源(这种情况对于预期的发布时间表来说很好)。
我正在使用 Selenium Java (WebDriver) 每天测试 运行 几次(假设 GIT 存储库发生变化时),我想确保每个新 GIT 修订都正确擦除 worker 的缓存。
默认情况下,Selenium 为每个浏览器会话创建一个新的临时配置文件目录,这意味着测试开始时始终没有缓存。不过我想使用缓存,我的想法是(现在让我们谈谈 Firefox):
- 保留一些模型配置文件目录
- 当 Selenium 启动 Firefox 时,将模型配置文件传递给新的临时配置文件,例如
new FirefoxProfile(new File(PROFILE_MODEL))
- 这将确保在启动浏览器之前存在一些缓存
- 一旦测试完成,备份临时配置文件(因为它包含更新的缓存)并用最新的配置文件替换(现在较旧的)
PROFILE_MODEL
,以便下一次测试 运行 将使用它再次更新缓存
测试运行结束时的"model profile update step"在伪代码中看起来像这样
File modelDir = new File(PROFILE_MODEL);
// copy current model for backup
bckPath = Files.createTempDirectory("bckProfile");
Util.copy(modelDir, bckPath);
// remove the now obsolete PROFILE_MODEL
Util.deleteDirectory(modelDir);
// copy current profile to the model dir
File profileSnapshot = currentProfile.layoutOnDisk();
Util.copy(profileSnapshot.toPath(), modelPath);
问题是调用 currentProfile.layoutOnDisk();
似乎不包含任何缓存相关信息,我不知道如何获取当前使用的临时配置文件的路径(在 Linux ,通常类似于 /tmp/anonymous6209009552324183685webdriver-profile
所以基本上我的问题是,如何跨多个测试 运行s 保存和控制浏览器 (Firefox) 缓存?我知道在测试中通常需要使用新缓存启动浏览器,但我认为对于测试 Service Worker 的这种特殊情况,对其进行一些控制非常有用。
请注意,我 运行 在 CI 服务器中进行测试,因此一些手动解决方案可能非常困难(但是,嘿,至少它可以为我指明方向.. .)
更新: 我可以使用
启动 Firefox 配置文件profile = new ProfilesIni().getProfile("test")
然后知道 "test" 模型配置文件的实际路径(模型,而不是 getProfile 调用创建的副本),我可以用最后调用 ProfilesIni().getProfile("test")
的文件替换它的所有文件测试,但这似乎不是例如复制访问页面的历史记录。
更新 2: 这是我启动浏览器的方式:
caps = DesiredCapabilities.firefox();
FirefoxProfile sampleProfile = new ProfilesIni().getProfile("test");
caps.setCapability(FirefoxDriver.PROFILE, sampleProfile);
browser = new FirefoxDriver(caps);
我可以看到它创建了 2 个配置文件临时文件夹:
- 一个是通过调用
new ProfilesIni().getProfile("test")
创建的,在测试期间不会更新 "ever" - 第二个是从
FirefoxDriver.startClient
方法创建的,该方法调用NewProfileExtensionConnection.start
,然后调用FirefoxProfile.layoutOnDisk
现在在测试期间,只有第二个配置文件正在更新,我想这是有道理的。问题是如何获取根据功能创建的实际配置文件目录...
您可以覆盖 FirefoxProfile.layoutOnDisk
方法以始终对配置文件使用相同的文件夹:
public class FirefoxProfileEx extends FirefoxProfile {
File profileDir;
public FirefoxProfileEx(File profileDir){
super(profileDir);
this.profileDir = profileDir;
}
public File layoutOnDisk() {
try {
File userPrefs = new File(profileDir, "user.js");
installExtensions(profileDir);
deleteLockFiles(profileDir);
deleteExtensionsCacheIfItExists(profileDir);
updateUserPrefs(userPrefs);
return profileDir;
} catch (IOException e) {
throw new UnableToCreateProfileException(e);
}
}
}
用法:
FirefoxProfileEx profile = new FirefoxProfileEx(new File("C:\temp\profile"));
WebDriver driver= new FirefoxDriver(profile);
已知layoutOnDisk() 将用于调用当前配置文件,我们将其写入或保存到磁盘。我们需要尽可能早地使用这个保存的配置文件或目录,因为一旦执行完成,使用 layoutOnDisk() 保存的配置文件就没有用了。它们不起作用。
更多信息请见here
因此,对于 Firefox,唯一的事情就是使用 "driver.Manage().Cookies.AllCookies;" 保存所有 cookie,并在下次执行时使用这些 cookie。
我希望它更喜欢使用命名配置文件而不是从目录中获取配置文件。 Here创建个人资料的方式
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("myProfilewhichIsSaved");
WebDriver driver = new FirefoxDriver(myprofile);
我希望 IE 在启动前不清除,对您有所帮助。这是link帮助你
你也可以试试 chrome profile
谢谢, 穆拉利