调用多个页面对象

时间:2018-10-11 08:56:45

标签: c# selenium-webdriver

我想在同一测试中调用两个或多个页面对象。我设法调用了页面对象,但是它正在打开浏览器并分别进行每个测试,我想一个接一个地运行,因为第一个已经打开了浏览器。我该怎么办?

那是一页对象:

       using OpenQA.Selenium;
       using OpenQA.Selenium.Support.PageObjects;
       using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Text;
       using System.Threading.Tasks;
       using System.Threading;


      namespace SeleniumTests.PageObjects
      {
         class RegisterPage : TestBase
      {
        public RegisterPage()
        {
            PageFactory.InitElements(PropertiesCollection.driver, this);
        }

        public string GetLoginUrl()
        {
            return baseUrl + "/" + TestBase.market + "/" + TestBase.lang + "/register/?ReturnUrl=%2f" + TestBase.market + "%2f" + TestBase.lang + "%2faccount%2f&Zadkiel=true";
        }

        // Find first name input
        [FindsBy(How = How.Id, Using = "FirstName")]
        public IWebElement FirstNameInput { get; set; }

        // Find last name input
        [FindsBy(How = How.Id, Using = "LastName")]
        public IWebElement LastNameInput { get; set; }

        // Find email input
        [FindsBy(How = How.Id, Using = "txtEmail")]
        public IWebElement EmailInput { get; set; }

        // Find country prefix input
        [FindsBy(How = How.Id, Using = "selectphone")]
        public IWebElement CountryPrefixSelect { get; set; }

        // Find phone number input
        [FindsBy(How = How.Id, Using = "txtMobilePhone")]
        public IWebElement PhoneNumberInput { get; set; }

        // Find password input
        [FindsBy(How = How.Id, Using = "Password")]
        public IWebElement PasswordInput { get; set; }

        // Find password confirm input
        [FindsBy(How = How.Id, Using = "ConfirmPassword")]
        public IWebElement PasswordConfirmInput { get; set; }

        // Find submit button
        [FindsBy(How = How.Id, Using = "btnSubmit")]
        public IWebElement SubmitButton { get; set; }

        // Find agree button
        [FindsBy(How = How.XPath, Using = "//button[@type='submit']")]
        public IWebElement AgreeButton { get; set; }

        public RegisterPage TypeFirstName(string firstName)
        {
            FirstNameInput.Clear();
            FirstNameInput.SendKeys(firstName);
            return this;
        }

        public RegisterPage TypeLastName(string lastName)
        {
            LastNameInput.Clear();
            LastNameInput.SendKeys(lastName);
            return this;
        }

        public RegisterPage TypeEmail(string username)
        {
            EmailInput.Clear();
            EmailInput.SendKeys(username);
            return this;
        }

        public RegisterPage TypePhone(string phone)
        {
            PhoneNumberInput.Clear();
            PhoneNumberInput.SendKeys(phone);
            return this;
        }

        public RegisterPage TypePassword(string password)
        {
            PasswordInput.Clear();
            PasswordInput.SendKeys(password);
            return this;
        }

        public RegisterPage ConfirmPassword(string password)
        {
            PasswordConfirmInput.Clear();
            PasswordConfirmInput.SendKeys(password);
            return this;
        }

        public RegisterPage SubmitLogin()
        {
            SubmitButton.Click();
            return this;
        }

        public RegisterPage IAgreeButton()
        {
            AgreeButton.Click();
            return this;
        }

        public void DoRegister(string firstName, string lastName, string username, string phone, string password1, string password2)
        {
            var returnUrl = PropertiesCollection.driver.Url;

            TypeFirstName(firstName);
            TypeLastName(lastName);
            TypeEmail(username);
            TypePhone(phone);
            TypePassword(password1);
            ConfirmPassword(password2);
            SubmitLogin();
            IAgreeButton();

            //if AllowXRequestsEveryXSeconds restraint applied, wait a bit and repeat
            if (!IsElementPresent(By.TagName("h2")))
            {
                Console.WriteLine("AllowXRequestsEveryXSeconds triggered, try again and returns to " + returnUrl);
                Thread.Sleep(5000);
                PropertiesCollection.driver.Url = returnUrl;
                DoRegister(firstName, lastName, username, phone, password1, password2);
            }
        }
    }
}

