package ru.wt.gostev.AppManager;

import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

import java.util.concurrent.TimeUnit;

/**
 * Created by VGostev on 15.05.2017.
 */
public class HelperBase {
    protected WebDriver wd;

    public HelperBase(WebDriver wd) {
        this.wd = wd;
    }

    protected void click(By locator) {
        wd.findElement(locator).click();
    }

    protected void type(By locator, String text) {
        click(locator);
        if(text != null){
            String existingText = wd.findElement(locator).getAttribute("value");
            if(! text.equals(existingText)) {
            wd.findElement(locator).clear();
            wd.findElement(locator).sendKeys(text);
            }
        }
    }
    public  boolean isAlertPresent() { //метод для
        try {
            wd.switchTo().alert();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

    protected boolean isElementPresent(By locator) { //метод для проверки присутвует элемент на странице
        try{
            wd.findElement(locator);
            return true;
        }
        catch (NoSuchElementException e){
            return false;
        }
    }

}
