2014-02-20 57 views
0

所以我想創建一個基本上持有我的匹配器,實際結果,錯誤消息和測試標籤的「TestClass」。使用匹配器對象

但是,我慢慢開始意識到這可能是不可能的,但我覺得應該是。

也許有人在這裏可以提供幫助。

這裏就是我想要做:

public void runTest(){ 
     Assert.assertThat(testLabel + " " + errorMessage, actualResult, testToRun); 
    } 

,或者因爲我做的Selenium測試,這樣的事情:

public void runTestAsWebElement(String attributeToCheck){ 
     Object tempActualResult; 

     switch (attributeToCheck.toLowerCase()){ 
      case "isdisplayed()": 
      case "isdisplayed": 
       tempActualResult = ((WebElement) actualResult).isDisplayed(); 
       break; 
      case "isselected": 
      case "isselected()": 
       tempActualResult = ((WebElement) actualResult).isSelected(); 
       break; 
      case "isenabled": 
      case "isenabled()": 
       tempActualResult = ((WebElement) actualResult).isEnabled(); 
       break; 
      case "text": 
       tempActualResult = ((WebElement) actualResult).getText(); 
       break; 
      default: 
       tempActualResult = ((WebElement) actualResult).getAttribute(attributeToCheck); 
     } 

     Assert.assertThat(testLabel + " " + errorMessage, tempActualResult, testToRun); 
    } 

不過,我想知道,將匹配器是能夠弄清楚實際結果是一個字符串還是布爾值?或者它會一直失敗,因爲兩個對象都被比作對象,而不是字符串(不知道底層代碼是如何執行的)。

如何正確處理這種情況的任何意見將不勝感激!

只給一些背景 - 我目前正在寫這樣的代碼:

Switch(whatToTest){ 
    case "eye portfolio tests": 
    customCheckErrorMessages.add("The Eye Portfolio header doesn't match expected user's value"); 
    customCheckActualResults.add(eyePortfolioPage.eyePortfolioAccountHeader); 
    customCheckExpectedResults.add(holder); 
    customCheckTests.add(containsString(holder)); 
    customTestAttributeToTest.add("text"); 
    break; 
    //just showing what my step looks like 
    } 

String actualResult = ""; 
     for(int x = 0; x < customCheckErrorMessages.size(); x++){ 
      try{ 
       switch (customTestAttributeToTest.get(x)){ 
        case "text": 
         actualResult = customCheckActualResults.get(x).getText(); 
         break; 
        default: 
         actualResult = customCheckActualResults.get(x).getAttribute(customTestAttributeToTest.get(x)); 
         break; 
       } 
      }catch (IndexOutOfBoundsException e){ 
       //Assuming this field wasn't actually entered. Defaulting to getText 
       actualResult = customCheckActualResults.get(x).getText(); 
      } 

      System.out.println("Additional Test #" + (x+1)); 

      Assert.assertThat(customCheckErrorMessages.get(x) + " Expected: \"" + customCheckExpectedResults.get(x) 
        + "\", Actual: \"" + customCheckActualResults.get(x).getText().trim(), 
        actualResult.trim(), 
        customCheckTests.get(x)); 
     } 

我想這樣寫代碼:

switch(whatIWantToTest){ 
    case "transaction tests": 
    customTests.add(new TestClass("There should be at least one transaction appearing on the page", //error Message 
         "Looking For Transactions:", //Test Label 
         myOrdersPage.transactions.size(), //Actual Result 
         greaterThanOrEqualTo(1))); //Test to run (should contain expected result) 
//Just showing what my step looks like 
} 

for (TestClass test : customTests){ 
      test.runTest(); 
     } 
+0

我真的不明白你想要做什麼。你爲什麼要圍繞簡單的測試打包?如果您確實想創建一個運行斷言的方法,請爲每種斷言創建1個方法(1個方法聲明它被顯示,1個方法斷言它包含X的文本)等等。我在這裏看到的只是一層過多的代碼。 –

+0

@MrTi - 我正在做這個,所以我的Cucumber Step功能可以快速連續運行我所有的測試。它的主要是我的測試代碼更容易閱讀 – Jason

+0

更新的問題給你 – Jason

回答

0

這裏是我決定使用的代碼。如果任何人有更好的建議,我很想聽聽吧:)

private String testType; 
public String errorMessage, testAttribute; 
public WebElement actualResult; 
public List<WebElement> actualResults; 
public String providedStringValue = null; 
public Boolean providedBooleanValue = null; 
public Matcher testToRun; 
public int attempts = 1; 

//Empty Constructor 
public TestCase() { 
    errorMessage = ""; 
    testType = ""; 
    actualResult = null; 
    actualResults = null; 
    testToRun = null; 
    providedStringValue = null; 
    providedBooleanValue = null; 
} 

