2016-07-19 10 views
0
public void verifyErrorMsgforInvalidUserTransPosting(
{ 
String errorMSG="You dont have permission to create transaction using this ID"; 
String errorMSGSYS=driver.findElement(By.xpath("html/div/.....").getText(); 

if(errorMSGSYS.equals(errorMSG)) 
    { 
     System.out.println("System didnt allowed user to post the Transaction"); 
     report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System didnt allowed user to post the Transaction", Status.SCREENSHOT); 
    } 
    else 
    { 
     System.out.println("System is allowing user to post the Transaction which is NOT expected behavior"); 
     report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System is allowing user to post the Transaction which is NOT expected behavior", Status.FAIL); 
    } 
} 

In dem obigen Code, wenn (Bedingung 1) funktioniert, wenn das Element gefunden wird, und wenn die condition1 erfüllt else-Anweisung funktioniert, wenn eine Nichtübereinstimmung gibtHandhabung try catch für eine if-Anweisung in Selenium WebDriver

ist

Könnte mir jemand helfen, wie kann ich mit dem else umgehen, wenn das Element NICHT mit try catch oder einer anderen Methode gefunden wurde?

Antwort

0

Ist das etwas nach dem, wonach Sie suchen?

public void verifyErrorMsgforInvalidUserTransPosting(
{ 
    String errorMSG="You dont have permission to create transaction using this ID"; 
    Boolean isPresent = driver.findElements(By.xpath("html/div/.....").size() > 0; 

    if(!isPresent) 
    { 
     System.out.println("Failed to find element"); 
     return; 
    } 

    String errorMSGSYS = driver.findElement(By.xpath("html/div/.....").getText();  
    if(errorMSGSYS.equals(errorMSG)) 
    { 
     System.out.println("System didnt allowed user to post the Transaction"); 
     report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System didnt allowed user to post the Transaction", Status.SCREENSHOT); 
    } 
    else 
    { 
     System.out.println("System is allowing user to post the Transaction which is NOT expected behavior"); 
     report.updateTestLog("Verify System is NOT allowing user to post transaction for an invalid user", "System is allowing user to post the Transaction which is NOT expected behavior", Status.FAIL); 
    } 
} 
+0

Vielen Dank .... Das hat für mich funktioniert ... Happy :) :) :) – Meghasri

0

Ich würde Dinge ein wenig anders machen. Sie haben einige Saiten, die mehrere Stellen wiederholen, also lege ich sie alle oben. Ich denke, es macht den Code leichter zu lesen. Ich kümmerte mich auch darum, sicherzustellen, dass das gewünschte Element vorhanden ist, bevor ich den Textwert überprüfe.

public void verifyErrorMsgforInvalidUserTransPosting() 
{ 
    String testDescription = "Verify System is NOT allowing user to post transaction for an invalid user"; 
    String expectedCondition = "System didnt allowed user to post the Transaction"; 
    String unexpectedCondition = "System is allowing user to post the Transaction which is NOT expected behavior"; 
    String errorMSG = "You dont have permission to create transaction using this ID"; 
    List<WebElement> error = driver.findElements(By.xpath("html/div/.....")); 

    if (error.isEmpty()) 
    { 
     System.out.println("Failed to find element"); 
     return; 
    } 

    String errorMSGSYS = error.get(0).getText(); 
    if (errorMSGSYS.equals(errorMSG)) 
    { 
     System.out.println(expectedCondition); 
     report.updateTestLog(testDescription, expectedCondition, Status.SCREENSHOT); 
    } 
    else 
    { 
     System.out.println(unexpectedCondition); 
     report.updateTestLog(testDescription, unexpectedCondition, Status.FAIL); 
    } 
} 

HINWEIS: Ich habe bemerkt, dass Ihre Probe XPath mit dem HTML Tag gestartet. Ich hoffe, dass Sie keinen Code haben, der das tatsächlich tut ... es ist eine wirklich, wirklich schlechte Idee, weil es den XPath sehr zerbrechlich macht. Jede Änderung in der HTML-Struktur wird diesen Locator brechen. Ich würde vorschlagen, dass Sie nach Wegen suchen, Elemente viel näher am gewünschten Element zu finden, ohne alle relativen Pfade zu verwenden.

+0

Danke Jeff. Ich habe es auch mit dem obigen Code versucht. Das funktioniert auch für mich. Mein tatsächlicher Code beginnt nicht mit html..Dies ist nur als Referenz, die ich erwähnt habe. Danke für deinen Rat. Es hilft mir in Zukunft :) :) – Meghasri

0

Denken Sie daran, driver.findElements() kann Elemente zurückgeben, die derzeit nicht sichtbar sind (wenn Sie mehrere Seiten haben). Daher kann dies ein Problem sein.

Ich würde folgende empfehlen:

try 
    { 
     String errorMSGSYS=driver.findElement(By.xpath("html/div/.....").getText(); 
    } 
    catch(Exception e) 
    { 
     //code to handle "element not found" 
    } 

    //normal flow as earlier 

In diesem Fall, wenn das Element gefunden wird, Ihr normaler Fluss funktioniert wie erwartet. Wenn das Element nicht gefunden wird, löst findElement eine Ausnahme aus, die im catch-Block gefangen wird. Sie können den Fluss wie gefordert abwickeln.

Hoffe, das hilft.