Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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
硒Webdriver开关/外壳c#_C#_Selenium Webdriver_Webdriver_Switch Statement_Case - Fatal编程技术网

硒Webdriver开关/外壳c#

硒Webdriver开关/外壳c#,c#,selenium-webdriver,webdriver,switch-statement,case,C#,Selenium Webdriver,Webdriver,Switch Statement,Case,我正在努力将其转换为case/switch语句,而不是使用多个if语句。我多次使用类似的登录方法。我希望这能有所帮助 [When(@"I click the Login button")] public void WhenIClickTheLoginButton() { _driver.Click(ElementType.Id, VariableList.LoginButtonId); //Clicks login button first

我正在努力将其转换为case/switch语句,而不是使用多个if语句。我多次使用类似的登录方法。

我希望这能有所帮助

    [When(@"I click the Login button")]
    public void WhenIClickTheLoginButton()
    {
        _driver.Click(ElementType.Id, VariableList.LoginButtonId); //Clicks login button first

        string currentUrl = _driver.Url;

        if (currentUrl == BaseUrls.HomepageUk)
        {
            _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.HomepageUk);
        }
        if (currentUrl == BaseUrls.LogonPageUk) //Takes into account erroneous login
        {
            _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.LogonPageUk);
        }
        if (currentUrl == BaseUrls.PromoPageUk) //Takes into account the possibility of a promotion being displayed
        {
            _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.PromoPageUk);
            _driver.WaitForElementPresent(ElementType.Id, VariableList.PromoBanner);
            _driver.AssertElementDisplayed(ElementType.Id, VariableList.PromoBanner);
            _driver.AssertElementDisplayed(ElementType.XPath, VariableList.ContinueToHomepageButton);
            _driver.Click(ElementType.XPath, VariableList.ContinueToHomepageButton);
        }
    }

阅读一些教程后,将
if
语句转换为
开关应该非常简单。但是您的
if
语句中存在一些逻辑问题,我认为在执行之前应该对这些逻辑问题进行清理和简化

例如:

switch (currentUrl)
{
    case BaseUrls.HomepageUk
        _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.HomepageUk);
        break;
    case BaseUrls.LogonPageUk
        _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.LogonPageUk);
        break;
    case BaseUrls.PromoPageUk
        _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.PromoPageUk);
        _driver.WaitForElementPresent(ElementType.Id, VariableList.PromoBanner);
        _driver.AssertElementDisplayed(ElementType.Id, VariableList.PromoBanner);
        _driver.AssertElementDisplayed(ElementType.XPath, VariableList.ContinueToHomepageButton);
        _driver.Click(ElementType.XPath, VariableList.ContinueToHomepageButton);
        break;
}
上面的断言什么时候会失败?不会的。实际上,您已经通过比较URL完成了断言,因此实际上不需要该断言、下一个断言或下一个断言。相反,您需要验证当前URL是否在3个URL的列表中。您可以使用
开关执行此操作,如

if (currentUrl == BaseUrls.HomepageUk)
{
    _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.HomepageUk);
}
您遗漏的重要案例是实际会导致失败的案例。如果URL与三个URL中的一个不匹配怎么办?您的
如果
s没有涵盖这一点,这就是
开关上出现的
默认
案例。如果比较与任何一个案例都不匹配,则默认情况下将覆盖

这样做的方式是点击登录按钮,我们进入下一页。我们获取URL并将其与两个URL进行比较。如果它与其中任何一个匹配,我们什么也不做就跳出开关。如果它匹配
BaseUrls.PromoPageUk
,那么我们将执行所需的步骤。注意,我们在这三种情况下都没有断言,因为没有必要。我们知道考试会通过的。现在我们来看一下
默认情况。如果URL与上面3个不匹配,那么我们需要一些断言失败,因为我们登录到了一个意外的页面。我放置了一个注释占位符,因为您没有使用
NUnit
或我熟悉的任何测试框架,所以我不知道您将如何在框架中编写断言代码


以下是您没有要求的其他建议…:)

  • 您确实应该使用
    NUnit
    或其他标准库。我可以告诉您没有使用标准库,因为断言不应该挂在
    \u驱动程序
    引用上。从NuGet中取出NUnit,安装并使用它。它附带了您需要的所有断言比较。您不需要编写和测试自己的断言代码,也没有必要,因为它已经被很多人建立和使用了

  • 出于很多原因,我通常不太喜欢像
    Click()
    这样的通用方法,但是如果你想要一个,至少通过
    定位器传入
    ,这样你就不必传入定位器类型和定位器。这将使你的生活更容易,因为你写的这个和其他方法。比如说

    [When(@"I click the Login button")]
    public void WhenIClickTheLoginButton()
    {
        _driver.Click(ElementType.Id, VariableList.LoginButtonId); //Clicks login button first
    
        switch (_driver.Url)
        {
            case BaseUrls.HomepageUk:
                break;
            case BaseUrls.LogonPageUk:
                break;
            case BaseUrls.PromoPageUk:
                _driver.WaitForElementPresent(ElementType.Id, VariableList.PromoBanner);
                _driver.AssertElementDisplayed(ElementType.Id, VariableList.PromoBanner);
                _driver.AssertElementDisplayed(ElementType.XPath, VariableList.ContinueToHomepageButton);
                _driver.Click(ElementType.XPath, VariableList.ContinueToHomepageButton);
                break;
            default:
                // some assert that the URL is not one of the acceptable three
                break;
        }
    }
    
    您的
    变量列表
    类如下所示

    public static void Click(By locator)
    {
        _driver.FindElement(locator).Click();
    }
    
    你会这样称呼它吗

    public static class VariableList
    {
        public static By LoginButtonId = By.Id("whateverTheIdIs");
    }
    

  • 听起来像是重构。请访问codreview.stackexchange.com。虽然用
    开关(currentUrl)
    _driver.Click(VariableList.LoginButtonId)