所以我想创建一个" 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 :(得分: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);
}
}
然后,如果由于某种原因我需要其他类型的测试,我只需将它添加到课程中。