我正在使用 Playwright 编写一个测试,用于在用户 httpsession 中设置 java 应用程序属性,以在不同页面之间共享。
测试在一页中打开应用程序,向 httpssession 添加一个属性。然后它打开一个新页面,添加一个不同的属性并期望在会话中找到这两个属性。
但是 Playwright 的文档 提到每个页面在默认情况下都是隔离的,而且是按照设计的。
有没有办法绕过这种隔离?或者可以与 Playwright 一起使用的解决方法吗?
我的代码:
部署和测试的应用程序在上面链接(来自 glassfish-samples 的 ClusterHAJSP)。测试本身:
它基本上将应用程序部署在 2 个服务器中,打开 page1,添加 attribute1,打开 page2,添加 attribute2 并期望找到 page1 中添加的属性(未来的步骤是重新加载 page1 并查找 attribute2)
public class DualSessionsIT {
private static BrowserContext context;
public static WebArchive createClusterJsp() {
WebArchive archive = ShrinkWrap.getWebArchive()
.addAsWebInfResource(new File("src/test/resources/clusterjsp/clusterjsp-war/web/WEB-INF/web.xml"))
.addAsWebResource(new File("src/test/resources/clusterjsp/clusterjsp-war/web/HaJsp.jsp"))
.addAsWebResource(new File("src/test/resources/clusterjsp/clusterjsp-war/web/ClearSession.jsp"))
.addAsManifestResource("");
return archive;
}
@Deployment(name = "instance1", order = 1)
public static WebArchive instance1() {
return createClusterJsp();
}
@Deployment(name = "instance2", order = 2)
public static WebArchive instance2() {
return createClusterJsp();
}
@ArquillianResource
@OperateOnDeployment("instance1")
private URL deployment1URL;
@ArquillianResource
@OperateOnDeployment("instance2")
private URL deployment2URL;
//Load the desired page
static public Page openPage(String url) {
if (context == null) {
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
context = browser.newContext();
}
Page page = context.newPage();
page.navigate(url);
page.waitForLoadState();
assertThat(page).hasTitle("Cluster - Ha JSP Sample ");
return page;
}
@Test
public void addAndReadAttributes() throws MalformedURLException, InterruptedException {
Page page1 = openPage(deployment1URL.toString());
ClusterHAJSPPage.enterNameAttribute(page1, "attribute1");
ClusterHAJSPPage.enterValueAttribute(page1, "value1");
ClusterHAJSPPage.addSessionData(page1);
String results = ClusterHAJSPPage.readDataHttpSession(page1);
assertThat(results, containsString("attribute1 = value1"));
Page page2 = openPage(deployment2URL.toString());
page2.bringToFront();
ClusterHAJSPPage.enterNameAttribute(page2, "attribute2");
ClusterHAJSPPage.enterValueAttribute(page2, "value2");
ClusterHAJSPPage.addSessionData(page2);
String results2 = ClusterHAJSPPage.readDataHttpSession(page2);
assertThat(results2, containsString("attribute1 = value1"));
}
}
不是对我最初问题的直接回答,但我通过使用同一页面、导航到第二个网址、检查 http 会话并添加属性,然后返回到第一页并再次检查会话来进行测试。