C# 将两个selenium按钮点击组合在一个if语句中
我想使用IF语句将以下两个按钮操作组合到SpecFlow场景中C# 将两个selenium按钮点击组合在一个if语句中,c#,selenium,selenium-webdriver,specflow,C#,Selenium,Selenium Webdriver,Specflow,我想使用IF语句将以下两个按钮操作组合到SpecFlow场景中 _driver.FindElement(By.Id("gbqfba")).Click(); // Google - 'Google Search' _driver.FindElement(By.Id("gbqfsb")).Click(); // Google - 'I'm feeling lucky' 我想使用(*)通过“谷歌搜索”或“我觉得很幸运”。有什么好办法可以做到这一点吗 [When("I click on (.*
_driver.FindElement(By.Id("gbqfba")).Click(); // Google - 'Google Search'
_driver.FindElement(By.Id("gbqfsb")).Click(); // Google - 'I'm feeling lucky'
我想使用(*)通过“谷歌搜索”或“我觉得很幸运”。有什么好办法可以做到这一点吗
[When("I click on (.*)")]
public void WhenIClickOn(string buttonValue)
{
}
一个简单的方法是:
[When("I click on (.*)")]
public void WhenIClickOn(string buttonValue)
{
if(buttonValue=="Google Search")
{
_driver.FindElement(By.Id("gbqfba")).Click(); // Google - 'Google Search'
}
else if(buttonValue=="I'm feeling lucky")
{
_driver.FindElement(By.Id("gbqfsb")).Click(); // Google - 'Google Search'
}
else
{
throw new ArgumentOutOfRangeException();
}
}
但是,specflow还支持通过以下方式实现此目的:
这确保了从规范中的id到封装该id和任何相关位的对象的转换在单个位置进行,而不是在使用它的每个测试中进行。谢谢Sam,这非常有用:)
[When("I click on (.*)")]
public void WhenIClickOn(ButtonIdentifier buttonId)
{
_driver.FindElement(By.Id(buttonId.Identifier)).Click();
}
[StepArgumentTransformation]
public ButtonIdentifier GetButtonIdentifier(string buttonValue)
{
switch (buttonValue)
{
case "Google Search":
return new ButtonIdentifier("gbqfba");
case "I'm feeling lucky":
return new ButtonIdentifier("gbqfsb");
default:
throw new ArgumentOutOfRangeException();
}
}