Friday, October 14, 2016

Assert.IsTrue Examples

Assert.IsTrue Examples

Assert.IsTrue method verifies that the specified condition is true. The assertion fails if the condition is false. Assert.IsTrue is common assertion used while testing web pages.
Lets take a look at some of the examples of Assert.IsTrue:

Example 1 of Assert.IsTrue - Assert button enabled

Below are two buttons “Click Me 1!” and "Click Me!". I right-clicked on "Click Me 1!" and selected "Inspect Element" as can be seen in the below image:

We are asserting if the button "Click Me!" (with id as 2) is enabled.

Example 2 of Assert.IsTrue - Assert page text

We are asserting if text on the page contains a particular string. Like if the page contains text "One account. All of Google."

Note: The driver.FindElement(By.TagName("body")).Text returns the text view of a web page after stripping off the HTML tags.

Example 3 of Assert.IsTrue - Assert page source

We are asserting if page source contains a particular string.

The page source is raw HTML returned from the server. If I uncomment the line starting with "String Y .." in the above code and debug the code then below is the value of Y as we see it using Text Visualizer.

Example 4 of Assert.IsTrue - Assert checkbox selected

Below are the two checkboxes and their HTML code.

Here we are asserting if the Checkbox2 is checked.

Example 5 of Assert.IsTrue - Assert image displayed

Below is the Google image and the code you get by right-clicking on the image and selecting "Inspect Element".

We are asserting if the image is being displayed.