Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2005-2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/package io.fabric8.selenium;
import com.google.common.base.Function;
import io.fabric8.kubernetes.api.KubernetesHelper;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.utils.Millis;
import io.fabric8.utils.Strings;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.Arrays;
import java.util.List;
importstatic org.junit.Assert.assertNotNull;
importstatic org.junit.Assert.assertTrue;
importstatic org.junit.Assert.fail;
/**
* A facade and helper methods for writing {@link WebDriver} based Selenium tests
*/publicclassWebDriverFacadeextendsLogSupport{
privatefinal WebDriver driver;
privatefinal KubernetesClient client;
privatefinal String namespace;
privatelong defaultTimeoutInSeconds = 60;
publicWebDriverFacade(WebDriver driver, KubernetesClient client, String namespace){
super(driver);
this.driver = driver;
this.client = client;
this.namespace = namespace;
}
@Overridepublic WebDriverFacade getFacade(){
returnthis;
}
public String getNamespace(){
return namespace;
}
public KubernetesClient getClient(){
return client;
}
/**
* Returns the service URL for the given service name
*/public String getServiceUrl(String serviceName){
String url = KubernetesHelper.getServiceURL(client, serviceName, namespace, "http", true);
assertNotNull("No external Service URL could be found for namespace: " + namespace + " and name: " + serviceName, url);
assertTrue("No external Service URL could be found for namespace: " + namespace + " and name: " + serviceName, Strings.isNotBlank(url));
logInfo("Service " + serviceName + " in namespace: " + namespace + " URL = " + url);
return url;
}
/**
* Navigates to the given service name in the current namespace
*
* @return the URL navigated to
*/public String navigateToService(String serviceName){
String url = getServiceUrl(serviceName);
WebDriver driver = getDriver();
driver.navigate().to(url);
return url;
}
/**
* Finds an element or returns null if it could not be found
*/public WebElement findOptionalElement(By by){
try {
return getDriver().findElement(by);
} catch (NoSuchElementException e) {
returnnull;
} catch (Throwable e) {
logError("Failed to find " + by, e);
returnnull;
}
}
/**
* Find an optinoal element from a given element or return null
*
* @param element
* @param by
*/public WebElement findOptionalElement(WebElement element, By by){
try {
return element.findElement(by);
} catch (NoSuchElementException e) {
returnnull;
} catch (Throwable e) {
logError("Failed to find " + by, e);
returnnull;
}
}
/**
* Finds the element for the `by`, clears the field and sends the given text
*
* @return the element or null if it is not found
*/public WebElement clearAndSendKeys(By by, String text){
WebElement field = findOptionalElement(by);
if (field != null) {
field.clear();
field.sendKeys(text);
}
return field;
}
/**
* Returns a form facade for submitting a form
*/public FormFacade form(){
returnnew FormFacade(this);
}
publicbooleanuntil(ExpectedCondition condition){
return until(condition.toString(), defaultTimeoutInSeconds, condition);
}
publicbooleanuntil(String message, ExpectedCondition condition){
return until(message, defaultTimeoutInSeconds, condition);
}
publicbooleanuntil(String message, long timeoutInSeconds, ExpectedCondition condition){
returnnew WebDriverWait(getDriver(), timeoutInSeconds).withMessage(message).until(condition);
}
publicbooleanuntilLinkClicked(final By by){
return untilLinkClicked(defaultTimeoutInSeconds, by);
}
publicbooleanuntilSelectedByVisibleText(By by, String value){
return untilSelectedByVisibleText(defaultTimeoutInSeconds, by, value);
}
publicbooleanuntilSelectedByVisibleText(long timeoutInSeconds, final By by, final String value){
String message = "select " + by + " with value: " + value;
returnnew WebDriverWait(getDriver(), timeoutInSeconds).withMessage(message).until(new ExpectedCondition() {
@Overridepublic Boolean apply(WebDriver webDriver){
WebElement element = findOptionalElement(by);
if (element != null && element.isEnabled()) {
Select select = new Select(element);
try {
select.selectByVisibleText(value);
logInfo("" + by + " select " + select + " selected value: " + value);
returntrue;
} catch (NoSuchElementException e) {
logWait("" + by + " select " + select + " does not yet have value: " + value);
returnfalse;
}
} else {
logWait("" + by + " not enabled");
returnfalse;
}
}
});
}
publicbooleanuntilIsDisplayed(By firstBy, By secondBy){
return untilIsDisplayed(defaultTimeoutInSeconds, firstBy, secondBy);
}
publicbooleanuntilIsDisplayed(long timeoutInSeconds, final By firstBy, final By secondBy){
String message = "" + firstBy + " then " + secondBy + " is displayed";
return until(message, timeoutInSeconds, new ExpectedCondition() {
public Boolean apply(WebDriver driver){
WebElement element = findOptionalElement(firstBy);
if (element == null) {
logWait("" + firstBy + "");
returnfalse;
} else {
WebElement link = findOptionalElement(element, secondBy);
if (link != null && link.isDisplayed()) {
logInfo("" + firstBy + " then " + secondBy + " displayed");
returntrue;
} else {
logWait("" + firstBy + " then " + secondBy + " displayed");
returnfalse;
}
}
}
});
}
publicbooleanuntilElementPredicate(final By by, final Function elementPredicate){
return untilElementPredicate(defaultTimeoutInSeconds, by, elementPredicate);
}
publicbooleanuntilElementPredicate(long timeoutInSeconds, final By by, final Function elementPredicate){
String message = "" + by + " matches " + elementPredicate;
return until(message, timeoutInSeconds, new ExpectedCondition() {
public Boolean apply(WebDriver driver){
WebElement element = findOptionalElement(by);
if (element == null) {
logWait("" + by + "");
returnfalse;
} else {
Boolean value = elementPredicate.apply(element);
if (value != null && value.booleanValue()) {
logInfo("" + by + " matches " + elementPredicate + "");
returntrue;
} else {
logWait("" + by + " matches " + elementPredicate + "");
returnfalse;
}
}
}
});
}
publicbooleanuntilIsDisplayed(final By by){
return untilIsDisplayed(defaultTimeoutInSeconds, by);
}
publicbooleanuntilIsDisplayed(long timeoutInSeconds, final By by){
return untilElementPredicate(timeoutInSeconds, by, new Function() {
@Overridepublic String toString(){
return"element.isDisplayed()";
}
@Overridepublic Boolean apply(WebElement element){
return element.isDisplayed();
}
});
}
publicbooleanuntilIsEnabled(final By by){
return untilIsEnabled(defaultTimeoutInSeconds, by);
}
publicbooleanuntilIsEnabled(long timeoutInSeconds, final By by){
return untilElementPredicate(timeoutInSeconds, by, new Function() {
@Overridepublic String toString(){
return"element.isEnabled()";
}
@Overridepublic Boolean apply(WebElement element){
return element.isEnabled();
}
});
}
publicbooleanuntilLinkClicked(long timeoutInSeconds, final By by){
String message = "click link " + by;
return until(message, timeoutInSeconds, new ExpectedCondition() {
public Boolean apply(WebDriver driver){
WebElement link = findOptionalElement(by);
if (link != null) {
logInfo("Clicking link: " + by + "");
link.click();
logInfo("Clicked link: " + by + " now");
returntrue;
} else {
logInfo("Not found link " + by + "");
returnfalse;
}
}
});
}
/**
* Waits until one of the given elements is available
*/publicvoiduntilOneOf(final By... bys){
final List byList = Arrays.asList(bys);
String message = "One of these is available: " + byList;
until(message, defaultTimeoutInSeconds, new ExpectedCondition() {
@Overridepublic Boolean apply(WebDriver driver){
for (By by : bys) {
WebElement element = findOptionalElement(by);
if (element != null && element.isDisplayed() && element.isEnabled()) {
logInfo("Found " + element + " for " + by + "");
returntrue;
}
}
logInfo("Still not found any of " + byList + "");
returnfalse;
}
});
}
publicvoidsleep(long millis){
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
// ignore
}
}
publicbooleancurrentUrlStartsWith(String expectedUrl){
String currentUrl = getDriver().getCurrentUrl();
boolean answer = currentUrl != null && currentUrl.startsWith(expectedUrl);
if (!answer) {
logWarn("Current URL `" + currentUrl + "` does not start with `" + expectedUrl + "`");
}
return answer;
}
publicvoidassertCurrentUrlStartsWith(String expectedUrl){
String currentUrl = getDriver().getCurrentUrl();
boolean answer = currentUrl != null && currentUrl.startsWith(expectedUrl);
if (!answer) {
fail("Current URL `" + currentUrl + "` does not start with `" + expectedUrl + "`");
}
}
/**
* Lets wait until the link is visible then click it. If the click doesn't work lets retry a few times
* just in case
*/publicvoiduntilLinkClickedLoop(By by, String expectedUrl){
for (int i = 0; i < 10; i++) {
untilLinkClicked(by);
sleep(Millis.seconds(10));
if (currentUrlStartsWith(expectedUrl)) {
break;
} else {
logWarn("lets try re-clicking link: " + by);
}
}
assertCurrentUrlStartsWith(expectedUrl);
}
publiclonggetDefaultTimeoutInSeconds(){
return defaultTimeoutInSeconds;
}
publicvoidsetDefaultTimeoutInSeconds(long defaultTimeoutInSeconds){
this.defaultTimeoutInSeconds = defaultTimeoutInSeconds;
}
}