In Selenium Web Driver, Assertions
Introduction
Users can validate any type of test
using assertions. The outcomes of the assertion are based on a comparison of
the actual and anticipated results. Only when there are no exceptions in the
Assertions is a test said to have passed. Thus, using assertions effectively is
essential for finding bugs in the programme under test. This blog aims to shed
light on the numerous Assert statements that the Selenium Automation Testing Web driver supports.
Verify Commands in Selenium vs. Assert
Selenium typically offers two types of
validation checks: one that employs assertions and the other that incorporates
"Verify" statements. Despite the fact that both have the same goal,
there is still a significant variation in the way they perform.
Hard Assertions place tight
restrictions on the test script when they fail, stopping further programme
execution. Verify checks get test results for several conditions even if one of
them fails. Although Soft Assertions operate differently from Hard Assertions
in that normal execution continues even when the asserting script fails, whether
to employ assertions or verify statements depends only on the user's
preferences.
Frequently Made Assertions
Assert Equals
When using Assert Equals, the test
results are given based on a comparison between the Expected condition and the
Actual condition. The employment of assert equals as a validation check to
ensure that the Title of the homepage displayed for the provided site is
correct is demonstrated in the example below.
testCaseVerifyTitlePage() is a public
void.
new FirefoxDriver(), driver;
"https://wiki.perficient.com/confluence/login.action?os
destination=%2Fhomepage.action", driver.navigate().to;
"Log In - Perficient Wiki,"
driver.getTitle(), Assert.assertEquals;
}
Assert
Not Equals
For Testers, "Assert Not
Equals" fulfils the objective of negation testing. The test script passes
if the Actual and Expected do not match; else, it fails. The example that
follows shows how to use "Assert Not Equals" to check the presentation
of text that varies dynamically depending on the date.
testCaseVerifyDate () public void
Actualtext is equal to
driver.findElement(By.xpath("/h3/span")). getText();
"Here comes the surprise gift for
every order on Sunday, January 1, 2017," "Expected and Actual do not
match when the test is conducted on December 31st, 2016," Actualtext;
Assert.assertNotEquals;
}
Assuming the aforementioned Scenario
is tested on December 31st, 2016, this Test Case will pass.
Assert True
The boolean result returned must be
"True" in order for this assertion to pass the test step. The example
that follows shows how a user verifies that the default checkbox is selected on
the supplied website.
testDefaultCheckbox() is public void.
new FirefoxDriver(), driver;
'driver.get'
('https://wiki.perficient.com/confluence/login.action?os destination=%2Fhomepage.action');
Driver.findElement(By.name("os
cookie")); defaultchk1
"n" plus defaultchk1.isSelected()
in the system output;
Assert.assertTrue(defaultchk1.isSelected());
"Assertion Passed successfully,"
System.out.print;
}
Assert false
Only when the boolean result returned
is "False" does "Assert False" succeed in the Test Step.
The example that follows shows how the user verifies that the optional checkbox
is not selected in the designated site.
TestOptionalCheckbox public void
new FirefoxDriver(), driver;
'driver.get'
('https://wiki.perficient.com/confluence/login.action?os destination=%2Fhomepage.action');
Driver.findElement(By.name("os
cookie")); optionalchk
System.out.print "n" +
optionalchk.isSelected() ;
Assert.assertFalse(optionalchk.isSelected());
"Assertion Passed successfully,"
System.out.print;
}
Assert Null
This assertion checks to see if the
object being tested is null and succeeds if the answer is yes.
Let's have a look at an aspect that
the user uses the Assert Null statement to confirm is enabled in the
application.
testEnabledText() is public void.
'driver.get'
('https://wiki.perficient.com/confluence/login.action?os destination=%2Fhomepage.action');
driver.findElement(By.xpath("/input[@id='text2′]"));
enatxt;
Assert.assertNull(enatxt.getAttribute(“disabled”));
"Assertion Passed successfully,"
System.out.print;
}
Because the "enatxt" element
does not have a disabled attribute, the enatxt.getAttribute("disabled")
object will return "null" in the example above. So, that claim will be
accepted.
Assert Not Null
In contrast to "Assert
Null," this assertion has the opposite effect. Therefore, this assertion
checks to see if the object being tested is not null, and it succeeds if the
result is true. Let's take an element that the programme is supposed to disable
into consideration. The user uses the Assert Not Null statement to confirm
this.
testDisabledText() is public void.
'driver.get'
('https://wiki.perficient.com/confluence/login.action?os destination=%2Fhomepage.action');
driver.findElement(By.xpath
("/input[@id='text21']") returns distxt;
Assert.assertNull(distxt.getAttribute(“disabled”));
"Assertion Passed successfully,"
System.out.print;
}
Assert Same
This assertion verifies if two objects
relate to the same object; if they do, the assertion succeeds; otherwise, it
fails. Using "Assert.assert same" to test two objects that refer to
the same object is demonstrated in the example below:
the AssertSameTest Demo public class
final String key + public String
getTextValue
Mapstring, string="">
textProps = new HashMap();
"text1," "value
1," textProps.put;
"text2," "value
2," textProps.put;
"text3," "value
3," textProps.put;
get textProps with key;
}
@Test
void public isSameTest()
Abc = new AssertSameTest Demo
(AssertSameTest Demo);
if (abc. getTextValue ("text1"),
abc. text1; getTextValue;
}
Assert Not Same
This assertion verifies that two
objects do not share a reference to the same object; if they do, the assertion
succeeds; otherwise, it fails. The usage of "Assert.assert Not same"
for testing two objects that relate to two separate objects is demonstrated in
the example below.
the AssertNotSameTest Demo public
class
final String key + public String
getTextValue
Mapstring, string="">
textProps = new HashMap();
"text1," "value
1," textProps.put;
"text2," "value
2," textProps.put;
"text3," "value
3," textProps.put;
get textProps with key;
}
@Test
the isNotSameTest() public void
Input xyz = new AssertNotSameTest Demo
();
xyz.getTextValue ("text1"),
xyz.assertNotSame(xyz. text3); getTextValue
}
Conclusion
Automation is bound to
rule production activities in the future. It is held to propel efficiency as
well as guarantee ROI. Given so, Selenium Automated Testing is bound to remain
the tool of choice. It may have its own disadvantages; however, its benefits
overshadow other tools, predominantly in three specific areas: cost,
flexibility and parallel testing.
Comments
Post a Comment