C# 有没有一种方法可以通过为type传入字符串值来编写FindElement调用
我目前正在为我们新的事务性网站编写一个Selenium UI测试,但是在设置解决方案的同时,我正在试图找到一种更有效的方法来创建一个查找元素的方法,而无需重复代码 理想情况下,我希望创建类似于以下内容的内容:C# 有没有一种方法可以通过为type传入字符串值来编写FindElement调用,c#,selenium,C#,Selenium,我目前正在为我们新的事务性网站编写一个Selenium UI测试,但是在设置解决方案的同时,我正在试图找到一种更有效的方法来创建一个查找元素的方法,而无需重复代码 理想情况下,我希望创建类似于以下内容的内容: public void SearchForElement(IWebDriver driver, string elementType, string elementReference) { driver.FindElement(By.Id(elementRe
public void SearchForElement(IWebDriver driver, string elementType, string elementReference)
{
driver.FindElement(By.Id(elementReference));
}
其中“Id”是变量值“elementType”
在以前的解决方案中,我做了以下工作:
if (elementType == "Id")
{
returnElement = driver.FindElement(By.Id(elementReference));
}
else if (elementType == "Name")
{
returnElement = driver.FindElement(By.Name(elementReference));
}
else if (elementType == "CssSelector")
{
returnElement = driver.FindElement(By.CssSelector(elementReference));
}
else if (elementType == "XPath")
{
returnElement = driver.FindElement(By.XPath(elementReference));
}
else if (elementType == "ClassName")
{
returnElement = driver.FindElement(By.ClassName(elementReference));
}
但是,正如您所看到的,后者实际上是同一行代码,只根据类型进行区分
如有任何帮助/建议,将不胜感激
由于UI测试是在开发我们的事务性站点的同时编写的(我知道这并不理想),我发现维护我正在使用的元素数组更容易 在开发的同时编写自动化测试并没有什么错。事实上,这是最理想的方法。你实际上有一个比你问的问题更大的问题。您的问题可以通过不同的测试体系结构来解决。将自动化行为封装在中允许您部分地创建其他测试人员可以使用的类,但将定义定位器之类的事情推迟到您有一个用户界面来处理 例如,假设您正在自动化待办事项列表应用程序的UI。UI尚未构建,但这不会阻止您编写测试:
todoList = new TodoListPageModel(driver);
todoList.Add("Pay the electric bill");
CollectionAssert.Contains(todoList.GetItems().ToList(), "Pay the electric bill");
您的页面模型如下所示:
public class TodoListPageModel
{
private readonly IWebDriver driver;
private IWebElement AddButton => throw new NotImplementedException();
private IWebElement TodoTextField => throw new NotImplementedException();
private IEnumerable<IWebElement> Items => throw new NotImplementedException();
public TodoListPageModel(IWebDriver driver)
{
this.driver = driver;
}
public void Add(string todo)
{
TodoTextField.SendKeys(todo);
AddButton.Click();
}
public IEnumerable<string> GetItems()
{
return Items.Select(item => item.Text.Trim());
}
}
public类TodoListPageModel
{
专用只读IWebDriver;
private IWebElement AddButton=>抛出新的NotImplementedException();
private IWebElement TodoTextField=>抛出新的NotImplementedException();
private IEnumerable Items=>抛出新的NotImplementedException();
公共ToDoListManager模型(IWebDriver)
{
this.driver=driver;
}
公共空添加(字符串todo)
{
TodoTextField.SendKeys(todo);
AddButton.Click();
}
公共IEnumerable GetItems()
{
returnitems.Select(item=>item.Text.Trim());
}
}
在拥有用户界面之前,引用元素或元素集合的属性可以抛出NotImplementedException。然后可以使用正确的定位器实现这些属性,并运行测试
根据谁先完成测试,您可以通知开发人员测试已经完成,他们只需要填写定位器。之后,开发人员在开发过程中运行测试,这非常好
由于UI测试是在开发我们的事务性站点的同时编写的(我知道这并不理想),我发现维护我正在使用的元素数组更容易 在开发的同时编写自动化测试并没有什么错。事实上,这是最理想的方法。你实际上有一个比你问的问题更大的问题。您的问题可以通过不同的测试体系结构来解决。将自动化行为封装在中允许您部分地创建其他测试人员可以使用的类,但将定义定位器之类的事情推迟到您有一个用户界面来处理 例如,假设您正在自动化待办事项列表应用程序的UI。UI尚未构建,但这不会阻止您编写测试:
todoList = new TodoListPageModel(driver);
todoList.Add("Pay the electric bill");
CollectionAssert.Contains(todoList.GetItems().ToList(), "Pay the electric bill");
您的页面模型如下所示:
public class TodoListPageModel
{
private readonly IWebDriver driver;
private IWebElement AddButton => throw new NotImplementedException();
private IWebElement TodoTextField => throw new NotImplementedException();
private IEnumerable<IWebElement> Items => throw new NotImplementedException();
public TodoListPageModel(IWebDriver driver)
{
this.driver = driver;
}
public void Add(string todo)
{
TodoTextField.SendKeys(todo);
AddButton.Click();
}
public IEnumerable<string> GetItems()
{
return Items.Select(item => item.Text.Trim());
}
}
public类TodoListPageModel
{
专用只读IWebDriver;
private IWebElement AddButton=>抛出新的NotImplementedException();
private IWebElement TodoTextField=>抛出新的NotImplementedException();
private IEnumerable Items=>抛出新的NotImplementedException();
公共ToDoListManager模型(IWebDriver)
{
this.driver=driver;
}
公共空添加(字符串todo)
{
TodoTextField.SendKeys(todo);
AddButton.Click();
}
公共IEnumerable GetItems()
{
returnitems.Select(item=>item.Text.Trim());
}
}
在拥有用户界面之前,引用元素或元素集合的属性可以抛出NotImplementedException。然后可以使用正确的定位器实现这些属性,并运行测试
根据谁先完成测试,您可以通知开发人员测试已经完成,他们只需要填写定位器。之后,开发人员将在开发过程中运行测试,这非常好。您为什么要为类型传递字符串?因为UI测试是在开发事务性站点的同时编写的(我知道这并不理想)我发现维护我正在使用的元素数组更容易,但对于这些元素是否具有关联的Id,我发现几乎并没有一致性,我必须使用各种不同的类型。我正在使用的数组的一个摘录是:{“ppsbyflow”、“emailRegister”、“Id”、“Email”}、{“ppsbyflow”、“emailRegisterGo”、“XPath”、“/html/body/div[6]/div/form/div/div/div[2]/input”},创建页面模型可能更好。您可以在不知道确切定位器的情况下定义方法和属性。类似于
private IWebElement EmailField=>抛出新的NotImplementedException()
为您提供了足够的代码编写能力。您希望为类型传递字符串的原因是什么?因为UI测试是在开发我们的事务性站点的同时编写的(我知道这并不理想)我发现维护我正在使用的元素数组更容易,但对于这些元素是否具有关联的Id,我发现几乎并没有一致性,我必须使用各种不同的类型。我正在使用的数组的一个摘录是:{“ppsbyflow”、“emailRegister”、“Id”、“Email”}、{“ppsbyflow”、“emailRegisterGo”、“XPath”、“/html/body/div[6]/div/form/div/div/div[2]/input”},创建页面模型可能更好。您可以定义方法和属性,而不必知道