use of org.openqa.selenium.support.ui.ExpectedCondition in project devonfw-testing by devonfw.
the class DriverExtention method waitForElement.
public WebElement waitForElement(final By by) throws BFElementNotFoundException {
BasePage.getAnalytics().sendMethodEvent(BasePage.analitycsCategoryName);
long startTime = System.currentTimeMillis();
WebElement element = null;
try {
element = webDriverWait().until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver driver) {
return driver.findElement(by);
}
});
} catch (TimeoutException | NoSuchElementException e) {
boolean isTimeout = true;
throw new BFElementNotFoundException(by, isTimeout, BasePage.EXPLICITYWAITTIMER);
}
BFLogger.logTime(startTime, "waitForElement()", by.toString());
return element;
}
use of org.openqa.selenium.support.ui.ExpectedCondition in project devonfw-testing by devonfw.
the class DriverExtention method waitForPageLoaded.
public void waitForPageLoaded() throws BFElementNotFoundException {
long startTime = System.currentTimeMillis();
final String jsVariable = "return document.readyState";
ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript(jsVariable).equals("complete");
}
};
int progressBarWaitTimer = BasePage.PROGRESSBARWAITTIMER;
WebDriverWait wait = webDriverWait(progressBarWaitTimer);
try {
wait.until(expectation);
} catch (TimeoutException | NoSuchElementException e) {
boolean isTimeout = true;
throw new BFElementNotFoundException(By.cssSelector(jsVariable), isTimeout, progressBarWaitTimer);
}
BFLogger.logTime(startTime, "waitForPageLoaded");
}
use of org.openqa.selenium.support.ui.ExpectedCondition in project selenium_java by sergueik.
the class ChromePagePerformanceTest method beforeMethod.
@Before
public void beforeMethod() throws IOException {
driver.get(baseURL);
// Wait for page url to update
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.pollingEvery(500, TimeUnit.MILLISECONDS);
ExpectedCondition<Boolean> urlChange = driver -> driver.getCurrentUrl().matches(String.format("^%s.*", baseURL));
wait.until(urlChange);
System.err.println("Current URL: " + driver.getCurrentUrl());
/*
// Take screenshot
// under headless Chrome, some vendor pages behave differently e.g.
// www.royalcaribbean.com redirects to the
// "Oops... Looks like RoyalCaribbean.com is on vacation" page
File screenShot = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
// To get the width of image.
BufferedImage readImage = ImageIO.read(screenShot);
int width = readImage.getWidth();
FileUtils.copyFile(screenShot, new File(System.getProperty("user.dir")
+ System.getProperty("file.separator") + "test.png"));
*/
}
use of org.openqa.selenium.support.ui.ExpectedCondition in project selenium_java by sergueik.
the class PlunkerTest method testFullScreeenInNewWindow.
// Failed tests: afterMethod(com.mycompany.app.PlunkerTest): no such window:
// target window already closed(..)
@Test(enabled = false)
public void testFullScreeenInNewWindow() {
projectId = "WFJYrM";
driver.get(String.format("https://plnkr.co/edit/%s/?p=info", projectId));
WebElement runButton = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("body > nav button i.icon-play")));
assertThat(runButton, notNullValue());
highlight(runButton);
runButton.click();
WebElement previewIframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("iframe[name='plunkerPreviewTarget']")));
assertThat(previewIframe, notNullValue());
WebDriver iframe = driver.switchTo().frame(previewIframe);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
// System.err.println(iframe.getPageSource());
// the fullscreen button is not in the preview frame
driver.switchTo().defaultContent();
WebElement fullScreenButton = null;
try {
fullScreenButton = wait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver d) {
Optional<WebElement> e = d.findElements(By.cssSelector("button#expand-preview")).stream().filter(o -> {
String t = o.getAttribute("title");
return (Boolean) (t.contains("Launch the preview in a separate window"));
}).findFirst();
return (e.isPresent()) ? e.get() : (WebElement) null;
}
});
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
}
assertThat(fullScreenButton, notNullValue());
String currentHandle = null;
try {
currentHandle = driver.getWindowHandle();
// System.err.println("Thread: Current Window handle: " + currentHandle);
} catch (NoSuchWindowException e) {
}
// open fullscreen view in a new browser window.
String openinLinkNewBrowserWindow = Keys.chord(Keys.SHIFT, Keys.RETURN);
fullScreenButton.sendKeys(openinLinkNewBrowserWindow);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
// confirm it opens in a new browser window.
windowHandles = driver.getWindowHandles();
assertThat(windowHandles.size(), equalTo(2));
Iterator<String> windowHandleIterator = windowHandles.iterator();
while (windowHandleIterator.hasNext()) {
String handle = windowHandleIterator.next();
if (!handle.equals(currentHandle)) {
driver.switchTo().window(handle);
assertThat(getBaseURL(), equalTo("https://run.plnkr.co"));
// System.err.println(getBaseURL());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
driver.close();
// System.err.println("After close");
try {
driver.switchTo().defaultContent();
System.err.println("After defaultcontext");
} catch (NoSuchWindowException e) {
}
}
}
}
use of org.openqa.selenium.support.ui.ExpectedCondition in project selenium_java by sergueik.
the class SeleniumEasyTest method alertConfirmTest.
@Test(enabled = true)
public void alertConfirmTest() {
// wrong selector
String buttonSelector = String.format("//*[@id='easycont']//div[@class='panel-heading'][contains(normalize-space(.), '%s')]/..//button", "Java Script Confirm Box");
try {
WebElement buttonElement = wait.until(new ExpectedCondition<WebElement>() {
Predicate<WebElement> textCheck = _element -> {
String _text = _element.getText();
System.err.println("in filter: Text = " + _text);
return (Boolean) (_text.contains("Click me!"));
};
@Override
public WebElement apply(WebDriver d) {
System.err.println("Locating " + buttonSelector);
Optional<WebElement> e = d.findElements(By.xpath(buttonSelector)).stream().filter(textCheck).findFirst();
return (e.isPresent()) ? e.get() : (WebElement) null;
}
});
System.err.println("Acting on: " + buttonElement.getAttribute("outerHTML"));
highlight(buttonElement);
flash(buttonElement);
buttonElement.click();
sleep(1000);
} catch (Exception e) {
System.err.println("Exception: " + e.toString());
verificationErrors.append("Exception: " + e.toString());
}
try {
// dismiss alert
driver.switchTo().alert().dismiss();
} catch (NoAlertPresentException ex) {
// Alert not present - ignore
} catch (WebDriverException ex) {
System.err.println("Alert was not handled : " + ex.getStackTrace().toString());
return;
}
assertThat(driver.findElement(By.id("confirm-demo")).getText(), is(equalTo("You pressed Cancel!")));
}
Aggregations