简体   繁体   English

Selenium Webdriver开关/案例c#

[英]Selenium Webdriver Switch/Case c#

    [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);
        }
    }

I'm struggling to change this into a case/switch statement rather than using multiple if statements. 我正在努力将其更改为case / switch语句,而不是使用多个if语句。 I have a similar login method multiple times. 我多次使用类似的登录方法。

I hope this helps 我希望这有帮助

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;
}

Translating if statements into a switch should be pretty straightforward after reading some tutorials. 阅读一些教程后,将if语句转换为switch应该非常简单。 BUT... you've got some logic issues in your if statements that I think should be cleaned up and simplified before doing so. 但是...您的if语句中存在一些逻辑问题,我认为应该在进行此操作之前先进行清理和简化。

An example: 一个例子:

if (currentUrl == BaseUrls.HomepageUk)
{
    _driver.AssertIsTrueByUrl(_driver.Url, BaseUrls.HomepageUk);
}

When is the above Assert going to fail? 上述断言什么时候会失败? It won't. 不会的 You've already essentially done the assert by comparing the URLs so there's really no need for that assert, or the next one, or the next one. 您实际上已经通过比较URL来完成断言,因此实际上不需要该断言,下一个或下一个断言。 What you want instead is to verify that the current URL is in the list of 3 URLs. 您想要的是验证当前URL是否在3个URL的列表中。 You can do this with a switch like 您可以使用类似的switch来执行此switch

[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;
    }
}

The important case that you were missing is the case that would actually cause a failure. 您丢失的重要情况是实际上会导致故障的情况。 What if the URL doesn't match one of the three? 如果URL与三个不匹配怎么办? Your if s don't cover that and that's where the default case comes in on the switch . 您的if不能解决这个问题,那就是default情况下switch The default case covers if the comparison doesn't match any of the cases. default情况包括比较是否与任何情况都不匹配。

So the way this works is the login button is clicked and we arrive on the next page. 因此,其工作方式是单击登录按钮,然后我们进入下一页。 We grab the URL and compare it to two of the URLs. 我们获取该URL,并将其与两个URL进行比较。 If it matches either of those we break out of the switch doing nothing. 如果它与任何一个都匹配,我们将退出开关,不执行任何操作。 If it matches BaseUrls.PromoPageUk then we do the steps required. 如果它与BaseUrls.PromoPageUk相匹配,那么我们将执行所需的步骤。 Notice that we don't assert in any of these three cases because there is no need. 注意,在这三种情况下,我们都没有断言,因为没有必要。 We know the test would pass. 我们知道测试会通过。 Now we come to the default case. 现在我们来看default情况。 If the URL doesn't match one of the above 3, then we need some assert to fail because we landed on an unexpected page. 如果URL与以上3个都不匹配,则需要断言失败,因为我们登陆了意外页面。 I put a comment placeholder because you aren't using NUnit or any test framework that I'm familiar with so I don't know how you would code that assert in your framework. 我放置了一个注释占位符,因为您没有使用NUnit或我熟悉的任何测试框架,所以我不知道如何在框架中对声明的代码进行编码。


Here's some other advice that you didn't ask for... :) 这是您没有要求的其他建议... :)

  1. You really should be using NUnit or some other standard library. 您确实应该使用NUnit或其他一些标准库。 I can tell you aren't using a standard library because asserts shouldn't be hanging off of _driver references. 我可以告诉您没有使用标准库,因为断言不应挂在_driver引用上。 Grab NUnit out of NuGet, install it, and use it. 从NuGet中拿起NUnit,安装并使用它。 It comes with all the assert comparisons you will need. 它带有您将需要的所有断言比较。 You don't need to write and test your own assert code and there's no need since it's established and used by LOTS of people. 您不需要编写和测试自己的断言代码,也不需要这样做,因为它是由很多人建立和使用的。

  2. I'm generally not a big fan of generic methods like Click() for a lot of reasons but if you are going to have one, at least pass in a By locator so you don't have to pass in the type of locator and the locator. 由于很多原因,我通常不喜欢Click()这样的通用方法,但如果要使用它,至少要传入By定位符,这样就不必传入定位符的类型了,定位器。 It will make your life a lot easier for this and other methods you write. 使用此方法和您编写的其他方法,会使您的生活更加轻松。 For example 例如

     public static void Click(By locator) { _driver.FindElement(locator).Click(); } 

    Your VariableList class would look like 您的VariableList类看起来像

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

    and you would call it like 你会这样称呼它

     _driver.Click(VariableList.LoginButtonId) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM