Miscellaneous Assertions

Explore more about assertions by learning CSS, JavaScript, and image assertions with Selenium.

Assert if an image is present

We can assert if an image is present by:

assert(driver.findElement(By.id("next_go")).isDisplayed());

Assert element location and width

We can assert the width and location of an element by:

driver.findElement(By.id("next_go")).getSize().then(function(the_size) {
  assert.equal(46,the_size.width)
});
driver.findElement(By.id("next_go")).getLocation().then(function(the_loc) {
  assert(the_loc.x > 100)
});

Assert element CSS style

We can assert an element by its CSS style values using the following method:

driver.findElement(By.id("highlighted")).getCssValue("background-color").then(  
  function(bc) {
    assert.equal("rgba(206, 218, 227, 1)", bc)
});
driver.findElement(By.id("highlighted")).getCssValue("font-size").then(function(fsize) {
  assert.equal("15px", fsize)
});

Assert JavaScript errors on a web page

The JavaScript errors can be inspected in the browser’s console as:

Get hands-on with 1400+ tech skills courses.