junit 用于PowerApps测试的Playwright Java

ztyzrc3y  于 2023-02-12  发布在  Java
关注(0)|答案(1)|浏览(110)

我想重用一个使用Playwright和Java的页面,这样我可以加快测试的运行速度。我使用了一个持久上下文,因为项目组织页面和MFA上的登录要求。我尝试实现类似于https://playwright.dev/java/docs/test-runners的东西,但是我得到了一个NullPointerException。
登录类:

public class PersistentLogin {

    private static String userDataDirPath = "/Users/user/powerAppsTesting/userData";

    public static Page persistentLogin(Boolean headlessState){
        Playwright playwright = Playwright.create();
        BrowserType chromium = playwright.chromium();
        Path userDataDir = Paths.get(userDataDirPath);
        BrowserContext context = chromium.launchPersistentContext(userDataDir
                , new BrowserType.LaunchPersistentContextOptions().setHeadless(headlessState));
        Page page = context.newPage();
        return page;
    };
}

测试类别:

public class TestsExample {

    private String goToUrl = "www.example.com";    
    Page page;

    @BeforeAll //also tried @BeforeEach but with the same result
    void createPage(){
        page = persistentLogin(true);
    }

    @Test
    public void saveHolidayPeriodFromAdminScreenTest(){

        //Page page = persistentLogin(true);
        page.navigate(goToUrl);

        page.frameLocator("#fullscreen-app-host").getByRole(AriaRole.BUTTON, new FrameLocator.GetByRoleOptions().setName("Run as Admin")).click();
        assertThat(page.frameLocator("#fullscreen-app-host").getByText("Preferences - New Summer Leave 2023")).isVisible();

        page.close();
    }

输出:

java.lang.NullPointerException
    at org.PilotAnnualLeaveRequests.PilotsALUsingSamePageTests.saveHolidayPeriodFromAdminScreenTest(PilotsALUsingSamePageTests.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:42)
    at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)
    at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:72)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:147)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:127)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:90)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:55)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:102)
    at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:54)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86)
    at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86)
    at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

我不知道我错过了什么。谢谢!

dzhpxtsq

dzhpxtsq1#

页面在使用前未初始化,仅在@BeforeEach中初始化。

Page page1 = persistentLogin(true);

解决了这个问题。

相关问题