Monday 1 December 2014

Hard Assertion - assertNotNull(object) in webdriver Assertion



-> assertNotNull(object) assertion will check and verify that object is NOT NULL.

-> It will pass if object returns any value. That means without Null. and continue the execution. it will mark that specific test method as fail in TestNG result.

-> It will fail if object returns NULL.  and continue the execution in method.



Example :

Let us take example like we have two text box. one is Enabled in application. Second is disabled in application.



 WebElement txt1, txt2;
 @BeforeClass
 public void load_url(){
  driver.get("google.com");
  txt1 = driver.findElement(By.xpath("//input[@id='text1']"));
  txt2 = driver.findElement(By.xpath("//input[@id='text2']"));
 }

 //Example Of Assertion Method - will Pass
 @Test
 public void null1() {
  System.out.print("\n"+txt1.getAttribute("disabled"));
  Assert.assertNOTNull(txt1.getAttribute("disabled"));
 }

 //Example Assertion Method - will Fail
 @Test
 public void null2() {
  System.out.print("\n"+txt2.getAttribute("disabled"));
  Assert.assertNOTNull(txt2.getAttribute("disabled"));
 }



From the above example


First Text box -> Enabled="True". Its dont have Disabled attribute. So its returns NULL.

Second Text box -> Disabled="True". Its dont have Disabled attribute. So its returns value.


So o/p will be


-> null1() will get fail, because its not have the disabled attribute. So its return Null.

-> null2() will get pass, because its have the disabled attribute. So its return sone value. not return Null

Hard Assertion - assertNull(object) in Webdriver Assertion




-> assertNull(object) assertion will check and verify that object is NULL.

-> It will pass if object returns Null. and continue the execution

-> It will fail if object returns any value. then it will return error message like "java.lang.AssertionError: expected [null] but found [true]". Whenever your assertion fails, it will mark that specific test method as fail in TestNG result.



Example :

Let us take example like we have two text box. one is Enabled in application. Second is disabled in application.


   WebElement txt1, txt2;
 @BeforeClass
 public void load_url(){
  driver.get("google.com");
  txt1 = driver.findElement(By.xpath("//input[@id='text1']"));
  txt2 = driver.findElement(By.xpath("//input[@id='text2']"));
 }

 //Example Of Assertion Method - will Pass
 @Test
 public void null1() {
  System.out.print("\n"+txt1.getAttribute("disabled"));
  Assert.assertNull(txt1.getAttribute("disabled"));
 }

 //Example Assertion Method - will Fail
 @Test
 public void null2() {
  System.out.print("\n"+txt2.getAttribute("disabled"));
  Assert.assertNull(txt2.getAttribute("disabled"));
 }



From the above example


First Text box -> Enabled="True". Its dont have Disabled attribute. So its returns NULL.

Second Text box -> Disabled="True". Its dont have Disabled attribute. So its returns value.


So o/p will be


-> null1() will get pass, because its not have the disabled attribute. So its return Null.

-> null2() will get fails, because its have the disabled attribute. So its return sone value. not return Null

Hard Assertion - assertFalse(condition) Assertion in Selenium Webdriver



-> assertTrue assertion is generally used for when boolean condition false.

-> It will pass if condition returns "false". If it will return true then it will fail and skip test execution from that specific method.

-> Syntax for assertFalse assertion is as bellow.

Assert.assertFalse(condition);


Example:

Suppose you want to continue the process if checkbox Unchecked/unselect.



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();
WebElement check1;

@BeforeMethod
public void OpenBrowser(){
driver1.get("http://google.com");
check1 = driver1.findElement(By.xpath("//input[@name='option1']"));
driver1.manage().window().maximize();
}

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


@Test
public void Assertion_method1() {
String title = driver1.getTitle();
Assert.assertFalse(check1.isSelected());
System.out.println("title"+title);
}


}


From above Example:

-> Assertion_method1 will get pass and complete all steps if select the check box and return the condition value as False .

-> Assertion_method1 will get fail and skip all steps if unselect the check box and return the condition value as True .

Hard Assertion - assertTrue(condition) Assertion in Selenium Webdriver



-> assertTrue assertion is generally used for boolean condition true.

-> It will pass if condition returns "true". If it will return false then it will fail and skip test execution from that specific method.

-> Syntax for assertTrue assertion is as bellow.

Assert.assertTrue(condition);


Example:

Suppose you want to continue the process if checkbox Selected/Checked.



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();
WebElement check1;

@BeforeMethod
public void OpenBrowser(){
driver1.get("http://google.com");
check1 = driver1.findElement(By.xpath("//input[@name='option1']"));
driver1.manage().window().maximize();
}

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


@Test
public void Assertion_method1() {
String title = driver1.getTitle();
Assert.assertTrue(check1.isSelected());
System.out.println("title"+title);
}


}


