Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将两个selenium按钮点击组合在一个if语句中_C#_Selenium_Selenium Webdriver_Specflow - Fatal编程技术网

C# 将两个selenium按钮点击组合在一个if语句中

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 (.*

我想使用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 (.*)")]
    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();          
     }
}