c#:切换案例:if(case :)

时间:2011-07-14 18:14:35

标签: c# selenium switch-statement

对于那些回答或查看我之前关于休息的问题的人来说,这个问题似乎很熟悉;声明。如果案例1得到满足,我想做点什么,如果是案例2,我想做点别的事情。如下所示。任何人都可以指导我(如果它是可行的)如何实现我正在尝试做的事情,而不是必须将我的if语句放在开关案例中?

        switch (searchType)
        {
            case "SearchBooks":
                Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
                break;

            case "SearchAuthors":
                Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
                break;
        }

        int count = int(Selenium.GetXpathCount("//[@id='Results_Table']");

        if case was "SearchBooks"
        {
            //do something
        }
        else if case was "SearchAuthors"
        {
            //do something else
        }

8 个答案:

答案 0 :(得分:5)

if (searchType == "SearchAuthors")

你不能做得更好。

答案 1 :(得分:4)

叫我极客。 :)

Selenium.Type(string.Format(@"//*[@id='{0}_TextInput']", searchType), searchText);
Selenium.Click(string.Format(@"//*[@id='{0}_SearchBtn']", searchType));

并保存案例陈述。

现在,显然,只有在searchType值始终与实际元素ID相对应时,这才有效。

答案 2 :(得分:3)

如果两种情况共同的唯一一行是

int count = int(Selenium.GetXpathCount("//[@id='Results_Table']");

然后我会写两次。

int count;
switch (searchType)
{
    case "SearchBooks":
        Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
        count = int(Selenium.GetXpathCount("//[@id='Results_Table']");
        //do something
        break;

    case "SearchAuthors":
        Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
        count = int(Selenium.GetXpathCount("//[@id='Results_Table']");
        //do something else
        break;
}

答案 3 :(得分:2)

您可以通过多种不同方式执行此操作,具体取决于您不希望在case语句中包含代码的原因。其他的回答者有很好的建议。在我看来,这个代码是尖叫的面向对象的方法:

var search = GetSearch(searchType);
search.PerformSearch(searchText);
int count = (int)Selenium.GetXpathCount("//[@id='Results_Table']");
search.DoSomething(count);

...

public ISearch GetSearch(string searchType)
{
    switch (searchType)
    {
        case "SearchBooks": return new SearchBooks();
        case "SearchAuthors": return new SearchAuthors();
        default: 
            throw new ArgumentException(
                string.Format("Invalid searchtype \"{0}\"", searchType), 
                "searchType");
    }
}


public interface ISearch
{
    void PerformSearch(string searchText);
    void DoSomething();
}

public class SearchBooks : ISearch
{
    public void PerformSearch(string searchText)
    {
        Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
    }

    void DoSomething()
    {
        // do something
    }
}

public class SearchAuthors : ISearch
{
    public void PerformSearch(string searchText)
    {
        Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
        Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
    }

    void DoSomething()
    {
        // do something else
    }
}

答案 4 :(得分:1)

searchType将包含您需要的值。

所以你可以得到你当前的案例陈述然后写

if (searchType == "SearchBooks")
// do something
else if (searchType == "SearchAuthors")
// do something else

答案 5 :(得分:1)

    Action<int> myAction;
    switch (searchType)
        {
            case "SearchBooks":
                Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
                // assign custom action
                myAction = count => { /* the count is count */};
                break;

            case "SearchAuthors":
                Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
                // assign custom action
                myAction = count => { /* the count is count */};
                break;
           default:
              throw new NotSupportedException ();
        }

        int count = int(Selenium.GetXpathCount("//[@id='Results_Table']");
        // invoke custom action
        myAction(count);

答案 6 :(得分:1)

    public enum MyEnumSearchTypes
    {
        SearchBooks, SearchAuthors, SearchSomethingElse 
    }

    public void MyDoSomethingMethod(MyEnumSearchTypes searchType)
    {
        switch (searchType)
        {
            case MyEnumSearchTypes.SearchBooks:
                Selenium.Type("//*[@id='SearchBooks_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchBooks_SearchBtn']");
            break;

            case MyEnumSearchTypes.SearchAuthors:
                Selenium.Type("//*[@id='SearchAuthors_TextInput']", searchText);
                Selenium.Click("//*[@id='SearchAuthors_SearchBtn']");
            break;
        }    

        if (searchType == MyEnumDefinedTypes.SearchBooks)
        {     
           do something
        }       

        else if (searchType == MyEnumDefinedTypes.SearchAuthors)
        {     
           do something else
        }            

        else
        {     
           do nothing
        }     
    }        

但是,我不明白为什么在switch语句中添加功能不符合您的需求?

答案 7 :(得分:1)

如果为了清晰起见,您希望有两批switch语句,请先将字符串转换为枚举,然后从那里继续。

如果您有兴趣这样做,我可以进一步填写答案。让我知道。

public enum SearchTypeEnum{None,SearchBooks,SearchAuthors}

var searchType = (SearchTypeEnum)Enum.Parse(typeof(SearchTypeEnum), searchTypeString);

switch (searchType){    
case SearchTypeEnum.SearchBooks:
...

switch (searchType){    
case SearchTypeEnum.SearchBooks:
...