Monday 1 December 2014

Hard Assertion - Assert.assertEquals(actual, expected) assertion in selenium webdriver





-> This assertion used to compare actual and expected value.
-> If both the values are same means its continue the Execution.
-> IF both the values different means it will mark specifi method as fails immediately and exit from the test method.


You can use different types of values in actual and expected like boolean, byte[], char, double, float, int, etc..


Example :

package TestNG_Package;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.testng.Assert;

public class TestNG_Test1 {

WebDriver driver1=new FirefoxDriver();

@BeforeMethod
public void OpenBrowser(){
driver1.get("http://google.com");
driver1.manage().window().maximize();
}

@AfterMethod
public void CloseBrowser(){
driver1.quit();
}


@Test
public void Assertion_method() {
String title = driver1.getTitle();
Assert.assertEquals(title,"Home Page");
System.out.println("title"+title);
}

@Test
public void Verification_method() {
String title = driver1.getTitle();
if(title=="Home Page)
System.out.println("title"+title);
else
Syste,.out.println("Different Title");
}

}


From above Example,

-> Assertion_Method will get fail and execution goes to next method if title is not equal to "Home Page". The remaining part in method wont be executed.

-> Assertion_Method will get pass and execution goes to next method if title is equal to "Home Page". The remaining part in method will be executed.

-> Verification_Method will run completely if title have any values. Its just verify the values. Its not terminate tthe process.


No comments:

Post a Comment