Verification code samples (Java SDK)
Basic verification example
The following example uses the Verify class to verify navigation to a page on a website.
@Test public void verify_linkNavigatesToLaptopsPage() throws Exception { //Launch the Chrome browser. Browser browser = BrowserFactory.launch(BrowserType.CHROME); try{ //Navigate to the New Tours website. browser.navigate("http://www.advantageonlineshopping.com/#/"); //Click the "LAPTOPS" link browser.describe(Link.class, new LinkDescription.Builder() .tagName("SPAN").innerText("LAPTOPS").build()).click(); //Wait for the browser to complete the navigation. browser.sync(); //Verify that the correct Web page opens. Verify.areEqual("http://www.advantageonlineshopping.com/#/category/Laptops/1", browser.getURL(), "verify_linkNavigatesToLaptopsPage", "Verify that the browser's URL is as expected."); } catch(Error e){ Reporter.reportEvent("verify_linkNavigatesToLaptopsPage","Failed during test",Status.Failed, e); throw e; } finally{ browser.close(); } }
Custom verification example
The following example shows how to use a custom verification method that verifies a sum of numbers. In this case, it is used to sum the price of two items in a shopping cart in a retail application.
The method receives three parameters: the two numbers to sum and the total.
The method is called in the test script, and the arguments passed are fetched from the application using OpenText Functional Testing for Developers API.
// The example below shows usage of verification extensibility - implementing custom verifications @Test public void testTotalPrice() throws ReportException { long price1; long price2; long totalPrice; // . // . // This is where you add code to: add two items to the shopping cart, find their price, // and update each item's price in the price1 and price2 variables. price1 = 100; price2 = 200; totalPrice = 300; // Then go to the check-out page, find the total price, add update the totalPrice variable with this value. // . // . // The following code calls the custom verification method verifyTotalSumOfNumbers. // The verification name and description are set and then passed as arguments along with the price arguments. String name = "Verify Total Price"; String verificationDescription = "Verify that the total purchase price is indeed a sum of the items in the cart"; verifyTotalSumOfNumbers(price1, price2, totalPrice, name, verificationDescription); // . // . // Continue test logic // . // . } // This is the implementation of the custom verification method protected void verifyTotalSumOfNumbers(long num1, long num2, long total, String name, String verificationDescription) throws ReportException{ // Create a VerificationData object and set its name, description and operation. VerificationData verificationData = new VerificationData(); verificationData.name = name; verificationData.description = verificationDescription; verificationData.operationName = "totalSumOfNumbers"; // Operation name helps better categorize the check made. // For example, for the same verification name ("Verify Total Price") you could also have // the “checkCurrency” operation (in another VerificationData object). // Create the verification parameters, set their label and values. VerificationParameter verificationParameter1 = new VerificationParameter("First Number", num1); VerificationParameter verificationParameter2 = new VerificationParameter("Second Number", num2); VerificationParameter verificationParameter3 = new VerificationParameter("Actual Sum", total); // Add the verification parameters to the parameters list of the verificationData object. verificationData.verificationParameters.add(verificationParameter1); verificationData.verificationParameters.add(verificationParameter2); verificationData.verificationParameters.add(verificationParameter3); // Call a custom comparison method boolean sumMatches = (num1+num2 == total); // Check whether the sums match. If they do, return reportVerification with a passes status. // If not, return it with a failed status. The status is displayed in the test report. if (sumMatches){ Reporter.reportVerification(Status.Passed, verificationData); } else{ Reporter.reportVerification(Status.Failed, verificationData); } } }
See also: