剧本-如何在多方法任务中切换窗口?

时间:2019-01-25 13:23:34

标签: serenity-bdd

我正在从Slideshare tutorial扩展 在幻灯片164上,我们将一堆代码行替换为:

connie.attemptsTo(
            PlanAjourney.from("Waterloo").to("CanaryWharf").departingAt("09:00")
        );

仅具有第一个方法的PlanAJourney类应类似于:

public class PlanAjourney
{
    public static PlanAjourney from(String departure)
    {
        return Task.where("{0} chooses #departure as a departure station",
            Enter.theValue(departure).into(JourneyPlanner.DEPARTURE_STATION)
        )
    }
}

现在想象这不是一个单页面应用程序,而是一个多窗口应用程序。想象一下,康妮需要切换到另一个窗口来查看JourneyPlanner.DEPARTURE_STATION。

现在,我希望使actor能够获得当前的窗口句柄。就像这样:

import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Task;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;
import net.serenitybdd.screenplay.actions.Click;
import net.serenitybdd.screenplay.actions.Enter;
import net.serenitybdd.screenplay.actions.Switch;

    public class PlanAjourney
    {

        public static PlanAjourney from(String departure)
        {
            String currentWindowHandle = BrowseTheWeb.as(actor).getWindowHandle();
            return Task.where("{0} chooses #departure as a departure station",
                Click.on(HomePage.JOURNEY_PLANNER_LINK),
                Switch.to(getNewWindowHandle(actor, currentWindowHandle)),
                Enter.theValue(departure).into(JourneyPlanner.DEPARTURE_STATION)
            )
        }

        private static String getNewWindowHandle(Actor actor, String currentWindowHandle)
        {
            for (String windowHandle : BrowseTheWeb.as(actor).getDriver().getWindowHandles())
            {
                if (!windowHandle.equals(currentWindowHandle))
                {
                    return windowHandle;
                }
            }
            return null;
        }
    }

但是当然不会编译,因为不知道actor。

我怎么找演员?还是有另一种方法?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。 首先,我们在StepDefinition中有一个方法调用:

public class PlanAJourneyStepDefinitions {
    @When("^(.*) plans a journey from (.*) to (.*) departing at (.*)$")
    public void plan_a_journey(String name, String departure,
        String destination, String departureTime) throws Throwable {
        Actor theUser = theActorCalled(name);
        theUser.attemptsTo(
            PlanAjourney.from(departure, theUser)
                        .to(destination)
                        .departingAt(departureTime)
        );
    }  
}

我们隔离了用户,以便能够将他/她传递给PlanAjourney类的from方法。

然后轻松应对窗口开关:

public class PlanAjourney {

    public static PlanAjourney from(String departure, Actor theUser) {
        String currentHandle = BrowseTheWeb.as(theUser).getDriver().getWindowHandle();
        return Task.where("{0} chooses #departure as a departure station",
            Click.on(HomePage.JOURNEY_PLANNER_LINK),
            Switch.toWindow(getNewWindowHandle(theUser, currentHandle)),
            Enter.theValue(departure).into(JourneyPlanner.DEPARTURE_STATION)
        );
    }

    private static String getNewWindowHandle(Actor actor, String currentWindowHandle){
        for (String windowHandle : BrowseTheWeb.as(actor).getDriver().getWindowHandles()) {
            if (!windowHandle.equals(currentWindowHandle)) {
                return windowHandle;
            }
        }
        return null;
    }
}

但是我仍然没有得到的是如何使“ from”方法返回PlanAJourney类的实例。但这是一个不同的问题,所以我会问另一个问题。