com.ionidea.RegressionNGA.Tests.util.DriverExtension.java Source code

Java tutorial

Introduction

Here is the source code for com.ionidea.RegressionNGA.Tests.util.DriverExtension.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ionidea.RegressionNGA.Tests.util;

import com.google.inject.Inject;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

/**
 *
 * @author Avicorn
 */
public class DriverExtension implements IDriverExtension {

    public void waitForPageLoaded(WebDriver driver, int standartWaitTime) {
        ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
            }
        };

        Wait<WebDriver> wait = new WebDriverWait(driver, standartWaitTime);
        try {
            wait.until(expectation);
        } catch (Throwable error) {
            Assert.fail("Timeout waiting for Page Load Request to complete.", error);
        }
    }

    public String getElementText(WebElement element) {
        return element.getAttribute("innerText");
    }
}