//Run Test as a String check 
public TestCase(String errorMessage, String providedResult, Matcher testToRun){ 
    this.errorMessage = errorMessage; 
    providedStringValue = providedResult; 
    testType = "String"; 
    this.testToRun = testToRun; 
} 

//Run Test as a Boolean check 
public TestCase(String errorMessage, Boolean providedResult, Matcher testToRun){ 
    this.errorMessage = errorMessage; 
    providedBooleanValue = providedResult; 
    testType = "Boolean"; 
    this.testToRun = testToRun; 
} 

//Run Test on a WebElement 
public TestCase(String errorMessage, String testAttribute, WebElement actualResult, Matcher testToRun) { 
    this.errorMessage = errorMessage; 
    testType = "WebElement"; 
    this.testAttribute = testAttribute; 
    this.actualResult = actualResult; 
    this.testToRun = testToRun; 
} 

//Run Test on a List<WebElement> 
public TestCase(String errorMessage, String testAttribute, List<WebElement> actualResults, Matcher testToRun) { 
    this.errorMessage = errorMessage; 
    testType = "List"; 
    this.testAttribute = testAttribute; 
    this.actualResults = actualResults; 
    this.testToRun = testToRun; 
} 

public void clearTest() { 
    errorMessage = ""; 
    testType = ""; 
    testAttribute = ""; 
    actualResult = null; 
    actualResults = null; 
    testToRun = null; 
    providedStringValue = null; 
    providedBooleanValue = null; 
} 

public void runTest() { 
    try { 
     if (testType.equalsIgnoreCase("WebElement")) { 
      runTestAsWebElement(testAttribute); 
     } else if (testType.equalsIgnoreCase("List")) { 
      runTestAsList(testAttribute); 
     } else if(testType.equalsIgnoreCase("Boolean")){ 
      runTestAsBooleanCheck(); 
     } else if(testType.equalsIgnoreCase("String")){ 
      runTestAsStringCheck(); 
     } else { 
      Assert.fail("Don't know how to run this test type"); 
     } 
    } catch (StaleElementReferenceException e) { 
     if (attempts < 5) { 
      System.out.println("StaleElementReferenceException - Running test again"); 
      attempts++; 
      runTest(); 
      return; 
     } else { 
      throw e; 
     } 
    } 
    attempts = 1; 
} 

private void runTestAsStringCheck(){ 
    Assert.assertThat(errorMessage, providedStringValue, testToRun); 
} 

private void runTestAsBooleanCheck(){ 
    Assert.assertThat(errorMessage, providedBooleanValue, testToRun); 
} 

//Sometimes the actualResult is a WebElement which means the value to check is wrapped within another value. 
private void runTestAsWebElement(String attributeToCheck) { 
    Object actualResultHolder; 

    switch (attributeToCheck.toLowerCase()) { 
     case "exists": 
     case "present": 
      try{ 
       actualResult.isDisplayed(); //Forcing WebElement to look for element. If throws exception, element not found 
       actualResultHolder = true; 
      } catch (NoSuchElementException e){ 
       actualResultHolder = false; 
      } 
      break; 
     case"display": 
     case "displayed": 
     case "isdisplayed()": 
     case "isdisplayed": 
      try{ 
       actualResultHolder = actualResult.isDisplayed(); 
      } catch (NoSuchElementException e){ 
       System.out.println("Element not found"); 
       throw e; 
       //actualResultHolder = false; 
      } 
      break; 
     case "selected": 
     case "isselected": 
     case "isselected()": 
      actualResultHolder = actualResult.isSelected(); 
      break; 
     case "enabled": 
     case "isenabled": 
     case "isenabled()": 
      actualResultHolder = actualResult.isEnabled(); 
      break; 
     case "text": 
      actualResultHolder = actualResult.getText().trim(); 
      break; 
     default: 
      if (attributeToCheck.contains("css: ")) {//In case we want to test the css attribute instead of the HTML attribute 
       attributeToCheck = attributeToCheck.split("css: ")[1]; 
       actualResultHolder = actualResult.getCssValue(attributeToCheck); 
      } else { 
       actualResultHolder = actualResult.getAttribute(attributeToCheck); 
      } 
    } 

    Assert.assertThat(errorMessage + "\n Actual Result = \"" + actualResultHolder + "\" ", actualResultHolder, testToRun); 
} 

private void runTestAsList(String attributeToCheck) { 
    switch (attributeToCheck.toLowerCase()) { 
     case "size": 
     case "size()": 
      Assert.assertThat(errorMessage, actualResults.size(), testToRun); 
      break; 
     default: 
      //do nothing - Test has to do with the list of elements. 
      Assert.assertThat(errorMessage, actualResults, testToRun); 
    } 
} 

然後,如果出於某種原因,我需要一些其他類型的測試,我只是把它添加到類。

相關問題