我已经创建了硒测试,但是我想开始清理它们以提高可读性和重用性。您如何处理变量?
我曾考虑过要设置一种可以查看的按钮字典,但认为这可能会导致更多问题而不是解决。
[Test]
public static void TestAccrualRuleSet()
{
//Setup buttons and variables
IWebDriver driver = new ChromeDriver();
var hamburgerMenu = By.XPath("//*[@id='menu-toggle']/span/i");
var settingsMenuButton = By.XPath("//*[@id='ulAdminNavBar']/li[4]/a");
var timeAndLaborMgmtButton = By.XPath("//*[@id='ulAdminNavBar']/li[4]/ul/li[11]/a");
var timeOffTrackingButton = By.XPath("//*[@id='ulAdminNavBar']/li[4]/ul/li[11]/ul/li[2]/a");
var actionMenu = By.Id("ucAccrualRules_ddlAccrual");
var ruleNameTextBox = By.Id("ucAccrualRules_ucAccrualRule_txtRuleName");
var accrualRuleFrequencyDropDown = By.Id("ucAccrualRules_ucAccrualRule_cboAccrualType");
var dropDownAccrualTypes = By.Id("ucAccrualRules_ucAccrualRule_hourTypes");
var dropDownAccrualTypesCheckBox1 = By.Id("ucAccrualRules_ucAccrualRule_hourTypes_0");
var dropDownAccrualTypesCheckBox2 = By.Id("ctl00_ContentPlaceHolder1_ucAccrualRules_ucAccrualRule_hourTypes_1");
var dropDownAccrualTypesCheckBox3 = By.Id("ctl00_ContentPlaceHolder1_ucAccrualRules_ucAccrualRule_hourTypes_6");
var maxCarryForwardTextBox = By.Id("ctl00_ContentPlaceHolder1_ucAccrualRules_ucAccrualRule_txtMaxRollover");
var accrualWaitingTextBox = By.Id("ctl00_ContentPlaceHolder1_ucAccrualRules_ucAccrualRule_txtAccrualWaitingPeriod");
var saveButton = By.XPath("//*[@id='ctl00_ContentPlaceHolder1_ucAccrualRules_ucAccrualRule_cmdUpdateName']");
var savedSuccessfully = By.Id("ctl00_ContentPlaceHolder1_ucAccrualRules_ucAccrualRule_UCErrorRuleProperties_lblMessage");
var propertiesButton = By.Id("ctl00_ContentPlaceHolder1_ucAccrualRules_ucAccrualRule_tpAccuralRulePropertiesnewForms");
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
//Begin test
AppExtensions.LoginUsingChrome(driver);
AppExtensions.SelectCompany(driver, "Ren's Test Account");
System.Threading.Thread.Sleep(1500);
WebDriverExtensions.Element_Click(driver, hamburgerMenu);
System.Threading.Thread.Sleep(1500);
虽然这不是完整的测试,但它向您展示了我用于存储执行测试所需的按钮/链接的大量变量的示例。关于如何以及在硒测试中存储这些变量的最佳实践是什么?
答案 0 :(得分:1)
建议将变量分组在类中。您每页将有一个类,并将该页面的Web元素的所有变量移到相应的类中。 这称为POM或页面对象模型。 您可以走得更远,并将方法添加到这些类中,这些方法涵盖特定页面的多个操作,甚至导航到其他页面。提示:导航方法将导航并等待下一页加载,并返回该目标页面的POM类的实例。
答案 1 :(得分:1)
通常人们会选择两种模式:
答案 2 :(得分:0)
一种方法是您可以创建属性文件并在其中维护所有xpath等。然后只需引用类中的属性文件,因为需要使用xpath或定位器来访问变量。这样,如果xpath中的任何一个发生了更改,则只有一个要更新的文件即属性文件。示例:
Inside home.properties file you can have xpath as
searchBtn = //input[@elementid = 'abc']
this.properties = new Properties();
InputStream input = new FileInputStream("home.properties");
this.properties.load(input);
//Inside your class file you can access xpath as
String searchBtn = By.xpath(properties.get("searchButton"))
我想不出另一种方法。