Selenium: try catch, isElementPresent

Last updated on:

There is some circumstances when you have logic operators, such as "if else", "try catch" etc. Today I faced one problem, our application having coach marks, user have an ability disable coach marks, so I were trying to implement detection isElementPresented on the page or no. In order to run tests without braking code and continue to perform test I were need catch exception (catch(org.openqa.selenium.NoSuchElementException e)). Since coach mark is basically an image it is quite complicated to discard it, I were using javascript Executer so test can perform taping on specific coordinate on the screen


import java.util.HashMap;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

 * @author slavkurochkin

public class CoachMarksPageObject extends AppDriver {
	@FindBy(xpath = "//*[contains(@name,'1-568h@2x.png')]")
    private WebElement coachMark;
	 @FindBy(xpath = "//UIAButton[1]")
	 private WebElement checkBox;
	 @FindBy(xpath = "//UIAImage[1]")
	 private WebElement coachMarkTapbleArea;
	public void coachMarksTest() throws InterruptedException {
			JavascriptExecutor js = (JavascriptExecutor) driver;
			js.executeScript("mobile: tap", new HashMap<String, Double>() {{ put("tapCount", 1.0); put("touchCount", 1.0); put("duration", 0.5); put("x", 104.0); put("y", 295.0); }});
			catch(org.openqa.selenium.NoSuchElementException e){
				System.out.print("Coach mark was not on the screen");
View Comments

Leave a Comment

Use markdown or basic HTML and be nice.