为 selenium pagefactory 设置 BasePage
Setup BasePage for selenium pagefactory
我已经设置了我网站的前两个页面,两个页面在页面顶部共享一个实用程序导航栏。正如您在这两页中看到的那样,我不得不复制用于查找和单击这些元素的代码。我已经读到您可以将公共元素移动到基页,然后让您的其他页面共享该基页的代码。
我的问题是,如何将重复代码移动到新文件,但仍然能够访问其他页面中的所有元素。
仪表板页面:
public class DashboardPage
{
private IWebDriver driver;
#region WebElement
[FindsBy(How = How.Id, Using = "AgentPAS")]
private IWebElement Policy;
[FindsBy(How = How.Id, Using = "Billing")]
private IWebElement Billing;
[FindsBy(How = How.LinkText, Using = "Activity")]
private IWebElement Activity;
[FindsBy(How = How.LinkText, Using = "Premium")]
private IWebElement Premium;
[FindsBy(How = How.LinkText, Using = "Production Summary")]
private IWebElement ProductionSummary;
[FindsBy(How = How.LinkText, Using = "Quote to Bind")]
private IWebElement QuoteToBind;
#endregion
public DashboardPage()
{
PageFactory.InitElements(ObjectRepository.Driver, this);
}
#region Actions
public void PolicyButton()
{
Policy.Click();
}
public void BillingButton()
{
Billing.Click();
}
public void ActivityLink()
{
Activity.Click();
}
public void PremiumLink()
{
Premium.Click();
}
public void ProductionSummaryLink()
{
ProductionSummary.Click();
}
public void QuoteToBindLink()
{
QuoteToBind.Click();
}
#endregion
}
ClientDetailsOnePage:
public class ClientDetailsOnePage
{
private IWebElement driver;
#region WebElement
[FindsBy(How = How.Id, Using = "AgentPAS")]
private IWebElement Policy;
[FindsBy(How = How.Id, Using = "Billing")]
private IWebElement Billing;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.Title']")]
private IWebElement Title;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.FirstName']")]
private IWebElement FirstName;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.Name']")]
private IWebElement LastName;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.DesignatedAuthority']")]
private IWebElement DesignatedAuthority;
[FindsBy(How = How.XPath, Using = "//span[@data-ref='displayEl']")]
private IWebElement UKResident;
[FindsBy(How = How.LinkText, Using = "Save & Exit")]
private IWebElement SaveExit;
[FindsBy(How = How.LinkText, Using = "Next")]
private IWebElement Next;
#endregion
public ClientDetailsOnePage()
{
PageFactory.InitElements(ObjectRepository.Driver, this);
}
#region Actions
public void PolicyButton()
{
Policy.Click();
}
public void BillingButton()
{
Billing.Click();
}
public void TitleDropDown(string text)
{
Title.SendKeys(text);
}
public void FirstNameTextBox(string text)
{
FirstName.SendKeys(text);
}
public void LastNameTextBox(string text)
{
LastName.SendKeys(text);
}
public void DesignatedAuthorityDropDown(string text)
{
DesignatedAuthority.SendKeys(text);
}
public void UKResidentCheckBox()
{
UKResident.Click();
}
public void SaveExitButton()
{
SaveExit.Click();
}
public void NextButton()
{
Next.Click();
}
#endregion
}
两个页面都有导航栏,因此导航栏是页面的一部分。您可以为导航栏创建一个单独的 class 并在您喜欢的每个页面上重复使用它。
这叫做合成
快速示例:
public class NavigationBar
{
[FindsBy(How = How.LinkText, Using = "Next")]
private IWebElement Next;
[FindsBy(How = How.LinkText, Using = "Previous")]
private IWebElement Previous;
public NavigationBar(IWebDriver driver)
{
PageFactory.InitElements(driver, this)
}
}
public class DashboardPage
{
public NavigationBar NavigationBar { get; set; }
public DashboardPage(IWebDriver driver)
{
NavigationBar = new NavigationBar(driver);
}
}
public class ClientDetailsOnePage
{
public NavigationBar NavigationBar { get; set; }
public ClientDetailsOnePage(IWebDriver driver)
{
NavigationBar = new NavigationBar(driver);
}
}
我已经设置了我网站的前两个页面,两个页面在页面顶部共享一个实用程序导航栏。正如您在这两页中看到的那样,我不得不复制用于查找和单击这些元素的代码。我已经读到您可以将公共元素移动到基页,然后让您的其他页面共享该基页的代码。
我的问题是,如何将重复代码移动到新文件,但仍然能够访问其他页面中的所有元素。
仪表板页面:
public class DashboardPage
{
private IWebDriver driver;
#region WebElement
[FindsBy(How = How.Id, Using = "AgentPAS")]
private IWebElement Policy;
[FindsBy(How = How.Id, Using = "Billing")]
private IWebElement Billing;
[FindsBy(How = How.LinkText, Using = "Activity")]
private IWebElement Activity;
[FindsBy(How = How.LinkText, Using = "Premium")]
private IWebElement Premium;
[FindsBy(How = How.LinkText, Using = "Production Summary")]
private IWebElement ProductionSummary;
[FindsBy(How = How.LinkText, Using = "Quote to Bind")]
private IWebElement QuoteToBind;
#endregion
public DashboardPage()
{
PageFactory.InitElements(ObjectRepository.Driver, this);
}
#region Actions
public void PolicyButton()
{
Policy.Click();
}
public void BillingButton()
{
Billing.Click();
}
public void ActivityLink()
{
Activity.Click();
}
public void PremiumLink()
{
Premium.Click();
}
public void ProductionSummaryLink()
{
ProductionSummary.Click();
}
public void QuoteToBindLink()
{
QuoteToBind.Click();
}
#endregion
}
ClientDetailsOnePage:
public class ClientDetailsOnePage
{
private IWebElement driver;
#region WebElement
[FindsBy(How = How.Id, Using = "AgentPAS")]
private IWebElement Policy;
[FindsBy(How = How.Id, Using = "Billing")]
private IWebElement Billing;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.Title']")]
private IWebElement Title;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.FirstName']")]
private IWebElement FirstName;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.Name']")]
private IWebElement LastName;
[FindsBy(How = How.XPath, Using = "//input[@fieldref='AccountInput.DesignatedAuthority']")]
private IWebElement DesignatedAuthority;
[FindsBy(How = How.XPath, Using = "//span[@data-ref='displayEl']")]
private IWebElement UKResident;
[FindsBy(How = How.LinkText, Using = "Save & Exit")]
private IWebElement SaveExit;
[FindsBy(How = How.LinkText, Using = "Next")]
private IWebElement Next;
#endregion
public ClientDetailsOnePage()
{
PageFactory.InitElements(ObjectRepository.Driver, this);
}
#region Actions
public void PolicyButton()
{
Policy.Click();
}
public void BillingButton()
{
Billing.Click();
}
public void TitleDropDown(string text)
{
Title.SendKeys(text);
}
public void FirstNameTextBox(string text)
{
FirstName.SendKeys(text);
}
public void LastNameTextBox(string text)
{
LastName.SendKeys(text);
}
public void DesignatedAuthorityDropDown(string text)
{
DesignatedAuthority.SendKeys(text);
}
public void UKResidentCheckBox()
{
UKResident.Click();
}
public void SaveExitButton()
{
SaveExit.Click();
}
public void NextButton()
{
Next.Click();
}
#endregion
}
两个页面都有导航栏,因此导航栏是页面的一部分。您可以为导航栏创建一个单独的 class 并在您喜欢的每个页面上重复使用它。
这叫做合成
快速示例:
public class NavigationBar
{
[FindsBy(How = How.LinkText, Using = "Next")]
private IWebElement Next;
[FindsBy(How = How.LinkText, Using = "Previous")]
private IWebElement Previous;
public NavigationBar(IWebDriver driver)
{
PageFactory.InitElements(driver, this)
}
}
public class DashboardPage
{
public NavigationBar NavigationBar { get; set; }
public DashboardPage(IWebDriver driver)
{
NavigationBar = new NavigationBar(driver);
}
}
public class ClientDetailsOnePage
{
public NavigationBar NavigationBar { get; set; }
public ClientDetailsOnePage(IWebDriver driver)
{
NavigationBar = new NavigationBar(driver);
}
}