UI自动化的设计模式:Screenplay 模式(Journey 模式)

2019-08-02 11:30:11 浏览数 (1)

Screenplay模式

Junit的Screenplay

举例

代码语言:javascript复制
Actor theReceptionist = new Actor().with(WebBrowsing.ability())

theReceptionist.attemptsTo(
    Go.to(findOwnersScreen.url()),
    Search.forOwnersWith(EMPTY_SEARCH_TERMS),
    Count.theNumberOfOwners()
);

assertThat(
    theReceptionist.sawThatThe(numberOfOwners()),
    was(TheExpectedNumberOfOwners)
);

A Task

代码语言:javascript复制
private static String searchTerms;
    @Override
    public void performAs(Actor asAReceptionist){
        asAReceptionist.attemptTo(
                Enter.the(searchTerms).into(findOwnersScreen.searchTerms),
                Click.onThe(findOwnersScreen.searchButton)
            );
        }

    public SearchForOwnersWith(String searchTerms){
        this.searchTerms = searchTerms;
    }

A Screen

代码语言:javascript复制
@ Url("owner/find.html")
    public class FindOwnerScreen extends WebScreen{

    @LocateBy(css="#search-owner-form input")
    public ScreenElement searchTerms;

    @LocateBy(css="##search-owner-form button")
    public SearchElement searchButton;

An Action

代码语言:javascript复制
public class Enter extends WebDriverInteraction implements Perform{

        private String text;
        private ScreenElement field;

        public void performAs(Actor actor){

            web(actor).findElement(field.locator()).sendKeys(text);
        }

        public Enter(String text){this.text = text;}

        public static Enter the(String text){return new Enter(text);}

        public Perform into(ScreenElement field){

            this.field = field;
            return this;
            }
        }

优越性

相比于PO模式

  • screen的类更小
  • 更精简更聚焦的Task类
  • 可读性更高
  • 继承关系简单

0 人点赞