Button Assertions

Look at the details of Selenium button assertions, and test it out yourself.

Assert if a button is present

Just like hyperlinks, we can use isDisplayed() to check whether a control is present on a web page or not. This check applies to most of the web controls in Selenium. In the following script, we are first using isDisplayed() function to check the presence of Selenium button on the web page. Once its presence is assured, we then move on to change its properties from visible to hidden and then to visible again while asserting it at each step.

assert(driver.findElement(By.id("choose_selenium_btn")).isDisplayed());
driver.findElement(By.linkText("Hide")).click().then(function(){ 
  driver.findElement(By.id("choose_selenium_btn")).isDisplayed().then(function(displayed){
   assert(!displayed);
  });
});
driver.findElement(By.linkText("Show")).click().then(function(){
  driver.findElement(By.id("choose_selenium_btn")).isDisplayed().then(function(displayed){
    assert(displayed);
  });
})

Assert if a button is disabled

A web control can be in a disabled state as well. A disabled button is unclickable, and is displayed as:

Get hands-on with 1200+ tech skills courses.