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();
}
我真的不明白你想要做什麼。你爲什麼要圍繞簡單的測試打包?如果您確實想創建一個運行斷言的方法,請爲每種斷言創建1個方法(1個方法聲明它被顯示,1個方法斷言它包含X的文本)等等。我在這裏看到的只是一層過多的代碼。 –
@MrTi - 我正在做這個,所以我的Cucumber Step功能可以快速連續運行我所有的測試。它的主要是我的測試代碼更容易閱讀 – Jason
更新的問題給你 – Jason