2017-11-03 11 views
-1
[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 문 대신 case/switch 문으로 변경하려고 고심하고 있습니다. 여러 번 비슷한 로그인 방법이 있습니다.Selenium Webdriver 스위치/사례 C#

+2

이 일을 리팩토링과 같은 소리처럼 부를 것이다. codreview.stackexchange.com을 살펴보십시오. 이 if를 'switch (currentUrl)'로 바꾸면 무엇이 잘못 될 수 있는지는 분명하지 않지만 –

답변

1

도움이되기를 바랍니다. 그러나 ... 당신은 당신의 if 진술에 몇 가지 논리 문제를 가지고 있습니다. 그렇게하기 전에 정리하고 단순화해야한다고 생각합니다.

예 :

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

위 어설가 실패 할 것입니다? 그렇지 않습니다. 이미 본질적으로 URL을 비교함으로써 어설 션을 완료 했으므로 어설 션, 또는 다음 어설 션, 또는 다음 어설 션이 실제로 필요하지 않습니다. 대신 현재 URL이 3 개의 URL 목록에 있는지 확인하는 것이 좋습니다. 당신은 실제로 실패를 야기하는 경우입니다 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; 
    } 
} 

누락 된 중요한 경우

등으로이 작업을 수행 할 수 있습니다. URL이 세 가지 중 하나와 일치하지 않으면 어떻게해야합니까? 귀하의 if은이를 커버하지 않으며 default 케이스는 switch에 있습니다. default 케이스는 케이스가 과 일치하지 않는 경우에 적용됩니다.

이렇게 작동하는 방식은 로그인 버튼을 클릭하면 다음 페이지로 이동합니다. Google은 URL을 가져 와서 두 개의 URL과 비교합니다. 만약 그것들 중 하나와 일치한다면 우리는 스위치를 벗어나 아무것도하지 않습니다. BaseUrls.PromoPageUk과 일치하면 필요한 단계를 수행합니다. 우리는이 세 가지 경우에서 주장 할 필요가 없기 때문에 주장하지 않습니다. 테스트가 통과한다는 것을 알고 있습니다. 이제 default 사례로 이동합니다. URL이 위의 3 중 하나와 일치하지 않으면 예기치 않은 페이지에 도착했기 때문에 일부 어설 션이 실패해야합니다. NUnit 또는 친숙한 테스트 프레임 워크를 사용하지 않아 주석 자리 표시자를 넣었으므로 프레임 워크에서 어설 션을 코딩하는 방법을 모르겠습니다.


다음은 요구하지 않았다 다른 충고 ... :) 당신은 정말 NUnit 또는 다른 표준 라이브러리를 사용한다

  1. . 어설 션이 _driver 참조에 매달려서는 안되기 때문에 표준 라이브러리를 사용하지 않는다고 말할 수 있습니다. NuGet에서 NUnit을 가져 와서 설치하고 사용하십시오. 그것은 당신이 필요로하는 모든 단언 비교를 제공합니다. 자신 만의 어설 션 코드를 작성하고 테스트 할 필요가 없으며 많은 사람들이 만들고 사용하기 때문에 필요가 없습니다.

  2. 나는 많은 이유 때문에 Click()과 같은 일반적인 방법을 사용하는 팬이 아니지만 적어도 하나는 가지고있을 경우 By 로케이터를 전달해야하므로 유형을 전달할 필요가 없습니다. 로케이터와 로케이터의 당신이 쓰는이 방법과 다른 방법을 사용하면 인생이 훨씬 쉬워 질 것입니다.예를

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

    당신의 VariableList 클래스는

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

    과 같을 것이다 당신은

    _driver.Click(VariableList.LoginButtonId) 
    
1

나는이 몇 가지 튜토리얼을 읽은 후 꽤 간단해야 switchif 문을 번역

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