Guten Morgen,
auch heute würde mich Eure Meinung zu meinem Code interessieren.
Er funktioniert so wie er ist. Die Frage die ich mir Stelle ist, ob man Dinge kürzer schreiben kann (und dann auch sollte.. ohne verlust der Übersicht) und ob der Ansatz mit 3 ähnlichen Methoden so vernünftig ist.
Es handelt sich um Testautomation.
Bin gespant auf Eure Beitrage
Die Methoden erhalten von einer Webseite Attribute die Sie finden sollen.
Die abtrakte Methode getRootTestObject kümmert sich dabei um die höchste Ebene eines Dialoges oder eines HTML Documents.
auch heute würde mich Eure Meinung zu meinem Code interessieren.
Er funktioniert so wie er ist. Die Frage die ich mir Stelle ist, ob man Dinge kürzer schreiben kann (und dann auch sollte.. ohne verlust der Übersicht) und ob der Ansatz mit 3 ähnlichen Methoden so vernünftig ist.
Es handelt sich um Testautomation.
Bin gespant auf Eure Beitrage
Code:
import org.apache.log4j.Logger;
import tchelperclass.util.Log;
import com.rational.test.ft.ObjectNotFoundException;
import com.rational.test.ft.RationalTestException;
import com.rational.test.ft.object.interfaces.TestObject;
import com.rational.test.ft.script.RationalTestScript;
import com.rational.test.ft.value.RegularExpression;
public abstract class FindTestObject extends RationalTestScript {
// **********************************************
// The class defines the methodes to find objects
// **********************************************
protected static final Logger log = Logger.getLogger(FindTestObject.class);
protected static final double timeOut = 15;
protected static final double timeBetweenChecks = 1;
protected TestObject rootTestObject = null;
protected TestObject[] to = null;
protected long start = System.currentTimeMillis();
protected long end;
private int timer = 0;
public abstract TestObject getRootObject();
// **************************************
// Methods that try to get the Testobject
// **************************************
public TestObject getTestObject(String property, String value, String property2, String value2) {
try {
rootTestObject = getRootObject();
while(timer<timeOut) {
// Try to find TestObject until timout
to = rootTestObject.find(atDescendant(property, value, property2,value2));
if (to.length > 2) {
log.error("More than one Object found on website! Methode getTestObject(" + property + "," + value + "," + property2 + "," + value2 + ")");
throw new ObjectNotFoundException("TestObject not found on Website");
}
if (to.length != 0) {
end = System.currentTimeMillis();
log.debug("Testobject found within: " + getElapsedTime(start, end) + " seconds");
start = System.currentTimeMillis();
// Reset timer
timer = 0;
return ((TestObject) (to[0]));
}
unregister(to);
sleep(timeBetweenChecks);
timer++;
System.out.println(timer);
}
log.error("TestObject not found in Website. Methode getTestObject(" + property + "," + value + "," + property2 + "," + value2 + ")");
throw new ObjectNotFoundException("TestObject not found in Website");
} catch (RationalTestException rte) {
log.error("Rational Exception: Methode getTestObject(" + property + "," + value + "," + property2 + "," + value2 + ")");
throw new ObjectNotFoundException("TestObject not found in Website");
}
}
public TestObject getTestObject(String property, String value, String property2, RegularExpression reg) {
try {
rootTestObject = getRootObject();
while (timer < timeOut) {
// Try to find TestObject until timout
to = rootTestObject.find(atDescendant(property, value, property2, reg));
if (to.length > 2) {
log.error("More than one Object found on website. Methode getTestObject(" + property + "," + value + "," + property2 + "," + reg + ")");
throw new ObjectNotFoundException("TestObject not found on Website");
}
if (to.length != 0) {
end = System.currentTimeMillis();
log.debug("Testobject found within: " + getElapsedTime(start, end) + " seconds");
start = System.currentTimeMillis();
// Reset timer
timer = 0;
return ((TestObject) (to[0]));
}
unregister(to);
sleep(timeBetweenChecks);
timer++;
}
log.error("TestObject not found in Website.Methode getTestObject(" + property + "," + value + "," + property2 + "," + reg + ")");
throw new ObjectNotFoundException("TestObject not found in Website");
} catch (RationalTestException rte) {
log.error("Rational Exception: Methode getTestObject(" + property + "," + value + "," + property2 + "," + reg + ")");
throw new ObjectNotFoundException("TestObject not found in Website");
}
}
public TestObject getTestObject(TestObject testObject, String property, String value) {
TestObject childNode = null;
try {
while (timer < timeOut) {
// Try to find TestObject until timeout
childNode = testObject;
to = childNode.find(atDescendant(property, value));
if (to.length > 2) {
log.error("More than one Object found on website. Methode getTestObject(" + testObject + "," + property + "," + value + ")");
throw new ObjectNotFoundException(
"TestObject not found on Website");
}
if (to.length != 0) {
end = System.currentTimeMillis();
log.debug("Testobject found within: " + getElapsedTime(start, end) + " seconds");
start = System.currentTimeMillis();
// Reset timer
timer = 0;
return ((TestObject) (to[0]));
}
unregister(to);
sleep(timeBetweenChecks);
timer++;
}
log.error("TestObject not found in Website. Methode getTestObject(" + testObject + "," + property + "," + value + ")");
throw new ObjectNotFoundException("TestObject not found in Website");
} catch (RationalTestException rte) {
log.error("Rational Exception: Methode getTestObject(" + testObject + "," + property + "," + value + ")");
throw new ObjectNotFoundException(
"getTestObject() - Object not found");
}
}
// Calculate runtime
public String getElapsedTime(long start, long end) {
long calcElpasedTime = (end/1000)-(start/1000);
//long calcElpasedTimeMS = (end)-(start);
String elapsedSec = Long.toString(calcElpasedTime);
//String elapsedMS = Long.toString(calcElpasedTimeMS);
return elapsedSec;
}
}
Die Methoden erhalten von einer Webseite Attribute die Sie finden sollen.
Die abtrakte Methode getRootTestObject kümmert sich dabei um die höchste Ebene eines Dialoges oder eines HTML Documents.