From above Example:

-> Assertion_method1 will get pass and complete all steps if select the check box and return the condition value as True .

-> Assertion_method1 will get fail and skip all steps if unselect the check box and return the condition value as False .

This way, Assert.assertTrue(condition) is helps use to assert boolean condition true.

Hard Assertion - Assert.assertNotEquals(actual, expected, message) assertion in Selenium Webdriver



-> It's function is opposite to assertEquals() assertion.

-> Means if both sides values will not match then this assertion will pass else it will fail.

-> Here you can write your own message for failure condition



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_method1() {
String title = driver1.getTitle();
Assert.assertNotEquals(title,"Home Page","Assertion_method1 has been Executed");
System.out.println("title"+title);
}


}



From above Example,

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

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

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.


Hard Assertion and Soft Assertion in Selenium webdriver using TestNG framework




-> Assertion used to verify the result. and if result is not as our expected means its stopped the perticular method execution.

-> There are many assertions available in Selenium WebDriver with TestNG framework and we will look at all of then one by one.

-> Assertions are important and very useful in any automation tools to assert something during your test execution.

-> This pass or fail indication will helps you in your testing process. So this is the very good function in selenium
WebDriver which helps us to find and log issue.

-> Use hard assertions in selenium webdriver only when you wants to skip remaining test execution on assertion fail. If you wants to continue execution on assertion failure, you can use TestNg soft assertions.



Difference between Assertion and Verification

Verification and assertion are not euqal. Both are different.

-> Verification is used to verify the result.

-> Assertion also verify the result. If result is not our expected means its stopped the perticular method execution.



How to use Hard Assertion in Selenium Webdriver

-> We can use pre defined class(Assert) for Hard Assertion. We no need to create object.

-> We use Hard Assertion in Selenium Webdriver like below

Assert.assertEquals(actual, expected)
Assert.assertNotEquals(actual, expected, message)
Assert..assertTrue(condition)
Assert.assertFalse(condition)
Assert.assertNull(object)
Assert.assertNotNull(object)



=========================================================\



Soft Assertion 



-> The hard assertion used to mark method as fail if assertion condition get false and the remaining statements inside the method will be aborted.

-> But the soft Assertion used to mark method as fail if assertion condition get false and the remaining statements inside the method will be continue the process. And also its reports them the Assertion get failure.



How to use Soft Assertion in Selenium Webdriver


-> If you will use soft assertion then your test execution will remain continue even If any assertion fails.

-> The Assertion failure will be reported in the report, so that we can verify at the end of the execution.

-> We can use soft assertion when we have multiple assertions need to be run in same method and execute all assertion even if any one of them fails in between.

-> To use testng soft assertion, you have to use testng SoftAssert class. Its declaration like below,

SoftAssert S_assert = new SoftAssert()

The we can use above object (S_assert) for using same methods which using in Hard Assertion like

S_assert.assertEquals(actual, expected)
S_assert.assertNotEquals(actual, expected, message)
S_assert.assertTrue(condition)
S_assert.assertFalse(condition)
S_assert.assertNull(object)
S_assert.assertNotNull(object)


The above methods used for Soft assertion in Selenium webdriver


This SoftAssert class is not throw the exception if assertion failure and recording failure. 

Parameterize with JXL (Data Driven)

parameterize with JXL


@Test
  public void test () throws BiffException, IOException, InterruptedException
  {
  //Open MyDataSheet.xls file from given location.
  FileInputStream fileinput = new FileInputStream("D:\\MyDataSheet.xls");
  //Access first data sheet. getSheet(0) describes first sheet.
  Workbook wbk = Workbook.getWorkbook(fileinput);
  Sheet sheet = wbk.getSheet(0);
 
  //Read data from the first data sheet of xls file and store it in array.
  String TestData[][] = new String[sheet.getRows()][sheet.getColumns()];
 
  //To enable Last Name text box.
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  String toenable = "document.getElementsByName('lname')[0].removeAttribute('disabled');";
  javascript.executeScript(toenable);
   
  //Type data in first name and last name text box from array.
  for(int i=0;i<sheet.getRows();i++)
  {
   for (int j=0;j<sheet.getColumns();j++)
   {
    TestData[i][j] = sheet.getCell(j,i).getContents();
    if(j%2==0)
    {
     driver.findElement(By.xpath("//input[@name='fname']")).sendKeys(TestData[i][j]);
    }
    else
    {
     driver.findElement(By.xpath("//input[@name='lname']")).sendKeys(TestData[i][j]);
    }
   }
   Thread.sleep(1000);
   driver.findElement(By.xpath("//input[@name='fname']")).clear();
   driver.findElement(By.xpath("//input[@name='lname']")).clear();
  }
  Thread.sleep(1000);
 
  }