【问题标题】:In Selenium, is it good to reuse same instance of the Page class multiple times in the same test method or should I create new instance?在 Selenium 中,是否可以在同一个测试方法中多次重用 Page 类的同一个实例,还是应该创建新实例?
【发布时间】:2021-07-19 15:54:55
【问题描述】:

我需要使用 PageFactory 模式测试网站:https://www.rediff.com
我的测试方法执行以下步骤:
第1步。转到 Rediff 主页https://www.rediff.com/
第2步。点击登录按钮
步骤 3。输入用户名和密码,然后点击提交按钮
第四步。单击最左上角的 rediff.com 链接,用户将返回 Rediff 主页。现在在搜索栏中输入文本并点击搜索按钮

下面是测试类:

    public class RediffLoginTest extends BaseClass
    {
    
        @Test
        public void loginAndSearch() throws InterruptedException
        {

//          Step-1
            getDriver().navigate().to("https://www.rediff.com");
    
//          Step-2
            RediffHomePage rhp = new RediffHomePage();
            PageFactory.initElements(getDriver(), rhp);
            rhp.signIn().click();
    
//          Step-3
            RediffLoginPage rlp = new RediffLoginPage();
            PageFactory.initElements(getDriver(), rlp);
            rlp.enterEmailID().sendKeys("hello");
            rlp.enterPassword().sendKeys("demopass");
            rlp.submitLogin().submit();
    
//          Step-4
            rlp.goToHomePage().click();
            rhp.enterSearchText().sendKeys("demo");
            rhp.submitSearch().click();
    
        }
    
    }

按照上面解释的第 4 步,它带我回到 Rediff 主页。我已经在步骤 2 中创建了 RediffHomePage 的对象rhp。所以,我的问题是,对于第 4 步,重用同一个对象 rhp 是否很好,如上面的代码所示,或者我应该在同一个测试方法中创建同一个页面类的另一个对象,例如说 RediffHomePage 的rhp1 如下代码所示?

RediffHomePage rhp1 = new RediffHomePage();
PageFactory.initElements(getDriver(), rhp1);
rhp1.enterSearchText().sendKeys("demo");
rhp1.submitSearch().click();

【问题讨论】:

    标签: selenium-webdriver pageobjects page-factory


    【解决方案1】:

    如果可能的话,在这种情况下,您绝对应该重用现有对象,而不是一次又一次地创建新的冗余对象。

    【讨论】:

    • 所以,如果我们在同一个测试方法中的多个步骤中引用同一个页面,那么我们可以在同一个测试方法中重用该特定 Page 类的现有对象,就像在这个例子中一样已在第一步中创建。我说的对吗?
    • 是的。确切地。反之亦然。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2019-06-04
    • 2014-10-24
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多