Tutorials Hut

  • Selenium WebDriver

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report



  • How to use Assertions in selenium Web Driver?

    This article will present you with a complete idea about  Assertions and Assertions in selenium and its type in Selenium WebDriver.

    We will learn below topics in this article

    What is Assertion?

    Assertions can be handled with the help of the predefined method of JUnit and TestNG frameworks.

    Assertions are used for validating a test case and based on it’s result test case has passed or failed

    Whenever  we  automate a web application using Selenium, we have to verify if the test case is working as expected or not , based on it’s result test case passes or fails.

    Selenium - TestNg Assertions

    Type of Assertion

    There are two types of assertion available in selenium WebDriver:

      • Soft Assert
      • Hard Assert

    1) Hard Assert

    Whenever we  write a Hard assert statement in our test scripts and if any assert statement fails then it throws an exception immediately and continues with the next test in the test suite.In case of an assertion error, it will throw the “java.lang.AssertionError” exception.

        • AssertEquals
        • AssertNotEquals
        • AssertTrue
        • AssertFalse
        • AssertNull
        • AssertNotNull
    AssertEquals

    AssertEquals is a method which takes a minimum of two arguments and compares actual results with expected results. 

    If actual and expected results are the same, then the assertion passes with no exception and the test case is marked as “passed”. 

    If both the actual and expected results are not the same, then the assertion fails with an exception and the test case is marked as “failed”. 

    Syntax:

    Assert.assertEquals(actual,expected);

    //Code Snippet

    package com.test;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.chrome.ChromeDriver;
    
    import org.testng.Assert;
    
    import org.testng.annotations.Test;
    
    public class HardAssertionsExample {
    
    @Test
    
    public void testAssertFunctions() {
    
    System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe");
    
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("https://tutorialshut.com/");
    
    String ActualTitle = driver.getTitle();
    
    String ExpectedTitle = "Tutorials Hut -Free Tutorials and Software Testing material";
    
    Assert.assertEquals(ActualTitle, ExpectedTitle);
    
    System.out.println("Next step after assertion");
    
    driver.close();
    
    }
    
    }
    

    Result

    Next step after assertion

    PASSED: testAssertFunctions..

    ===============================================

        Default test

        Tests run: 1, Failures: 0, Skips: 0

    ===============================================

    AssertNotEquals

    It  is a method that does the opposite of the assertEquals() method.

    AssertNotEquals() is a method used to compare the actual and expected results.

    If actual and expected results are not the same, then the assertion passes with no exception and the test case is marked as “passed”. 

    If actual and expected results are the same, then the assertion fails with an exception and the test case is marked as “failed”.

    Syntax:

    AssertNotEquals(actual,expected);  

    //Code Snippet

    package com.test;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.chrome.ChromeDriver;
    
    import org.testng.Assert;
    
    import org.testng.annotations.Test;
    
    public class HardAssertionsExample {
    
    @Test
    
    public void testAssertFunctions() {
    
    System.setProperty("webdriver.chrome.driver", "D:\\Drivers\\chromedriver.exe");
    
    WebDriver driver = new ChromeDriver();
    
    driver.navigate().to("https://tutorialshut.com/");
    
    String ActualTitle = driver.getTitle();
    
    String ExpectedTitle = "Tutorials Hut -Free Tutorials and Software Testing material";
    
    Assert.assertNotEquals(ActualTitle, ExpectedTitle);
    
    System.out.println("Next step after assertion");
    
    driver.close();
    
    }
    
    }
    

    Result-

    FAILED: testAssertFunctions

    java.lang.AssertionError:…

    ===============================================

        Default test

        Tests run: 1, Failures: 1, Skips: 0

    ===============================================

    AssertTrue

    AssertTrue is used to verify if a given Boolean condition is true. 

    This assertion returns true if the specified condition passes and the test case is marked as “passed”. 

    if not, then the assertion fails with an exception and the test case is marked as “failed”.

    Syntax

    Assert.AssertTrue(condition);  

    //Code Snippet
    
    package com.test;
    
    import org.testng.Assert;
    
    import org.testng.annotations.Test;
    
    public class DemoForAll {
    
    @Test
    
    public void testAssertFunctions() {
    
    System.out.println(" step Before assertion");
    
    Assert.assertTrue(true);
    
    System.out.println("Next step after assertion");
    
    }
    
    }
    

    Result

    step Before assertion

    Next step after assertion

    PASSED: testAssertFunctions

    ===============================================

        Default test

        Tests run: 1, Failures: 0, Skips: 0

    ===============================================

    AssertFalse

    AssertFalse is basically opposite to assertTrue. 

    AssertFalse is used to verify if a given Boolean condition is false. 

    When a condition value is true, the assertion aborts the method by an exception and if the condition returns false then test cases are passed.

    Syntax:

    Assert.AssertFalse(condition);  

    //Code Snippet

    package com.test;
    
    import org.testng.Assert;
    
    import org.testng.annotations.Test;
    
    public class DemoAssertionsl {
    
    @Test
    
    public void testAssertFunctions() {
    
    System.out.println(" step Before assertion");
    
    Assert.assertFalse(true);
    
    System.out.println("Next step after assertion");
    
    }
    
    }
    
     

    Result-

     step Before assertion

    FAILED: testAssertFunctions

    java.lang.AssertionError: expected [false] but found [true]

    ……..

    ===============================================

        Default test

        Tests run: 1, Failures: 1, Skips: 0

    ===============================================

    AssertNull

    This assertion checks if the object is null or not. 

    It aborts the test case if the object is null and gives an exception.

    Syntax:

    Assert.assertNull(object);

    //Code Implementation

    package com.test;
    
    import org.testng.Assert;
    
    import org.testng.annotations.Test;
    
    public class DemoHardAssertions{
    
    @Test
    
    public void testAssertFunctions() {
    
    System.out.println(" step Before assertion");
    
    Assert.assertNull(null);
    
    System.out.println("Next step after assertion");
    
    }
    
    }
    
    

    Result

    step Before assertion

    Next step after assertion

    PASSED: testAssertFunctions

    ===============================================

        Default test

        Tests run: 1, Failures: 0, Skips: 0

    AssertNotNull

    This Assertion Verifies whether the object is null or not. 

    If an object is not null, then assertion passes the test case and test case is marked as “passed”,

    if an object is null, then assertion aborts the test case and test case is marked as “failed”

    Syntax:

    Assert.assertNotNull(object);

    //Code Implementation

    package com.test;
    
    import org.testng.Assert;
    
    import org.testng.annotations.Test;
    
    public class DemoHardAssertion{
    
    @Test
    
    public void testAssertFunctions() {
    
    System.out.println(" step Before assertion");
    
    Assert.assertNotNull(null);      //test case will fail
    
    System.out.println("Next step after assertion");
    
    }
    
    }
    
    

    Result

    step Before assertion

    FAILED: testAssertFunctions

    java.lang.AssertionError: expected object to not be null

    ………

    ===============================================

        Default test

        Tests run: 1, Failures: 1, Skips: 0

    ===============================================

    2) Soft Assert

    Till now we have learnt the hard assertions so In a hard assertion, when assertion fails it terminates the test 

    If we want to run the whole test case and report the failure in testNg report and we don’t want our test terminates in the middle if any of the assertion fails then we use Soft Assertions

    Soft Assert does not throw an exception or terminates in the middle when an assert fails and our test execution proceeds further with the next steps

    To perform Soft Assertion we need to call assertAll() method compulsorily

    Lets understand how it works by below examples

    //Code Implementation

    package com.test;
    
    import org.testng.annotations.Test;
    
    import org.testng.asserts.SoftAssert;
    
    public class SoftAssertDemo {
    
    @Test
    
    public void softAssertionTest(){
    
    //Creating softAssert object
    
    SoftAssert softAssert = new SoftAssert();
    
    softAssert.assertEquals("Selenium", "Selenium", "1st Soft assert passed");
    
    System.out.println("First soft asssertion passed");
    
    softAssert.assertTrue("Selenium".equals("selenium"), "2nd Soft assert failed");
    
    System.out.println("Second soft assertion failed");
    
    softAssert.assertTrue("Testing".equals("testingggg"), "3rd soft assert failed");
    
    System.out.println("Third soft assertion failed");
    
    //Collates the assertion results and marks test as pass or fail
    
    softAssert.assertAll();
    
    }
    
    }
    

    Result

    Selenium - Assertions in Selenium- Hard and Soft Assertions



  • Selenium WebDriver Tutorials

       Selenium Introduction
       Benefits of Selenium
       Four components of Selenium
       Difference b/w Selenium IDE, RC & WebDriver
       Selenium WebDriver Architecture
       Background when user execute selenium code
       Download and Install Java
       Download and Install Eclipse
       Download Selenium WebDriver
       Selenium WebDriver Locators
       Selenium - Launch Browser
       Selenium WebDriver Waits
       Selenium- Implicit wait
       Selenium- Explicit wait
       Selenium- Fluent wait
       Selenium- Commonly used commands
       Selenium- findElement & findElements
       Selenium- Selenium-Handling check Box
       Selenium- Handling Radio button
       Selenium- Handling drop down
       Selenium- Take Screenshot
       Selenium- Handle Web Alerts
       Selenium- Multiple Windows Handling
       Selenium- Handle iframes
       Selenium- Upload a file
       Selenium- Download a file
       Selenium- Actions Class Utilities
       Selenium- Mouse Actions
       Selenium- Keyboards Events
       Selenium- Handle mouse hover Actions
       Selenium- Drag and Drop
       Selenium- Scroll a WebPage
       Selenium- Context Click / Right Click
       Selenium- Double Click
       Selenium- Desired Capabilities
       Selenium- Assertions
       Selenium- Exceptions and Exception Handling
       Selenium- Difference b/w driver.close() & driver.quit()
       Selenium- difference b/w driver.get() & driver.navigate()
       Selenium- JavascriptExecutor
       Selenium- Read excel file using Fillo API
       Selenium- Database Testing using Selenium
       Selenium- Read & write excel file using Apache POI
       Selenium- Read and Write csv file in Selenium
       Selenium- Dynamic Web Table Handling
       Selenium- Maven Integration with Selenium
       Selenium- Set up Logging using Log4j
       Selenium-Implement Extent Report


































  • Leave a Reply

    Your email address will not be published. Required fields are marked *

    Scroll to Top