Selenium WebDriver页面对象

duc*_*212 9 webdriver pageobjects selenium-webdriver

关于selenium webdriver中页面对象的快速问题.我们的网站非常动态,有很多ajax和各种身份验证状态.很难弄清楚如何定义每个页面对象但是我可以说我已经想出来并定义了几个代表我们网站的页面对象.

你如何处理页面之间的交叉.所以我得到了一个主页的页面对象,一个用于我的帐户页面,另一个用于我的结果页面.然后我需要编写一个遍历所有页面的测试来模拟执行多个操作的用户.

How do you say给我一个HomePage对象来创建一个新的用途 - >然后获取一个帐户页面对象去执行一些用户操作 - 然后得到一个结果页面对象来验证这些操作都来自一个脚本.

人们如何做到这一点?

谢谢

Dav*_*eds 10

当您模拟用户在浏览器的URL栏中输入新URL时,测试类负责创建所需的页面对象.

另一方面,当您在页面上进行某些操作时会导致浏览器指向另一个页面 - 例如,单击链接或提交表单 - 那么该页面对象的责任就是返回下一页对象.

由于我不太了解您的主页,帐户页面和结果页面之间的关系,以告诉您它在您的网站中的确切表现,我将使用在线商店应用程序作为示例.

假设你有一个SearchPage.当您在SearchPage上提交表单时,它返回一个ResultsPage.当您单击结果时,您将获得ProductPage.所以类看起来像这样(缩写为相关方法):

public class SearchPage {

    public void open() {
        return driver.get(url);
    }

    public ResultsPage search(String term) {
        // Code to enter the term into the search box goes here
        // Code to click the submit button goes here
        return new ResultsPage();
    }

}

public class ResultsPage {

    public ProductPage openResult(int resultNumber) {
        // Code to locate the relevant result link and click on it
        return new ProductPage();
    }

}
Run Code Online (Sandbox Code Playgroud)

执行这个故事的测试方法看起来像这样:

@Test
public void testSearch() {

    // Here we want to simulate the user going to the search page
    // as if opening a browser and entering the URL in the address bar. 
    // So we instantiate it here in the test code.

    SearchPage searchPage = new SearchPage();
    searchPage.open(); // calls driver.get() on the correct URL

    // Now search for "video games"

    ResultsPage videoGameResultsPage = searchPage.search("video games");

    // Now open the first result

    ProductPage firstProductPage = videoGameResultsPage.openResult(0);

    // Some assertion would probably go here

}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,页面对象的这种"链接"使每个页面对象返回下一个页面对象.

结果是您最终会有许多不同的页面对象实例化其他页面对象.因此,如果您有一个任何相当大的站点,您可以考虑使用依赖注入框架来创建这些页面对象.