How to verify the Results?
Why Assertion and how Selenium support Assertion?
Why Assertion and how Selenium support Assertion?
- Assertion is needed to verify that web application which consist of UI elements such as text menu, check box, button, combo box ,text field are correct or not
- It is used to verify Actual and Expected Output
- Assert Class is provided by TestNG which has different assert method available e.g.
- assertEquals(String Actual, String Expected):-It takes two string arguments and checks whether both are equal or not , if not it will fail the test
- assertTrue(condition)- It takes one Boolean argument and checks if condition is true, If it isint an Assertion Error is thrown
- assertFalse(condition)- It takes one Boolean argument and checks if condition is false, If it isint an Assertion Error is thrown
The following code will verify "Create An Account" text present on the facebook page
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.asserts.*;
public class Assert {
@Test
public void Ashishmthod()
{
WebDriver a= new FirefoxDriver();
a.manage().window().maximize();
a.get("http://www.facebook.com/");
String h= a.findElement(By.xpath(".//div[@class='mbs _52lq fsl fwb fcb']/span")).getText();
System.out.print(h);
org.testng.Assert.assertEquals(h, "Create an account");
}
}
Notes
1- Webdriver provides a getText() method to fetch the text present on the desired location of the page
2- Actual Text is stored in String "s"
3- Assert Equals method of Asset Class compares Actual text to the Expected Text
Output
0 comments:
Post a Comment