gpt4 book ai didi

java - 如何使用页面对象模型处理页面导航

转载 作者:行者123 更新时间:2023-12-04 15:49:45 31 4
gpt4 key购买 nike

我正在尝试将页面对象模型用于我的测试,并且我正在尝试构建我的页面类以能够执行“类似构建器模式”的结构(我不经常看到它,所以我没有知道它是否有一个名字,或者它是否甚至是一个东西)就像在这个例子中一样:

public class Page1 implements Page  {
public static Page1 goOn(){
return new Page1();
}

public Page1 action1() {
return this;
}

public Page2 gotoPage2() {
return new Page2();
}
}

public class Page2 implements Page {
public Page2 action2() {
return this;
}

public Page2 gotoPage1() {
return new Page2();
}
}

然后像这样使用它:
Page1.goOn()
.action1()
.gotoPage2() //it return a Page2 object which mean you cant use Page1 methods anymore
.action2() // avoiding using the Page1 object which wouldnt be usable anymore if we stored it
.goToPage1() //and poof we can only use Page1 methods again
.action1()
//etc...

所以这就是我管理“线性”页面更改的方式,它允许自动完成一些不错的东西,并强制您只在编译时间之前执行有效操作(我认为这很棒,因为我讨厌运行时错误 >:()

然后我试图处理一个登录页面(它主要是我接下来要做的事情的一个简单例子),根据登录是否失败,可能有 2 种不同的输出。对于那个特定的情况,我可以完成 2 个函数: public Page1 logIn(){...}public Page2 "logInButFail(){...}但出于几个原因,我想做一些类似的事情:
public Page logIn(User user) {
//do the login in the page
//IF login worked:
//return new Page2();
//ELSE
//return this; // or new Page1(),
}

问题是,在我必须进行不安全的强制转换之前,要继续使用我所拥有的方法链,这......我可以相当好和安全地处理它们,但这意味着打破java关于打字的神圣规则......好吧,我的静态打字迷的小脑袋一直在努力决定要做什么。

所以:
  • 有没有办法在不进行不安全转换的情况下做到这一点?
  • 如果没有,我应该:
  • 使用不安全的强制转换(实际上是可以的,因为如果这些强制转换失败,则意味着测试之前失败)是否可以执行高效的代码?
  • 编写效率较低的代码但让 Java 开心? :)

  • 感谢您对这个主题的任何帮助,如果您需要更多解释,请询问我,我会尽力更准确地解释它(如果您知道这种做法如何被称为我很感兴趣):)

    编辑:使用动态后 logIn()我使用以下函数来断言页面的类型(这是我必须尝试避免的不安全转换的地方):
    public <T extends Page> T assertPage(Class<T> type){
    boolean isgood = (this.getClass.equals(type));
    assertTrue(isgood);
    if(isgood){
    return (T)this; //this is the unsafe cast im trying to avoid
    }else{
    //throw an exception as this is not supposed to happen
    }
    }

    最佳答案

    所以,你想要页面对象方法,它会有条件地返回 Page1 或 Page2 的实例。为此,您创建了 Page 类型的方法。并且您想执行此方法并继续使用 Page1 或 Page2 的实例取决于没有强制转换的条件。对?
    您可以通过泛型来实现。这是简化的示例:

    class Page{
    <T extends Page> T conditionalAction( T pageObject ){
    return pageObject;
    }
    }
    class Page1 extends Page{
    Page1 action1(){ return this; }
    }
    class Page2 extends Page{
    Page2 action2(){ return this; }
    }

    现在,您可以执行以下操作:
    Page1 page1 = new Page1();
    Page2 page2 = new Page2();
    page1.action1()
    .conditionalAction( page1 )
    .action1()
    .conditionalAction( page2 )
    .action2();

    您可以在父/clild 类中的任何位置自由使用这种泛型返回类型的方法。

    如果您只想断言从方法中获得的类,您可以进行检查:
    public Page conditionalAction( boolean condition ) {
    if ( condition ) {
    return new Page1();
    } else {
    return new Page2();
    }
    }

    //测试中
    Page result = new Page1().conditionalAction( true );
    if ( result instanceof Page1 ){
    Page1 page1 = (Page1) result;
    } else if ( result instanceof Page2 ){
    Page2 page2 = (Page2) result;
    }

    关于java - 如何使用页面对象模型处理页面导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58996682/

    31 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com