在Page类中,操作方法应该将返回类型作为类名称,还是使用void返回类型是一种好习惯?

问题描述 投票:0回答:1
public class HomePage 
{
    public HomePage clickAboutUs1Link() 
 {
        aboutUs1.click();
        return this;
 }
 public void clickAboutUs1Link()
 {
        aboutUs1.click();

 }
}

[我将在我的测试类中调用action方法。因此,在将页面对象模型与Selenium Webdriver一起使用时,使用>任何一个有什么优点或缺点?

selenium selenium-webdriver testng pageobjects
1个回答
0
投票

如果您有更多方法,此问题将更加清楚。考虑那些类

public class HomePage {

    public AboutUsPage clickAboutUsLinkAndGoToAboutUsPage() {
        aboutUs1.click();
        return new AboutUsPage();
    }   

    public HomePage typeToField() {
        aboutUs1.click();
        return this;
    }

    public HomePage clickOnChecbox() {
        aboutUs1.click();
        return this;
    }
}

class AboutUsPage {

    public boolean isAboutUsPageDisplayed() {
        return someElement.isDisplayed();
    }
}

现在您可以在测试中使用方法链接来创建流程

public class TestAboutUsLink {

    boolean isDisplayed =
    new HomePage()
        .typeToField()
        .clickOnChecbox()
        .clickAboutUsLinkAndGoToAboutUsPage()
        .isAboutUsPageDisplayed();

    assertTrue(isDisplayed);
}

如果每个方法都没有返回任何内容

public class TestAboutUsLink {

    HomePage homePage = new HomePage();
    homePage.typeToField();
    homePage.clickOnChecbox();
    homePage.clickAboutUsLinkAndGoToAboutUsPage()

    AboutUsPage aboutUsPage = new AboutUsPage(); 
    boolean isDisplayed = aboutUsPage.isAboutUsPageDisplayed();

    assertTrue(isDisplayed);
}
© www.soinside.com 2019 - 2024. All rights reserved.