/** * @category Get Field Label */ private WebElement getFieldByLabel(String fieldLabel) { this.focusForm(true); WebElement field = null; List<WebElement> allLabels = _driver.findElements(By.className("label-text")); for (int i = 0; i < allLabels.size(); i++) { if (allLabels.get(i).getText().trim().equalsIgnoreCase(fieldLabel)) { field = allLabels.get(i).findElement(By.xpath("../../..")); break; } } if(field == null) { throw new NotFoundException("Field with the label of ["+fieldLabel+"] was not found."); } return field; }
@Test public void shouldCacheWindowHandleOnceFound() { ControllableView view = new ControllableView(); view.setIsLoaded(true); WebDriverTarget target = WebDriverTargets.withViewLoaded(view, context); WebDriver firstSwitch = target.switchTo(locator); view.setIsLoaded(false); try { WebDriver secondSwitch = target.switchTo(locator); assertEquals(firstSwitch, secondSwitch); } catch (NotFoundException e) { fail("Second switch tried to find window again:\n" + e); } }
/** * @param driver * @param element * @param wait * @return */ public boolean waitForElementToDissappear(WebDriver driver, By element, Integer wait) { Integer count = 0; while (count < wait) { try { driver.findElement(element); try { Thread.sleep(1000); } catch (InterruptedException e1) { } } catch (NotFoundException e) { return true; } count = count + 1; } return false; }
/** * Searches for the Application Name, Clicks the module under the application. * * @param applicationName * - The name of the Application * @param moduleName * - The name of the module */ public void ChooseApplicationAndModule(String applicationName, String moduleName) { this.focusForm(false); WebElement filterNavigator = _wait.until(ExpectedConditions.elementToBeClickable(By.id("filter"))); filterNavigator.clear(); filterNavigator.sendKeys(applicationName); WebElement applicationList = _wait .until(ExpectedConditions.visibilityOfElementLocated(By.id("concourse_application_tree"))); List<WebElement> applications = applicationList .findElements(By.xpath("//li[@ng-repeat='application in applications']")); for (int i = 0; i < applications.size(); i++) { if (applications.get(i).findElement(By.tagName("a")).getText().trim().equalsIgnoreCase(applicationName)) { // Found Application WebElement application = applications.get(i); WebElement moduleList = application.findElement(By.tagName("ul")); List<WebElement> modules = moduleList.findElements( By.xpath("//li[@ng-repeat='appModule in ::application.modules track by appModule.id']")); for (int j = 0; j < modules.size(); j++) { if (modules.get(j).getText().trim().equalsIgnoreCase(moduleName)) { // Found Module modules.get(j).click(); return; } } throw new NotFoundException("Module by the name of ["+moduleName+"] was not found."); } } throw new NotFoundException("Application by the name of ["+applicationName+"] was not found."); }
default By locator(String id) { if (id.contains("//") || id.contains(".//")) { return By.xpath(id.replace("xpath=", "")); } else if(id.contains("css")){ return By.cssSelector(id.replace("css=", "")); }else if(id.contains("id")) { return By.id(id.replace("id=", "")); }else if(id.contains("name")) { return By.name(id.replace("name=", "")); }else if(id.contains("link")) { return By.linkText(id.replace("link=", "")); }else { throw new NotFoundException("The locator type is not found"); } }
@Override public boolean isElementPresent() { try { if ( (element.getUiDriver() instanceof HiddenBrowserDriver) && (element instanceof UiAlert || element instanceof UiPrompt || element instanceof UiConfirm)) { return false; } if (element instanceof UiAlert || element instanceof UiConfirm) { Alert dialog = webDriver.switchTo().alert(); return dialog != null; } else if (element instanceof UiPrompt) { Alert prompt = webDriver.switchTo().alert(); return prompt.getText() != null && !prompt.getText().equals("false"); // return seleniumBrowser.isPromptPresent(); // Not implemented yet } HiddenHtmlElementLocator.findElement(this.element, null, false); return true; } catch (NotFoundException nfe) { return false; } catch (ElementNotFoundException enfe) { return false; } }
public void probe() { foundElement = null; try { foundElement = context.findElement(criteria); } catch (NotFoundException tryAgain) { } }
public void probe() { foundElement = null; parent.probe(); if (!parent.isSatisfied()) return; try { foundElement = parent.found().findElement(criteria); } catch (NotFoundException tryAgain) { } }
public void findElement() { super.findElement(); try { rows = element.findElements(By.tagName("tr")); } catch (NotFoundException e) { } }
/** * Wait single method in the result tree marked as failed (red color). * * @param nameOfFailedMethods name of that should fail */ public void waitMethodMarkedAsFailed(String nameOfFailedMethods) { FluentWait<WebDriver> wait = new FluentWait<WebDriver>(seleniumWebDriver) .withTimeout(ATTACHING_ELEM_TO_DOM_SEC, TimeUnit.SECONDS) .pollingEvery(200, TimeUnit.MILLISECONDS) .ignoring(NotFoundException.class, StaleElementReferenceException.class); String xpathToExpectedMethod = "//span[@id='%s']/following-sibling::*/div[text()='%s']"; wait.until( ExpectedConditions.visibilityOfElementLocated( By.xpath( String.format( xpathToExpectedMethod, METHODS_MARKED_AS_FAILED, nameOfFailedMethods)))); }
protected SearchContextWait(final SearchContext input, final Clock clock, final Sleeper sleeper, final long timeout, final TimeUnit timeoutTimeUnit, final long sleep, final TimeUnit sleepTimeUnit) { super(input, clock, sleeper); withTimeout(timeout, timeoutTimeUnit); pollingEvery(sleep, sleepTimeUnit); ignoring(NotFoundException.class); }
@Override public boolean isPresent() { try { getWrappedElement(); return true; } catch (NotFoundException e) { return false; } }
private WebElement webElement() { if (cached == null) { try { cached = getWrappedElement(); } catch (NotFoundException e) { throw new FindableNotPresentException(this, e); } } return cached; }
@Override public boolean isPresent() { try { driver().getTitle(); return true; } catch (NotFoundException e) { return false; } }
@PublicAtsApi public void navigateToFrame( WebDriver webDriver, UiElement element ) { if (lastWebDriver != webDriver) { // this is a new WebDriver instance lastWebDriver = webDriver; lastFramesLocation = ""; } String newFramesLocationProperty = element != null ? element.getElementProperty("frame") : null; try { if (newFramesLocationProperty == null) { // No frame selection. Go to top frame if not there yet if (!"".equals(lastFramesLocation)) { log.debug("Go to TOP frame"); webDriver.switchTo().defaultContent(); lastFramesLocation = ""; } } else { lastFramesLocation = newFramesLocationProperty; log.debug("Go to frame: " + newFramesLocationProperty); String[] newFramesLocation = newFramesLocationProperty.split("\\->"); webDriver.switchTo().defaultContent(); for (String frame : newFramesLocation) { if (frame.startsWith("/") || frame.startsWith("(/")) { WebElement frameElement = webDriver.findElement(By.xpath(frame.trim())); webDriver.switchTo().frame(frameElement); } else { webDriver.switchTo().frame(frame.trim()); } } } } catch (NotFoundException nfe) { String msg = "Frame not found. Searched by: '" + (element != null ? element.getElementProperty("frame") : "") + "'"; log.debug(msg); throw new ElementNotFoundException(msg, nfe); } }
/** * xFramium can be used with the standard Selenium and Appium interfaces. Use the TestContainer and TestPackge as descriped below as well * as the hook into the internal reporting system * @param testContainer * @throws Exception */ @Test( dataProvider = "deviceManager" ) public void testNativeSeleniumAppium( TestContainer testContainer ) throws Exception { // // Get access to the test and device // TestPackage testPackage = testPackageContainer.get(); TestName testName = testPackage.getTestName(); // // Enable some basic reporting // DeviceWebDriver webDriver = testPackage.getConnectedDevice().getWebDriver(); webDriver.setReportingElement( true ); // // Optionally start a step container to allowing for reporting structure // startStep( testName, "Step One", "Testing the Toggle Button" ); String beforeClick = webDriver.findElement( By.id( "singleModel" ) ).getText(); webDriver.findElement( By.xpath( "//button[text()='Toggle Value']" ) ).click(); String afterClick = webDriver.findElement( By.id( "singleModel" ) ).getText(); Assert.assertTrue( false ); Assert.assertFalse( (Boolean) executeStep( "COMPARE2", "Step One", CompareType.STRING.name(), new String[] { "Value One==" + beforeClick, "Value Two==" + afterClick }, webDriver ).get( "RESULT" ) ); dumpState( webDriver ); stopStep( testName, StepStatus.SUCCESS, null ); startStep( testName, "Step Two", "Testing Attributes" ); String typeAttribute = webDriver.findElement( By.xpath( "//button[text()='Toggle Value']" ) ).getAttribute( "type" ); Assert.assertTrue( (Boolean) executeStep( "COMPARE2", "Step One", CompareType.STRING.name(), new String[] { "Value One==" + typeAttribute, "Value Two==button" }, webDriver ).get( "RESULT" ) ); stopStep( testName, StepStatus.SUCCESS, null ); dumpState( webDriver, "afterAttribute", 5, 5 ); startStep( testName, "Step Three", "Testing Wait For" ); Assert.assertFalse( webDriver.findElement( By.id( "deleteButton" ) ).isDisplayed() ); webDriver.findElement( By.xpath( "//div[@id='aOpen']//a" ) ).click(); Assert.assertTrue( new WebDriverWait( webDriver, 12 ).ignoring( NotFoundException.class ).until( ExpectedConditions.visibilityOfElementLocated( By.id( "deleteButton" ) ) ).isDisplayed() ); stopStep( testName, StepStatus.SUCCESS, null ); dumpState( webDriver, "afterWaitFor", 2, 5 ); }
/** * Wait will ignore instances of NotFoundException that are encountered * (thrown) by default in the 'until' condition, and immediately propagate * all others. You can add more to the ignore list by calling * ignoring(exceptions to add). * * @param context * The SearchContext instance to pass to the expected conditions * @param clock * The clock to use when measuring the timeout * @param sleeper * Object used to make the current thread go to sleep. * @param timeOutInSeconds * The timeout in seconds when an expectation is * @param sleepTimeOut * The timeout used whilst sleeping. Defaults to 500ms called. */ public SearchContextWait(SearchContext context, Clock clock, Sleeper sleeper, long timeOutInSeconds, long sleepTimeOut) { super(context, clock, sleeper); withTimeout(timeOutInSeconds, TimeUnit.SECONDS); pollingEvery(sleepTimeOut, TimeUnit.MILLISECONDS); ignoring(NotFoundException.class); this.context = context; }