Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for modularizing Selenium RC test scripts

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.

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?

Thanks!

like image 242
Ame Avatar asked Oct 10 '10 04:10

Ame


1 Answers

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));
    }
}
like image 127
AutomatedTester Avatar answered Sep 30 '22 04:09

AutomatedTester