Java - Selenium > 我想为每个页面类创建一次实例

问题描述 投票:0回答:1

为了避免重复我的代码而不是在每次测试中都插入这些代码行,我想知道是否有一种有效/高效的方法,我将在类/页面中只使用一次。我想一次初始化对象。感谢您的协助!

这是我的代码:

public class ContributionTests extends BaseTest {
    @Test (priority = 1)
    @Severity(SeverityLevel.CRITICAL)
    @Description("Create a Contribution E2E, using only required fields, and validate the new creation contribution name")
    public void tc1_CreateContribution() throws InterruptedException {
        MainPage mainPageGP = new MainPage(driver);
        AssetTabPage assetTabPage = new AssetTabPage(driver);
        UnderManagementAssetPage umap = new UnderManagementAssetPage(driver);
        ContributionTabPage ctp = new ContributionTabPage(driver);
        NewContributionFormPage ncp = new NewContributionFormPage(driver);
        NewCreatedContributionPage nccp = new NewCreatedContributionPage(driver);
        ExistingContributionPage ecp = new ExistingContributionPage(driver);

        mainPageGP.setMainTab("Assets");
        assetTabPage.selectUnderManagementTab();
        getSleep(500);
        assetTabPage.selectAsset("new Asset- Test on Edge");
        getSleep(500);
        umap.selectInnerTab("Contributions");
}

我还没有尝试任何东西,但希望在我的测试类中节省很多代码行。

此时,我在每个测试中插入这些初始化器:

        MainPage mainPageGP = new MainPage(driver);
        AssetTabPage assetTabPage = new AssetTabPage(driver);
        UnderManagementAssetPage umap = new UnderManagementAssetPage(driver);
        ContributionTabPage ctp = new ContributionTabPage(driver);
        NewContributionFormPage ncp = new NewContributionFormPage(driver);
        NewCreatedContributionPage nccp = new NewCreatedContributionPage(driver);
        ExistingContributionPage ecp = new ExistingContributionPage(driver);

这是我从

BaseTest
类扩展的“设置”方法:

    @BeforeClass
    public void setup() throws InterruptedException {
        WebDriverManager.chromedriver().setup();
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("remote-allow-origins=*");
        driver = new ChromeDriver(chromeOptions);
        driver.manage().window().maximize();
        PropertyUtils u = new PropertyUtils();
        driver.get(u.readProperty("url"));
        LoginPage lp = new LoginPage(driver);
        lp.loginToPage();
    }
java selenium-webdriver automated-tests instance
1个回答
0
投票

您可以创建一个用@Before 注释的方法来初始化测试所需的对象。这个方法会在每次测试之前执行,你可以添加static关键字来确保对象在整个测试类中只被初始化一次。这是一个例子:

public class ContributionTests extends BaseTest {
private static MainPage mainPageGP;
private static AssetTabPage assetTabPage;
private static UnderManagementAssetPage umap;
private static ContributionTabPage ctp;
private static NewContributionFormPage ncp;
private static NewCreatedContributionPage nccp;
private static ExistingContributionPage ecp;

@Before
public void setUp() {
    mainPageGP = new MainPage(driver);
    assetTabPage = new AssetTabPage(driver);
    umap = new UnderManagementAssetPage(driver);
    ctp = new ContributionTabPage(driver);
    ncp = new NewContributionFormPage(driver);
    nccp = new NewCreatedContributionPage(driver);
    ecp = new ExistingContributionPage(driver);
}

@Test (priority = 1)
@Severity(SeverityLevel.CRITICAL)
@Description("Create a Contribution E2E, using only required fields, and validate the new creation contribution name")
public void tc1_CreateContribution() throws InterruptedException {
    mainPageGP.setMainTab("Assets");
    assetTabPage.selectUnderManagementTab();
    getSleep(500);
    assetTabPage.selectAsset("new Asset- Test on Edge");
    getSleep(500);
    umap.selectInnerTab("Contributions");
    // your test code here
}

// other tests here

}

在此示例中,setUp() 方法使用 BaseTest 类中的驱动程序实例初始化对象。然后,在每个测试方法中,您可以简单地引用在 setUp() 方法中初始化的对象。

© www.soinside.com 2019 - 2024. All rights reserved.