模块化 Selenium RC 测试脚本的最佳实践 [英] Best Practices for modularizing Selenium RC test scripts

查看:58
本文介绍了模块化 Selenium RC 测试脚本的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Visual Studio (C#) 中创建 Selenium RC 测试脚本.我是努力重新分解测试;我所有的测试都在一个文件.我将不胜感激任何输入和/或指向网站、书籍、等等,以了解模块化测试.

I am creating Selenium RC test scripts in Visual Studio (C#). I am struggling with re-factoring the tests; all my tests are in a single file. I would appreciate any input and/or pointers to websites, books, etc. to learn about modularizing the tests.

我必须在不同的站点上运行相同的测试(相同的应用程序,但为不同的客户端和登录配置不同),即 95%相同的.有人愿意提供一些好的例子或最好的吗这样做的做法?

I have to run the same tests on different sites (same application but configured differently for different clients and logins) which are 95% same. Would anybody like to provide some good examples or best practices to do this?

谢谢!

推荐答案

编写 Selenium 测试或任何 UI 测试的最佳实践是 页面对象模型 这是为每个页面创建一个对象的想法.这些对象中的每一个都抽象了页面,因此当您编写测试时,它看起来并不像您一直在使用 Selenium.

Best practise for writing Selenium tests or any UI tests is Page Object Model which is the idea that you create an Object for each of the pages. Each of these objects abstract the page so when you write a test it doesnt really look like you have been working with Selenium.

因此,对于博客,您可以执行以下操作来为主页创建一个对象

So for a blog you would do something like this to create an object for the home page

public class Home
{
    private readonly ISelenium _selenium;

    /// <summary>
    /// Instantiates a new Home Page object. Pass in the Selenium object created in the test SetUp(). 
    /// When the object in instantiated it will navigate to the root
    /// </summary>
    /// <param name="selenium">Selenium Object created in the tests
    public Home(ISelenium selenium)
    {
        this._selenium = selenium;
        if (!selenium.GetTitle().Contains("home"))
        {
            selenium.Open("/");
        }
    }

    /// <summary>
    /// Navigates to Selenium Tutorials Page. Selenium object wll be passed through
    /// </summary>
    /// <returns>SeleniumTutorials representing the selenium_training.htm</returns>
    public SeleniumTutorials ClickSelenium()
    {
        _selenium.Click("link=selenium");
        _selenium.WaitForPageToLoad("30000");
        return new SeleniumTutorials(_selenium);
    }

    /// <summary>
    /// Click on the blog or blog year and then wait for the page to load
    /// </summary>
    /// <param name="year">blog or blog year
    /// <returns>Object representing /blog.* pages</returns>
    public Blog ClickBlogYear(string year)
    {
        _selenium.Click("link=" + year);
        _selenium.WaitForPageToLoad("30000");
        return new Blog(_selenium);
    }
    // Add more methods as you need them
}

然后您将创建一个如下所示的测试

then you would create a test that looks like the following

[TestFixture]
public class SiteTests
{
    private ISelenium selenium;
    [SetUp]
    public void Setup()
    {
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.theautomatedtester.co.uk");
        selenium.Start();
    }

    [TearDown]
    public void Teardown()
    {
        selenium.Stop();
    }

    [Test]
    public void ShouldLoadHomeThenGoToXpathTutorial()
    {
        Home home = new Home(selenium);
        SeleniumTutorials seleniumTutorials = home.ClickSelenium();
        SeleniumXPathTutorial seleniumXPathTutorial = seleniumTutorials.ClickXpathTutorial();
        Assert.True(seleniumXPathTutorial.
                    IsInputOnScreen(SeleniumXPathTutorial.FirstInput));
        Assert.True(seleniumXPathTutorial
                    .IsInputOnScreen(SeleniumXPathTutorial.SecondInput));
        Assert.True(seleniumXPathTutorial
                    .IsInputOnScreen(SeleniumXPathTutorial.Total));
    }
}

这篇关于模块化 Selenium RC 测试脚本的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