How to use Assert Statment using Selenium
Posted By : Dinesh Dhiman | 28-Apr-2015
We use Assert statments while testing to verify the output of test case against a predefined set of Test Data .
We use Assert statments for:-
-
Verify whether an object is visible (such as a button, link, etc)
-
Verify whether a checkbox is checked or unchecked.
-
Check if an object is present on the screen
-
Check if an Editbox is editable (i.e. we can key in values into edit box)
-
Check if a particular value is selected in the dropdown box
Note : - If you use assert in your tests then the test will be aborted if the assert fails. Next test case will start executing in case you are running a test suite.
There are some assert statements
testNG
1. Assert.assertEquals(actual, expected, message);
2. Assert.assertNotEquals(actual1, actual2, message);
3. Assert.assertTrue(condition);
4. Assert.assertSame(actual, expected, message);
5. Assert.assertNotSame(actual, expected);
6. Assert.assertNotNull(object, message);
jUnit
1. Assert.assertArrayEquals(expecteds, actuals);
2. Assert.assertEquals( expected, actual);
3. Assert.assertFalse(condition);
4. Assert.assertNotEquals(first, second);
5. Assert.assertTrue(condition);
6. Assert.assertNull(message, object);
How to use asserts statements in our testcases
-
Make a java project
-
Add all selenium jar files
-
Make your test case using testNG class or u can also use jUnit for testcases
(I will demonstrate that example using testNG class)
package com.oodles;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class AssertTestCases {
String url=”any url ”;
WebDriver firefoxDriver = new FirefoxDriver();
@Test
public void assertExample() {
Assert.assertEquals("here you enter the title of you web application ", firefoxDriver.getTitle());
firefoxDriver.findElement(By.linkText("Login")).click();
WebElement login =firefoxDriver.findElement(By.id("login-form"));
if (Assert.assertTrue(login.isDisplayed())){
firefoxDriver.findElement(By.name("email")).sendKeys("your email id ");
firefoxDriver.findElement(By.id("login_password")).sendKeys(" your password ");
login.submit();
Assert.assertEquals(msg.getText(), "Please enter a valid email id.");
}
}
@BeforeTest
public void beforeTest() {
firefoxDriver.get(url);
}
@AfterTest
public void afterTest() {
firefoxDriver.close();
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Dinesh Dhiman
Dinesh is good in java and grails with having extra knowledge in AngularJS , HTML ,SQL , Selenium