Best Practices for modularizing Selenium RC test scripts

前端 未结 1 1542
梦谈多话
梦谈多话 2021-02-10 00:11

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

1条回答
  •  情话喂你
    2021-02-10 00:16

    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;
    
        /// 
        /// 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
        /// 
        /// Selenium Object created in the tests
        public Home(ISelenium selenium)
        {
            this._selenium = selenium;
            if (!selenium.GetTitle().Contains("home"))
            {
                selenium.Open("/");
            }
        }
    
        /// 
        /// Navigates to Selenium Tutorials Page. Selenium object wll be passed through
        /// 
        /// SeleniumTutorials representing the selenium_training.htm
        public SeleniumTutorials ClickSelenium()
        {
            _selenium.Click("link=selenium");
            _selenium.WaitForPageToLoad("30000");
            return new SeleniumTutorials(_selenium);
        }
    
        /// 
        /// Click on the blog or blog year and then wait for the page to load
        /// 
        /// blog or blog year
        /// Object representing /blog.* pages
        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));
        }
    }
    

    0 讨论(0)
提交回复
热议问题