那是第二个:

      using OpenQA.Selenium;
      using OpenQA.Selenium.Support.PageObjects;
      using System;
      using System.Threading;

      namespace SeleniumTests.PageObjects
      {
          class AddAddress : TestBase
       {
        public AddAddress()
        {
            PageFactory.InitElements(PropertiesCollection.driver, this);
        }

        //public string GetLoginUrl()
        //{
        //    return baseUrl + "/" + TestBase.market + "/" + TestBase.lang + "account/activate-test-kit/";
        //}

        // Find activate a test button
        [FindsBy(How = How.XPath, Using = "//a[@class='button button--outline ']")]
        public IWebElement ActivateKit { get; set; }

        // Find country dropbox
        //[FindsBy(How = How.XPath, Using = "//ul/li[@rel='103']")]
        //public IWebElement CountryDropDown { get; set; }

        // Find addess line 1 input
        [FindsBy(How = How.Id, Using = "AddressLine1")]
        public IWebElement AddressLine1 { get; set; }

        // Find address line 2 input
        [FindsBy(How = How.Id, Using = "AddressLine2")]
        public IWebElement AddressLine2 { get; set; }

        // Find town name input
        [FindsBy(How = How.Id, Using = "City")]
        public IWebElement TownName { get; set; }

        // Find postal code input
        [FindsBy(How = How.Id, Using = "PostalCode")]
        public IWebElement PostalCode { get; set; }

        // Find submit button
        [FindsBy(How = How.XPath, Using = "//button[@type='submit']")]
        public IWebElement SubmitButton { get; set; }

        //public AddAddress SelectCountry()
        //{
        //    CountryDropDown.Click();
        //    return this;
        //}

        public AddAddress ActivateAKit()
        {
            ActivateKit.Click();
            return this;
        }

        public AddAddress AddressLineOne(string address1)
        {
            AddressLine1.Clear();
            AddressLine1.SendKeys(address1);
            return this;
        }

        public AddAddress AddressLineTwo(string address2)
        {
            AddressLine2.Clear();
            AddressLine2.SendKeys(address2);
            return this;
        }

        public AddAddress TownCity(string city)
        {
            TownName.Clear();
            TownName.SendKeys(city);
            return this;
        }

        public AddAddress PostCode(string zip)
        {
            PostalCode.Clear();
            PostalCode.SendKeys(zip);
            return this;
        }

        public AddAddress SubmitLogin()
        {
            SubmitButton.Click();
            return this;
        }

        public void DoAddAddress(string address1, string address2, string city, string zip)
        {
            //var returnUrl = PropertiesCollection.driver.Url;

            //SelectCountry();
            ActivateAKit();
            AddressLineOne(address1);
            AddressLineTwo(address2);
            TownCity(city);
            PostCode(zip);
            SubmitLogin();

            //if AllowXRequestsEveryXSeconds restraint applied, wait a bit and repeat
            //if (!IsElementPresent(By.TagName("h2")))
            //{
            //    Console.WriteLine("AllowXRequestsEveryXSeconds triggered, try again and returns to " + returnUrl);
            //    Thread.Sleep(5000);
            //    PropertiesCollection.driver.Url = returnUrl;
            //    DoAddAddress(address1, address2, city, zip);
            //}
        }


    }
}

我想在这里称这两个页面:

   namespace SeleniumTests.Tests.Checkout
    {

     [TestFixture]
class ActivateKitTest : TestBase
{

    AddAddress addAddress;
    [TestCase("Dame Street", "City Center", "Dublin", "D2")]
    public void C598_ActivateAKitWithNewAccount(string address1, string address2, string city, string zip)
    {
        this.addAddress = new AddAddress();

        //driver.Url = addAddress.GetLoginUrl();

        addAddress.DoAddAddress(address1, address2, city, zip);

        //new WebDriverWait(driver, TimeSpan.FromSeconds(2)).Until(ExpectedConditions.ElementIsVisible((By.XPath("//p[@class='welcome']"))));

        //Assert.That(driver.FindElements(By.XPath("//p[@class='welcome']")).Count, Is.EqualTo(1));


    }
}
}

0 个答案:

没有答案