分别管理Web驱动程序代码

时间:2016-05-25 05:23:16

标签: selenium selenium-webdriver

你能帮我解决这个问题。

我正在开发一个测试自动化框架,我希望单独保存我的webdriver代码并从我的测试用例开始。

请帮帮我。 - 谢谢

2 个答案:

答案 0 :(得分:0)

如果我已正确理解,请转到POM(页面对象框架)

1.The main advantage of Page Object Model is that if the UI changes for any page, it don’t 
require us to change any tests, we just need to change only the code within the page objects 
(Only at one place).

2.Page Object model is writing all the functionalities / reusable components of a page that
we want to automate in a separate class.

3.As per google wiki Page object **"Within your web app’s UI there are areas that your tests 
interact with. A Page Object simply models these as objects within the test code. This 
reduces the amount of duplicated code and means that if the UI changes, the fix need only 
be applied in one place."**

Example : google home page

1.Say now if we consider our google home page as Home page.
2.For the above pages we will create class as HomePage.class.
3.In class we will identify and write reusable methods which are specific to Home page.
4.'google home page' which will have many options like Search, Sign In, +You, Images etc.
5.Now all functionalities that we want to automate should have reusable methods/components 
for each page.
6.as our main page is google page we can navigate to other pages by clicking on any link 
from the google page. When ever we are navigating to other page, we need to return that page 
object. Else Return the current page object as this action doesn't navigate to a other page 
represented by another Page Object.

<强>优点。

1.There is clean separation between test code and page specific code such as locators 
(or their use if you’re using a UI map) and layout.
2.There is single repository for the services or operations offered by the page rather than 
  having these services scattered through out the tests.

在这两种情况下,这都允许由于UI更改而在一个地方进行所需的任何修改。点击POM上的更多信息

答案 1 :(得分:0)

使用Encapsulation转到POM模型。在下面的示例中,我创建了一个Test Setup类,然后可以在所有测试类中使用它。

所以这就是你的TestSetup类 -

        using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.IE;



    namespace dummy
    {
        [SetUpFixture]
        public class TestSetup
        {
            public static IWebDriver driver;   


            [OneTimeSetUp]
            public void testSetup()
            {

                if (driver == null)
                {
                    //Local Tests
                    driver = new FirefoxDriver();

                }
            }


            public static bool IsElementPresent(By by)
            {
                try
                {
                    driver.FindElement(by);
                    return true;
                }
                catch (NoSuchElementException)
                {
                    return false;
                }
            }

            [OneTimeTearDown]
            public void TearDown()
            {
                driver.Quit();
            }

        }
    }

以下可以是TestCase Class。请注意,您只是调用测试设置。因此,您的测试类将继承Primary类中的所有函数 - TestSetup

using System;
using OpenQA.Selenium;
using NUnit.Framework;
using System.Threading;

namespace dummy
{
    [TestFixture]
    [Parallelizable]
    public class TESTCASE
    {        

        IWebDriver driver = TestSetup.driver;

        [Test] 

        public void siteVisit()
        {
            driver.Navigate().GoToUrl("http://google.com");
             Assert.IsTrue(String.Equals("Google, driver.Title));

        }

    }
}